Posted on 2012-03-16
Last Modified: 2012-03-16

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


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.
Question by:3GKID
LVL 51

Expert Comment

by:Huseyin KAHRAMAN
ID: 37730366
select CONVERT(varchar(12),getdate(),101)

LVL 51

Expert Comment

by:Huseyin KAHRAMAN
ID: 37730375
select CONVERT(varchar(12),CONVERT(date, '20110125',100),101)

LVL 40

Expert Comment

ID: 37730380
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.
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

LVL 51

Expert Comment

by:Huseyin KAHRAMAN
ID: 37730384
and make sure you have valid data in the column for all rows...

Accepted Solution

effes earned 500 total points
ID: 37730392
The problem is with this part of your statement:
Convert(Datetime, dbo.OESHDT.TRANDATE)

Open in new window

This tries to convert the number 20110125 and not the string '20110125' to datetime. That is not the same. You have to convert the value in TRANDATE to a string and then convert the result of that to datetime:
Convert(VARCHAR(50),Convert(Datetime, Convert(nvarchar, dbo.OESHDT.TRANDATE)),101)

Open in new window

LVL 51

Expert Comment

by:Huseyin KAHRAMAN
ID: 37730398
to find invalid data:

SET LANGUAGE us_english;

SELECT * from yourtable

Author Comment

ID: 37730629

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.

Featured Post

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Job - date manual 1 41
SQL Server 2008 R2, need a pivot/cross tab query... 4 48
Reformat SQL - so SSRS can read the columns 25 30
Linked Server - SP with Param to VIew 7 19
Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In an interesting question ( here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question