?
Solved

MSSQL Convert Char To Date

Posted on 2016-10-25
4
Medium Priority
?
93 Views
Last Modified: 2016-10-28
I have a table where the DateTime is stored as Char(14)
Example:  11272006124606    mmddyyyyhhmmss
Question:
How can I convert  11272006124606 to 2006-11-27
0
Comment
Question by:Phil Chapman
  • 2
4 Comments
 
LVL 37

Assisted Solution

by:Pawan Kumar
Pawan Kumar earned 1000 total points
ID: 41858631
Try..

--

DECLARE @ AS CHAR(14) = '11272006124606'

SELECT CAST(RIGHT(SUBSTRING(@,1,8),4) + '-' + LEFT(SUBSTRING(@,1,8),2) 
+ '-' + SUBSTRING(SUBSTRING(@,1,8),3,2) AS DATE)

--

Open in new window


O/p

2006-11-27
0
 
LVL 37

Expert Comment

by:Pawan Kumar
ID: 41858639
For SQL 2012..

DECLARE @ AS CHAR(14) = '11272006124606'

SELECT CAST( CONCAT(  RIGHT(SUBSTRING(@,1,8),4) , '-' , LEFT(SUBSTRING(@,1,8),2) 
, '-' , SUBSTRING(SUBSTRING(@,1,8),3,2)) AS DATE)

Open in new window



O/p

2006-11-27
0
 
LVL 71

Accepted Solution

by:
Qlemo earned 1000 total points
ID: 41858681
You can also use (in 2012+):
declare @dt as char(14) = '11272006124606'
select convert(date, stuff( stuff(left(@dt,8),5, 0, '/') , 3, 0, '/'), 101)

Open in new window

0
 
LVL 2

Author Closing Comment

by:Phil Chapman
ID: 41864576
Thanks for the help
0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

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

In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
What we learned in Webroot's webinar on multi-vector protection.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

864 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