1

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 in 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!)

UnDiUdin
  • 14,924
  • 39
  • 151
  • 249
  • It is unclear what do you need. Please describe tables with columns and write what do you want to change. – Dalex Apr 27 '11 at 08:50
  • I just need to have TODAY DATE as reference because I use it in WHERE conditions, for example `where MyTable.Date >= @Today`. If I use `where MyTable.Date >= GetDate()` I don't get correct results, since GetDate contains also time information. – UnDiUdin Apr 27 '11 at 09:41
  • MyTable.Date>=CAST(GetDate() as date) is enough. – Dalex Apr 27 '11 at 10:07
  • Yes you are right, that one is also an option. The reason why I am looking for a compact version of "today without time info" is that in the same query I am may be using it several times. So imagine it is 10 times I prefer to write @Today than `CAST(GETDATE() AS DATE)`. – UnDiUdin Apr 27 '11 at 11:45

1 Answers1

1

No caveats. Indeed it is the best way according to this answer.

Community
  • 1
  • 1
Martin Smith
  • 438,706
  • 87
  • 741
  • 845