Typically I need to remove time info from GETDATE()
. I need this because I have some DateTime fields where I know I am storing only date information so to make reliable comparisons (MyDate < GETDATE()
) I need to remove time information from GETDATE()
). Of course I could use the DATE
datatype in MyDate
, but this is ok for new applications, not for legacy ones.
I used to do it with CAST
, but since 开发者_如何学Cin SQL Server 2008 there is also the DATE
datatype it seems more readable.
Old approach
DECLARE @Today DateTime
SET @Today = (CAST(FLOOR(CAST( GETDATE() AS FLOAT)) AS DATETIME))
select @Today
New approach
DECLARE @TodayDate Date
Set @TodayDate = GETDATE()
select @TodayDate
May I go with the second or is there any caveat? (of coruse I use 2008 only!)
No caveats. Indeed it is the best way according to this answer.
精彩评论