Solved

SQL Text to Date error

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
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.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

867 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