Solved

SQL Text to Date error

Posted on 2014-10-30
4
142 Views
Last Modified: 2014-10-31
I have a text column called readdate which is text(6).  It is actually a date that is formatted yymmdd.  I need to convert this column to date.  Here is what I have:

Use HDB
Go
SELECT CUSTOMER
      ,convert(date,readdate,12) as DATEOFREAD
FROM openquery(UDB,'select * FROM ATABLE');

While it is executing, DATEOFREAD comes up with the format of XXXX-XX-XX, but then errors with this:

Conversion failed when converting date and/or time from character string.

Any suggestions?
0
Comment
Question by:PeelSeel2
4 Comments
 
LVL 22

Expert Comment

by:Steve Wales
ID: 40413586
Check CAST and CONVERT doco: http://msdn.microsoft.com/en-us/library/ms187928.aspx

My guess is that the format you are using doesn't match the data string.

Format of 12 is yymmdd

Format of yyyymmdd would be 112.

YYYY-MM-DD in and of itself doesn't appear to be supported.

You could try format 120 - but it may require a time component, not sure it will accept the date without the time component (and not in a position to test that just at the moment).
0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40413592
If you are using SQL Server 2012 or 2014, use Try_Convert instead of Convert; this will return Null if it doesn't succeed, allowing you to diagnose the error further.
0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
ID: 40414685
varchar(6) can hold information that isn't a date., e.g. the 31st of September
| READDATE | READDATE_DATE |
|----------|---------------|
|   140901 |    2014-09-01 |
|   140930 |    2014-09-30 |
|   140931 |        (null) |
|   141001 |    2014-10-01 |
|   141023 |    2014-10-23 |
|   141031 |    2014-10-31 |

Open in new window


Try either of these:
select
        readdate
      , case when isdate(readdate) = 1 then convert(date,readdate,12) end AS readdate_date
from atable
;

Open in new window

select
        readdate
      , ca.readdate_date
from atable
cross apply (
            select case when isdate(readdate) = 1 then convert(date,readdate,12) end
            ) AS ca (readdate_date)
;

Open in new window

Using CROSS APPLY is particularly useful if you need to do some more calculations with the converted date in the same query as you can refer to it by the column alias.

setup details
**MS SQL Server 2012 Schema Setup**:

    
    
    CREATE TABLE ATABLE
    	([readdate] varchar(6))
    ;
    	
    INSERT INTO ATABLE
    	([readdate])
    VALUES
    	('140901'),
    	('140930'),
    	('140931'),
    	('141001'),
    	('141023'),
    	('141031')
    ;

**Query 1**:

    
    select
            readdate
          , case when isdate(readdate) = 1 then convert(date,readdate,12) end AS readdate_date
    from atable
    

**[Results][2]**:
    
    | READDATE | READDATE_DATE |
    |----------|---------------|
    |   140901 |    2014-09-01 |
    |   140930 |    2014-09-30 |
    |   140931 |        (null) |
    |   141001 |    2014-10-01 |
    |   141023 |    2014-10-23 |
    |   141031 |    2014-10-31 |


**Query 2**:

    select
            readdate
          , ca.*
    from atable
    cross apply (
      select case when isdate(readdate) = 1 then convert(date,readdate,12) end
      ) ca (readdate_date)
    

**[Results][3]**:
    
    | READDATE | READDATE_DATE |
    |----------|---------------|
    |   140901 |    2014-09-01 |
    |   140930 |    2014-09-30 |
    |   140931 |        (null) |
    |   141001 |    2014-10-01 |
    |   141023 |    2014-10-23 |
    |   141031 |    2014-10-31 |



  [1]: http://sqlfiddle.com/#!6/b3b45/3

  [2]: http://sqlfiddle.com/#!6/b3b45/3/0

  [3]: http://sqlfiddle.com/#!6/b3b45/3/1

Open in new window

0
 

Author Closing Comment

by:PeelSeel2
ID: 40415564
Worked great!!
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

757 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

18 Experts available now in Live!

Get 1:1 Help Now