Wednesday, December 31, 2008

Convert DateTime in MSSQL

If you need to convert the format of a DateTime field in your SQL statement, utilize the following SQL function:

SELECT CONVERT(varchar, DateField, 101) AS DateField FROM [someTable]

Where DateField is the field name that holds your date, and 10 being the format. See this page for formats: http://msdn.microsoft.com/en-us/library/aa226054.aspx

Problems: If you're looking to sort this field, don't expect it to sort like a DateTime field as this converts it to a varchar field.

No comments: