SQL Server 2005 Convert DateTime Format
MS SQL Server 2005 provides CONVERT function to change or convert the DateTime formats.
CONVERT Function Syntax
CONVERT(datatype, datetime string, date style)
DateType parameter accepts the following types:
1. nchar, nvarchar, char, varchar, binary or varbinary. DataType length is optional.
2. DateTime String parameter accepts the datatime variable, datetime type column name or getdate() function to convert the datetime format.
3. Date Style parameter accepts the style number in which you want to convert the datetime.
Following are the SQL query Syntax to convert DateTime format:
Convert Datetime into mon dd yyyy hh:miAM (or PM) format
SELECT CONVERT(varchar, getdate(), 100)
Convert Datetime into Default + milliseconds mon dd yyyy hh:mi:ss:mmmAM (or PM) format
SELECT CONVERT(varchar, getdate(), 109)
Convert Datetime into Europe default + milliseconds dd mon yyyy hh:mm:ss:mmm(24h) format
SELECT CONVERT(varchar, getdate(), 113)
Convert Datetime into ODBC canonical yyyy-mm-dd hh:mi:ss(24h) format
SELECT CONVERT(varchar, getdate(), 120)
Convert Datetime into ODBC canonical (with milliseconds) yyyy-mm-dd hh:mi:ss.mmm(24h) format
SELECT CONVERT(varchar, getdate(), 121)
Convert Datetime into ISO8601 yyyy-mm-dd Thh:mm:ss:mmm(no spaces) format
SELECT CONVERT(varchar, getdate(), 126)
Continue to next tutorial: SQL Server 2005 Convert Date Format to learn how to convert the date values to different types of formats.
