Not able to convert String to Date Time

  • Hi,

    I am not able to convert this simple number to DateTime,could any one help me.

    select convert(datetime, '20140626190825',108)

    select cast('20140626190825' as datetime)

  • Use the STUFF function to make your string look like this: '20140626 19:08:25'.

    John

  • Hi John,

    I am glad for your reply,  I need to use this conversion of time and their time difference as well, I believe STUFF function might not able applicable, please correct me if I am not wrong.

    Thank you

    Raj

  • Raj

    You're wrong, I think.  If you have two dates that you need to compare, you need to make sure they can both be converted to datetime.  The lesson here, of course, is always to store dates as datetime (or similar) and not varchar or int.

    If you describe your requirement in a bit more detail, and provide a CREATE TABLE and INSERT statement by way of sample data, as well as the expected results, I'll show you how to do it.

    John

  • Sangeeth878787 - Wednesday, February 1, 2017 5:04 AM

    Hi John,

    I am glad for your reply,  I need to use this conversion of time and their time difference as well, I believe STUFF function might not able applicable, please correct me if I am not wrong.

    Thank you

    Raj

    Quick STUFF example
    😎

    SELECT CONVERT(DATETIME,STUFF(STUFF(STUFF('20140626190825',9,0,' '),12,0,':'),15,0,':'),112) AS DTIME

    Output
    DTime
    -----------------------
    2014-06-26 19:08:25.000

Viewing 5 posts - 1 through 4 (of 4 total)

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