How to convert char to time

  • Hey,

    How do I convert (n)(var)char to time? I tried several ways including a simple:

    SELECT CONVERT(time,'143825',108)

    But it always gives me this error:

    Conversion failed when converting date and/or time from character string.

  • How about '14:38:25'?

  • Gwandi (5/6/2011)


    Hey,

    How do I convert (n)(var)char to time? I tried several ways including a simple:

    SELECT CONVERT(time,'143825',108)

    But it always gives me this error:

    Conversion failed when converting date and/or time from character string.

    Does it work with a valid time

    ie

    SELECT CONVERT(time,'12:15',108)

    ?

    Ian

  • Yeah it does. But the problem is that I am getting the values from an imported XML file and I can´t add the ´:´.

  • Well it's called string manipulation. Why are you forbidden to use those?

    DECLARE @a CHAR(6)

    SET @a = '123456'

    SELECT STUFF(STUFF(@a, 3, 0, ':'), 6, 0, ':')

    In the select of your xml query just substitute @a with the column and you'll be good to go.

  • That should work but the problem is I'm not sure how to add that to the select query (I'm still kinda new to all this). My query currently looks like this:

    DECLARE @XML AS XML, @hDoc AS INT

    SELECT @XML = Data FROM dbo.TempTable

    EXEC sp_xml_preparedocument @hDoc OUTPUT, @XML

    SELECT *

    FROM OPENXML(@hDoc, '/FlexQueryResponse/FlexStatements/FlexStatement/Trades/Trade')

    WITH (accountId NVARCHAR(50) '@accountId', etc, etc)

    EXEC sp_xml_removedocument @hDoc

  • replace select * with the list of columns, then use the stuff function on that column rather than the variable I use in my demo.

  • Okay that worked. Thanks a lot 🙂

  • The code below works for what you are asking, if you are converting an entire column that has data like '143825' just replace the '143825' with the column name and it'll do the same

    SELECT ' ' + substring('143825', 1, 2)

    + ':' + substring('143825', 3, 2)

    + ':' + substring('143825', 5, 2)

    Output: 14:38:25

    This is the code I used to test it on a table that had all numbers in the column:

    SELECT ' ' + substring([Date], 1, 2)

    + ':' + substring([Date], 3, 2)

    + ':' + substring([Date], 5, 2) from DateTest

    Output: 14:38:25

    This link was super helpful: http://www.sqlusa.com/bestpractices/datetimeconversion/

  • Hello friends,

    how can I get time difference of the following record please help,

    STARTTIME ENDTIME

    3:30 PM 4:30PM

    7:30 PM 8:30PM

    I have tried it by below query,

    SELECT CONVERT(TIME,STARTTIME,108) - CONVERT(TIME,ENDTIME,108) FROM BATCH_MASTER

    but it gives following error message

    Operand data type time is invalid for subtract operator.

  • pspkshah (5/29/2015)


    Hello friends,

    how can I get time difference of the following record please help,

    STARTTIME ENDTIME

    3:30 PM 4:30PM

    7:30 PM 8:30PM

    I have tried it by below query,

    SELECT CONVERT(TIME,STARTTIME,108) - CONVERT(TIME,ENDTIME,108) FROM BATCH_MASTER

    but it gives following error message

    Operand data type time is invalid for subtract operator.

    You should start your own thread instead of hijacking another thread that is 4 years old.

    When you want to do math on date or datetime you should use DATEADD. You would do yourself a huge favor by using the time datatype instead of storing time information in a varchar column.

    --EDIT--

    I see that you did in fact start your own thread for this. http://www.sqlservercentral.com/Forums/Topic1689898-391-1.aspx

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

Viewing 11 posts - 1 through 10 (of 10 total)

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