Avatar of 3GKID
3GKID
 asked on

Converting YYYYMMDD to MM/DD/YYYY

Hi,

I have a SQL 2008 database with dates in a table stored in the format YYYYMMDD.

20041201
20040308
20040108
20040113

The above are examples of the dates in the tables.  The field is TRANDATE and the data is stored as decimal(9, 0).  I have written a SQL view to join a few tables so that we can use the view to pull data into microsoft excel with user friendly field names for analysis.  I am looking for a way to convert the dates so that they display as MM/DD/YYYY instead of the YYYYMMDD format.  I have searched ee and found the example:

Convert(VARCHAR(50),Convert(Datetime, '20110125'),101)

This successfully converts the string “20110125” to 01/25/2011.  I tried changing this to Convert(VARCHAR(50),Convert(Datetime, dbo.OESHDT.TRANDATE),101), however, I get the error message:

SQL Execution Error.

Executed SQL Statement: SELECT ……………………….
Error Source: .Net Sql Client Data Provider
Error Message: Arithmetic Overflow error converting expression to data type datetime.

I am extremely confused as to why I would get this error.  If I change the date string in the initial statement from '20110125' to any date like ‘20120316’, or with the function GetDate(), the date will be displayed correctly.  I would assume that putting the field name from the table would return the value and would work correctly.

Am I looking at this the wrong way?  Can someone please assist or show me another way?

Thanks in advance.
Microsoft SQL Server 2008

Avatar of undefined
Last Comment
3GKID

8/22/2022 - Mon
HainKurt

select CONVERT(varchar(12),getdate(),101)

03/16/2012
HainKurt

select CONVERT(varchar(12),CONVERT(date, '20110125',100),101)

01/25/2011
lcohan

What is the datatype for dbo.OESHDT.TRANDATE column and more important what junk data you may have in it? My guess is that this is the reason for your convert to fail. Strings or int in that column that would represent dates outside the 1900 to 2049 SQL/Windos platform knows.
Your help has saved me hundreds of hours of internet surfing.
fblack61
HainKurt

and make sure you have valid data in the column for all rows...
ASKER CERTIFIED SOLUTION
effes

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
HainKurt

to find invalid data:

SET LANGUAGE us_english;
SET DATEFORMAT ymd;

SELECT * from yourtable
WHERE ISDATE(TRANDATE) = -1
3GKID

ASKER
Hi.

Effes, you hit the nail on the head!  It took me quite a while to digest the statement you provided after trying it out.  Thanks a lot.

HainKurt, I tried the statement in the table and it produced no results.

Thanks a lot for all your help.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.