Search code examples
sqlsql-servert-sqldatetime

TSQL DATETIME ISO 8601


I have been given a specification that requires the ISO 8601 date format, does any one know the conversion codes or a way of getting these 2 examples:

ISO 8601 Extended Date 2000-01-14T13:42Z 
ISO 8601 Basic Date 20090123T105321Z

Solution

  • When dealing with dates in SQL Server, the ISO-8601 format is probably the best way to go, since it just works regardless of your language and culture settings.

    In order to INSERT data into a SQL Server table, you don't need any conversion codes or anything at all - just specify your dates as literal strings

    INSERT INTO MyTable(DateColumn) VALUES('20090430 12:34:56.790')
    

    and you're done.

    If you need to convert a date column to ISO-8601 format on SELECT, you can use conversion code 126 or 127 (with timezone information) to achieve the ISO format.

    SELECT CONVERT(VARCHAR(33), DateColumn, 126) FROM MyTable
    

    should give you:

    2009-04-30T12:34:56.790