Eden Ridgway's Blog

.Net and Web Development Information

  Home :: Contact :: Syndication  :: Login
  105 Posts :: 1 Stories :: 78 Comments :: 3 Trackbacks

Search

Article Categories

Archives

Post Categories

Development

General

Have you ever been irritated by the lack of a function in SQL server that returns only a date, here are two SQL scripts that can be used to return just the date part. You can use the good old faithful CONVERT function, which was the first thing that sprung to my mind, like this:

SELECT CONVERT(datetime, CONVERT(varchar(10), getdate(), 101))
>

The one fear I have with the script above is that it may be affected by the machines locale. While I've been told that SQL always correctly interprets YYYYMMDD, I've only ever tested against UK, US and South African locales which hardly makes that assumption conclusive.

Another approach, which is far safer and the one I'd recommend, is to use DateAdd:

SELECT DATEADD(dd, DATEDIFF(dd,0,getdate()), 0)
So how, does this work? Well when 0 is converted to a datetime value, it is interpreted as 1 Jan 1900 00:00:00. So the SQL above first gets the difference in days between today and the 1 Jan 1900 and then adds that difference back 1 Jan 1900, thereby removing the time component.
posted on Monday, July 04, 2005 10:48 AM
Comments have been closed on this topic.