adsense

Tuesday, July 9, 2013

SQL Server getdate in YYYY-MM-DD without time

If you ever wanted to get a date field without time? This is a common question that have been asked and there are several ways to get the desired result.

There are two options but you should select the best one to match your requirement

1) Get date without time part

 SELECT CONVERT(VARCHAR(12), GETDATE(), 23)

This will give the following output

2013-07-10

2) Get date with time part represented as zeros

SELECT CONVERT(datetime,CONVERT(char(10), GetDate(),126))
 
This one converts the result back to date time with the following output 
 
2013-07-10 00:00:00.000
 
Cheers
Samitha