Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2004
    Posts
    162

    Unanswered: cast/convert nvarchar to datetime

    hi

    How to convert '20100414103010' to datetime? format is yyyymmddhhmmss

    The column is nvarchar(14)

    I have tried
    cast (c1 as datetime)
    convert (datetime, c1)

    but all i get is 'Conversion failed when converting datetime from character string.'


    thx for any help

  2. #2
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    The first question (of course) is why on earth are you storing dates as a unicode string?

    You need to change your string to a format that can be converted.
    This works:
    Code:
    SELECT  CAST('20100414 10:30:10' AS DATETIME)
    Have a look at the STUFF() function for T-SQL to get your string in to that format.

  3. #3
    Join Date
    Mar 2004
    Posts
    162
    thx for fast replay, this works just fine
    cast (stuff(stuff(stuff(c1,13,0,':'),11,0,':'),9,0,' ') as datetime)

    thx
    Last edited by mrpcguy; 04-15-10 at 07:42.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •