Nexus6Studio

mikehouston.net

Simple TSQL datetime tricks...

by mhouston 26. March 2008 16:25

Someone ask me about working with dates recently in TSQL...I was too busy at the time to stop and remember this...I did get back to them later ;-)

Here it is. I'm sure most people know this, but at least I want forget next time...

DECLARE @CurrentDateWithTime datetime
DECLARE @CurrentDateZeroTime datetime
DECLARE @TomorrowZeroTime datetime
 
SET @CurrentDateWithTime = GETDATE()
SET @CurrentDateZeroTime = CAST(FLOOR(CAST(@CurrentDateWithTime AS float)) AS datetime)
SET @TomorrowZeroTime = CAST(CEILING(CAST(@CurrentDateWithTime AS float)) AS datetime)
 
PRINT @CurrentDateWithTime
PRINT @CurrentDateZeroTime
PRINT @TomorrowZeroTime

Why does it work? Add the following PRINT statements and it becomes obvious.

PRINT CAST(@CurrentDateWithTime AS float)
PRINT CAST(@CurrentDateZeroTime AS float)
PRINT CAST(@TomorrowZeroTime AS float)

 

The output should be :

Mar 26 2008  6:22PM
Mar 26 2008 12:00AM
Mar 27 2008 12:00AM
39531.8
39531
39532

 

Technorati Tags: ,,,

Currently rated 4.9 by 9 people

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

, , ,

SQL

Related posts

Add comment


(Will show your Gravatar icon)  

  Country flag

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



Live preview

May 9. 2008 02:16

Sign in
Disclaimer
The opinions expressed herein are my own personal opinions and do not represent any of my associates' views in anyway.
“A foolish consistency is the hobgoblin of little minds, adored by little statesmen and philosophers and divines. With consistency a great soul has simply nothing to do. He may as well concern himself with his shadow on the wall. Speak what you think now in hard words, and tomorrow say what tomorrow thinks in hard words again, though it contradict everything you say today. "Ah, so you shall be sure to be misunderstood." Is it so bad, then, to be misunderstood? Pythagoras was misunderstood, and Jesus and Socrates and Luther and Copernicus and Galileo and Newton, and every pure and wise spirit that ever took flesh. To be great is to be misunderstood.” - Ralph Waldo Emerson
© Copyright 2008 Nexus6Studio & Mike Houston