Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

MSSQL Convert Char To Date

Posted on 2016-10-25
4
Medium Priority
?
89 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
[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
  • 2
4 Comments
 
LVL 32

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 32

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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
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…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

650 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