Jump to content

Converting Data from varchar

- - - - -

  • Please log in to reply
6 replies to this topic

#1
DoktorD1313

DoktorD1313

    Newbie

  • Members
  • PipPip
  • 24 posts
In SQL, I need to convert a column of characters (actually dates represented as MM/DD/YY) that the Import/Export Wizard will not recognize as dates.

Since all records now and from here on out will be post 2000, I assumed I would be able to insert a "20" into the current record and then be able to convert it to a date.

For example:

"03/15/11" could be split into "03/15/" + "20" + "11" and recombined to get 03/15/2011 (which is a readable date).

My attempted code was:

INSERT INTO TRIAL (SOLD_DATE)

SELECT CONVERT(VARCHAR(10),(SUBSTRING(FIRST_SOLD,1,6)+'20'+SUBSTRING(FIRST_SOLD,7,2)),1)

FROM LABELS

However, it returns this error:

Msg 241, Level 16, State 1, Line 4
Conversion failed when converting date and/or time from character string.


Any suggestions?

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Which database type is this? MySQL is quite different from Oracle, for example.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
DoktorD1313

DoktorD1313

    Newbie

  • Members
  • PipPip
  • 24 posts
I didn't even give that a thought..

I'm using MS SQL Server

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
What does this return?
SELECT CONVERT(VARCHAR(10),(SUBSTRING(FIRST_SOLD,1,6)+'20'+SUBSTRING(FIRST_SOLD,7,2)),1) FROM LABELS

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
DoktorD1313

DoktorD1313

    Newbie

  • Members
  • PipPip
  • 24 posts
When I use that code, I get a column of the appended dates (07/21/2011). So I'm not sure why I'm having trouble to get it to convert to dates.

#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
What's the regional date/time setting on the database server? I've found that 2011-07-21 is the only safe date format you can use.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#7
DoktorD1313

DoktorD1313

    Newbie

  • Members
  • PipPip
  • 24 posts
Well, for whatever reason all I needed to do was restart the application and repasting the Query worked just fine.

Thanks for the help, though!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users