Solved

SQL Text to Date error

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

756 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