Solved

SQL Text to Date error

Posted on 2014-10-30
4
171 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 49

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

Percona Monitoring and Management and Grafana

Proactive monitoring is vital to a highly-available environment. We have a quick start guide on Experts Exchange for Grafana users.

Question has a verified solution.

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

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.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

628 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