Results 1 to 6 of 6

Thread: Type conversion

  1. #1
    Join Date
    Aug 2009
    Posts
    23

    Type conversion

    Hi

    What i am trying to do is to convert the string '20091212000019' to datetime format.
    So whenever i fire the query:

    SELECT CONVERT(DATETIME,'20091212000019')

    It gives me this error

    Msg 241, Level 16, State 1, Line 1
    Conversion failed when converting datetime from character string.


    Whereas this one works just perfect:

    SELECT CONVERT(DATETIME,'20091212')

    Is there any way to sort the former????

    Thanks

  2. #2
    Join Date
    Aug 2009
    Posts
    23
    I tried doing this.


    SELECT (SUBSTRING(P_DATE,1,4))+'-'+(SUBSTRING(P_DATE,5,2))+'-'+(SUBSTRING(P_DATE,7,2))+' '+(SUBSTRING(P_DATE,9,2))+':'+(SUBSTRING(P_DATE,11 ,2))+':'+(SUBSTRING(P_DATE,13,2)) AS P_DATE FROM tablename

    Then exported the data to desired table.

    and it worked just fine. but is there any direct way, some function may be, to do this????

  3. #3
    Join Date
    Nov 2002
    Location
    New Jersey, USA
    Posts
    3,932

  4. #4
    Join Date
    Aug 2009
    Posts
    23
    Dint work!! is there any direct function that converts char to datetime???

  5. #5
    Join Date
    Sep 2002
    Posts
    5,938
    Only convert and cast function, you may need format the string properly.

  6. #6
    Join Date
    Aug 2009
    Posts
    23
    Thanks a lot!!!

Posting Permissions

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