Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2005
    Location
    North Carolina
    Posts
    11

    Question Unanswered: Converting text to date

    I have a field that stores a date as text (121205). I need to convert this field to a date, but since it is text, I cannot figure out how to do it. Any ideas??? Thanks!
    Amber Hardee, DBA

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    which database? informix? sybase? db2? access? firebird? oracle? mysql? sql server? postgresql?
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Apr 2005
    Location
    North Carolina
    Posts
    11
    SQL Server
    Amber Hardee, DBA

  4. #4
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    Moving this thread to Microsoft SQL Server forum, but I'd use:
    Code:
    SELECT t, Convert(DATETIME, Stuff(Stuff(t, 5, 0, '/'), 3, 0, '/'))
       FROM (SELECT '121205' AS t UNION SELECT '111105' UNION SELECT '101005') AS z
    -PatP

  5. #5
    Join Date
    Nov 2002
    Location
    Jersey
    Posts
    10,322
    As you can tell, Pat likes to STUFF things....

    Code:
    CREATE PROC mySproc99 @x text
    AS
    SELECT CONVERT(datetime, CONVERT(varchar(25),@x))
    GO
    
    EXEC mySproc99 '121205' 
    GO
    
    DROP PROC mySproc99
    GO
    So who's to say it's not 2012?
    Brett
    8-)

    It's a Great Day for America everybody!

    dbforums Yak CorralRadio 'Rita
    dbForums Member List
    I'm Good Once as I ever was

    The physical order of data in a database has no meaning.

  6. #6
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    Brett is correct, you don't need the slashes in the US, and maybe not in the UK either... They assume a string of digits are MMDDYY. As far as I know, the slashes work in any locale.

    -PatP

Posting Permissions

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