Solved

MSSQL Convert Char To Date

Posted on 2016-10-25
4
43 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:PhilChapmanJr
  • 2
4 Comments
 
LVL 17

Assisted Solution

by:Pawan Kumar Khowal
Pawan Kumar Khowal earned 250 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 17

Expert Comment

by:Pawan Kumar Khowal
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 68

Accepted Solution

by:
Qlemo earned 250 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:PhilChapmanJr
ID: 41864576
Thanks for the help
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
sql server dynamic memory? 3 75
SAP and ODBC links 2 91
Compare and update data between two databases 6 42
oracle query help 29 64
APEX (Application Express) is used to develop a web application from Oracle. SQL Workshop is one of the tools that comes with Oracle APEX to query or modify the database objects or to make any changes to the structure.
SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

757 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

21 Experts available now in Live!

Get 1:1 Help Now