how to insert the exact date in database

  • Thread starter Thread starter Saimvp
  • Start date Start date
S

Saimvp

Hello to all.
Please help me.
I'm inserting a date into sqlserver 2005 using C#3. my problem is while
insert is successfull my date and time.
It's possible to insert only a date?
 
Hi,

SQLServer 2005 doesn't support only Date, so no, you can only insert a
DateTime.Today to simulate only Date. This will change with SQLServer 2008,
which supports Date, Time and DateTime and a few others as well.
 
Morten Wennevik said:
Hi,

SQLServer 2005 doesn't support only Date, so no, you can only insert a
DateTime.Today to simulate only Date. This will change with SQLServer
2008,
which supports Date, Time and DateTime and a few others as well.

Coming soon: more types! I love it!!!!!!!!!
 
Hi,

SQLServer 2005 doesn't support only Date, so no, you can only insert a
DateTime.Today to simulate only Date.  This will change with SQLServer 2008,
which supports Date, Time and DateTime and a few others as well.

--
Happy Coding!
Morten Wennevik [C# MVP]



Saimvp said:
Hello to all.
Please help me.
I'm inserting a date into sqlserver 2005 using C#3. my problem is while
insert is successfull my date and time.
It's possible to insert only a date?

- Show quoted text -

For what it is worth here are 2 UDF I have found that will set the
time portion to 12:00:00a and make comparisons work better. The second
one is more cryptic but supposedly faster.

/dg

-- #1
Create function [dbo].[DateOnly](@DateTime DateTime)
-- Returns @DateTime at midnight; i.e., it removes the time portion of
a DateTime value.
returns datetime
as
begin
return dateadd(dd,0, datediff(dd,0,@DateTime))
end
-- #2
CREATE FUNCTION [dbo].[GetDateElement]( @inDateTime as DATETIME )
RETURNS datetime AS
BEGIN
RETURN (select CAST(FLOOR( CAST(@inDateTime AS FLOAT ) )AS
DATETIME))
END
 
The datetime type in SQL 2005 has both date and Time, but there is nothing to
stop you from inserting only the date part of that and having all the times
be the same (I think it ends up as midnight of that day).
Alternately, you COULD make a varchar which stores the date as a string.
That would kill date comparisons, but would allow you to store just the
date...

Ethan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top