XML DATETIME CONVERSION FAILING TO CONVERT TO SQL DATETIME

  • I have an XML colmn which looks like:

    <abc>

    <abcdate>2007-01-31T13:47:27.25-05:00</abcdate>

    </abc>

    The following query :

    SELECT xmlColumn.value('(/abc/abcDate)[1]', 'nvarchar(30)')  FROM abcTABLE

    Returns 2007-01-31T13:47:27.25-05:00

    ---------

    SELECT CAST(xmlColumn.value('(/abc/abcDate)[1]', 'nvarchar(30)') AS DATETIME)  FROM abcTABLE

    Returns

    Msg 241, Level 16, State 1, Line 1

    Conversion failed when converting datetime from character string.

    -----------------

    SELECT CAST(xmlColumn.value('(/abc/abcDate)[1]', 'nvarchar(19)') AS DATETIME)  FROM abcTABLE

    Returns 2007-01-31 13:47:27.000

    because length of 19 trims the milliseconds

    ------------------

    Is it possible to convert this type of XML data type and still acheive accuracy to the milliseconds?

    Thanks

    Gary

     

  • You will have to remove the UTC offset ('-05:00') before converting to datetime.

    select DATE = convert(datetime,substring('2007-01-31T13:47:27.25-05:00',1,22))
    DATE                                                   
    ------------------------------------------------------ 
    2007-01-31 13:47:27.250
    (1 row(s) affected)
    

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply