TSQL DATETIME ISO 8601

前端 未结 7 1129
一整个雨季
一整个雨季 2020-12-04 12:57

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:



        
7条回答
  •  没有蜡笔的小新
    2020-12-04 13:25

    You technically have two options when speaking of ISO dates.

    In general, if you're filtering specifically on Date values alone OR looking to persist date in a neutral fashion. Microsoft recommends using the language neutral format of ymd or y-m-d. Which are both valid ISO formats.

    Note that the form '2007-02-12' is considered language-neutral only for the data types DATE, DATETIME2, and DATETIMEOFFSET.

    Because of this, your safest bet is to persist/filter based on the always netural ymd format.

    The code:

    select convert(char(10), getdate(), 126) -- ISO YYYY-MM-DD
    select convert(char(8), getdate(), 112) -- ISO YYYYMMDD (safest)
    

提交回复
热议问题