adsense

Thursday, January 3, 2013

SQL Server convert string to datetime

If you have used any of the fields in a table as a varchar to store date when querying the table will not return the expected result.
E.g
SELECT * from ISSUES where CREATED_DATE
  between '08/16/2012' AND '01/27/2013
Here CREATED_DATE field has been defined as string. To resolve this you;ll have to use SQL's convert funciton as follows
SELECT * from ISSUES where CONVERT(datetime,CREATED_DATE,120)
  between '08/16/2012' AND '01/27/2013'

For more date time formatting options check link below
http://msdn.microsoft.com/en-us/library/ms187928.aspx

Cheers
Samitha

No comments:

Post a Comment