Ever needed a way of returning today’s date without returning the time portion as well? I do.
Here goes:
SELECT CAST(ROUND(CAST(getdate() AS real),0,1) AS datetime)
So instead of getdate() returning something like “2006-07-24 16:48:29.910″, this nifty little function gives you it to you like “2006-07-24 00:00:00.000″.
Tested using MSSQL 2005.
This will run faster and have less errors:
SELECT CAST(CAST(getdate() as int) as datetime)
The datetime field is stored as 4 byte integers. And looks like “date.time” with the time as the decimal. Casting as an int causes the decimal to drop off giving a true time.
The example from BBCoder
“SELECT CAST(CAST(getdate() as int) as datetime)”
only works if it’s early in the day. Running this can round the date up to the following day, thereby returning wrong data.
U can also try this…
Select cast(getdate() as varchar(12))
This returns MMM dd yyyy eg Mar 28 2008 in Sql server 2000
Great code this worked awesome. Exactly what I was looking for.