How to Floor an SQL DateTime

by thebeebs 7/3/2008 2:10:31 PM

 

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

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Related posts

Comments

8/13/2008 1:37:31 PM

Tomislav

select cast(convert(varchar,getdate(),106) as datetime)

Tomislav hr

8/14/2008 8:07:46 PM

thebeebs

I think I prefer my version

thebeebs gb

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

8/21/2008 6:13:43 PM

Powered by BlogEngine.NET 1.3.1.0
Theme by Mads Kristensen

About the author

Name of author Author name
Something about me and what I do.

E-mail me Send mail

Calendar

<<  August 2008  >>
MoTuWeThFrSaSu
28293031123
45678910
11121314151617
18192021222324
25262728293031
1234567

View posts in large calendar

Recent posts

Recent comments

Authors

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008

Sign in