Solved

SQL Text to Date error

Posted on 2014-10-30
4
154 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

770 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