Solved

Converting YYYYMMDD to MM/DD/YYYY

Posted on 2012-03-16
7
2,318 Views
Last Modified: 2012-03-16
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.
0
Comment
Question by:3GKID
7 Comments
 
LVL 51

Expert Comment

by:HainKurt
Comment Utility
select CONVERT(varchar(12),getdate(),101)

03/16/2012
0
 
LVL 51

Expert Comment

by:HainKurt
Comment Utility
select CONVERT(varchar(12),CONVERT(date, '20110125',100),101)

01/25/2011
0
 
LVL 39

Expert Comment

by:lcohan
Comment Utility
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.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 51

Expert Comment

by:HainKurt
Comment Utility
and make sure you have valid data in the column for all rows...
0
 
LVL 6

Accepted Solution

by:
effes earned 500 total points
Comment Utility
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

0
 
LVL 51

Expert Comment

by:HainKurt
Comment Utility
to find invalid data:

SET LANGUAGE us_english;
SET DATEFORMAT ymd;

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

Author Comment

by:3GKID
Comment Utility
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.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

771 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now