Solved

Need SQL help to convert nvarchar(255) to datetime.

Posted on 2013-05-15
3
607 Views
Last Modified: 2013-05-16
I hope this posting finds everyone doing well.

I received a SQL table with a nvarchar(255) column with dates as strings, below are a few examples.

Sun Aug 19 19:01:17 +0000 2012
Thu Oct 13 21:35:22 +0000 2011
Fri Apr 13 03:24:38 +0000 2012
Mon Nov 28 03:05:30 +0000 2011
Wed May 27 01:30:07 +0000 2009
Fri Jul 22 00:27:20 +0000 2011

I wanted to see if anyone knew how to convert these to datetime.  I have been trying the convert method with no luck.  I figure I would ask on here before I started writing my own SQL function to do this.

Many thanks!
0
Comment
Question by:rye004
3 Comments
 
LVL 75

Assisted Solution

by:Aneesh Retnakaran
Aneesh Retnakaran earned 125 total points
ID: 39169671
declare @dt varchar(255) = 'Sun Aug 19 19:01:17 +0000 2012'
select cast (RIGHT(@dt,4)+ ' ' +substring(@dt,5, len(@dt)-14) as datetime )
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 125 total points
ID: 39172083
If you don't need the offset -- for example, it's always +0000 anyway, you can do this:

    CAST(  RIGHT(string, 4) + SUBSTRING(string, 4, 17)  ) AS datetime
0
 

Author Closing Comment

by:rye004
ID: 39173031
Thank you so much for your fast response.  These both worked perfectly.
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

680 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