开发者

Using Date datatype for removing time info from GetDate() is ok?

开发者 https://www.devze.com 2023-03-01 03:30 出处:网络
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 < GETDAT

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消