Home » Code

How to Floor an SQL DateTime

3. July 2008 by thebeebs 4 Comments

If you call GetDate you get todays date and time but what if you just want the date and not the time.

SQL provides no simple mechanism to do this but the solution below is probably the best way I’ve found:

CAST(FLOOR( CAST( GETDATE() AS FLOAT ) )AS DATETIME)

So a full example would be:

DECLARE @CurrentDate Datetime SET @CurrentDate = GETDATE() DECLARE @CurrentDateFloor Datetime SET @CurrentDateFloor  = CAST(FLOOR( CAST( GETDATE() AS FLOAT ) )AS DATETIME) SELECT @CurrentDateFloor  

2007–30-07 12:12:45 becomes 2007-30-07 00:00:00

Comments

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading