-1

I have a column of data in the below string formats

enter image description here

When I copy and paste the above into an Excel sheet and change the column to a date format, all of the above will be converted into the same format correctly. Is there a similar feature in SQL server? I've tried using CAST and CONVERT, but have ran into conversion failed errors.

Follow-up

I found the values causing standard CAST and CONVERT functions to fail. There's some rows with decimal and int values as follows:

enter image description here

5
  • Either CAST or CONVERT should work provided the date is in a valid format. Which ones are failing?
    – JC Ford
    Commented Jun 7, 2013 at 19:59
  • So, is row 4 meant to be March, 8 or August, 3?
    – Lamak
    Commented Jun 7, 2013 at 20:00
  • 1
    Those should all work unless your language / regional settings are British / Canadian or your dateformat is not set correctly. You did enclose them in quotes before trying to convert them, right? Why don't you show exactly what you tried that failed, instead of a screen shot of the raw data? Commented Jun 7, 2013 at 20:00
  • @Lamak All dates are in American date format (m/d/yyyy) so row 4 is Aug 3 Commented Jun 7, 2013 at 20:02
  • It's possible that those unparseable values are dates expressed as numeric values, but without knowing how they were derived it's hard to use them reliably. Personally I'd consider just nulling those if I couldn't go back and fix them reliably. But those shouldn't make Cast() or Convert() fail anyway -- you just can't necessarily trust the result.
    – JC Ford
    Commented Jun 7, 2013 at 20:15

2 Answers 2

1

I ended up writing a CASE statement to account for the different format variations:

CASE
    WHEN [CommissionStart Date] LIKE '%NULL%'
    THEN '1/1/1900'
    WHEN ISDATE([CommissionStart Date]) = 1
    THEN CAST([CommissionStart Date] AS DATE)
    ELSE CONVERT(DATETIME, CAST([CommissionStart Date] AS DECIMAL(11, 5)))
END
0

Cast or Convert should work provided the dates are all in a valid format. If a value can't be parsed as a date, there's no magic fix. You have to find them and deal with them manually. The IsDate() function can help you find them.

select * from MyTable where IsDate(TextDateColumn) = 0

IsDate() will return 0 for numeric values like 4567 or 5456.6826 so if you have a lot of those then you can also check IsNumeric()

select * from MyTable where IsDate(TextDateColumn) = 0 and IsNumeric(TextDateColumn) = 0

That should allow you to identify the unparseable dates that are causing the conversion errors.

3
  • Excel is able to accurately convert them along with the rest of the formats so I was hoping SQL Server had a similar magic arrow. I'd still like to salvage the dates that are in decimal and integer formats Commented Jun 7, 2013 at 20:37
  • Are you certain Excel is properly converting them all? If it's a long list you might just be missing some that didn't convert. At any rate, Excel and SQL Server each uses its own code for data type conversion including parsing string dates and there's no guarantee that what works for one works for the other. Use the IsDate() and IsNumeric() methods I described and see if there are any really wacky dates in your data.
    – JC Ford
    Commented Jun 7, 2013 at 20:40
  • FWIW, select DateAdd( day, 41227, 0 ) returns 16 November 2012.
    – HABO
    Commented Jun 8, 2013 at 3:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.