TSQL DATETIME ISO 8601

前端 未结 7 1073
一整个雨季
一整个雨季 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:38

    For ISO 8601 format for Datetime & Datetime2, below is the recommendation from SQL Server. It does not support basic ISO 8601 format for datetime(yyyyMMddThhmmss).

    DateTime

    YYYY-MM-DDThh:mm:ss[.mmm]

    YYYYMMDD[ hh:mm:ss[.mmm]]

    Examples:

    1. 2004-05-23T14:25:10

    2. 2004-05-23T14:25:10.487

    Datetime2

    YYYY-MM-DDThh:mm:ss[.nnnnnnn]

    YYYY-MM-DDThh:mm:ss[.nnnnnnn] Examples:

    1. 2004-05-23T14:25:10

    2. 2004-05-23T14:25:10.8849926

    You can convert them using 126 option

    --Datetime
    
    DECLARE @table Table(ExtendedDate DATETIME, BasicDate Datetime)
    
    DECLARE @ExtendedDate VARCHAR(30) = '2020-07-01T08:39:17' , @BasicDate VARCHAR(30) = '2009-01-23T10:53:21.000'
    
    INSERT INTO @table(ExtendedDate, BasicDate)
    SELECT convert(datetime,@ExtendedDate,126) ,convert(datetime,@BasicDate,126)
    
    SELECT * FROM @table
    go
    
    -- Datetime2
    
    DECLARE @table Table(ExtendedDate DATETIME2, BasicDate Datetime2)
    
    DECLARE @ExtendedDate VARCHAR(30) = '2000-01-14T13:42:00.0000000' , @BasicDate VARCHAR(30) = '2009-01-23T10:53:21.0000000'
    
    INSERT INTO @table(ExtendedDate, BasicDate)
    SELECT convert(datetime2,@ExtendedDate,126) ,convert(datetime2,@BasicDate,126)
    
    SELECT * FROM @table
    go
    

    Datetime

    +-------------------------+-------------------------+
    |      ExtendedDate       |        BasicDate        |
    +-------------------------+-------------------------+
    | 2020-07-01 08:39:17.000 | 2009-01-23 10:53:21.000 |
    +-------------------------+-------------------------+
    

    Datetime2

    
    +-----------------------------+-----------------------------+
    |        ExtendedDate         |          BasicDate          |
    +-----------------------------+-----------------------------+
    | 2000-01-14 13:42:00.0000000 | 2009-01-23 10:53:21.0000000 |
    +-----------------------------+-----------------------------+
    
    0 讨论(0)
提交回复
热议问题