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
Medium Priority
89 Views
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
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
• 2

LVL 32

Assisted Solution

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)

--
``````

O/p

2006-11-27
0

LVL 32

Expert Comment

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)
``````

O/p

2006-11-27
0

LVL 71

Accepted Solution

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)
``````
0

LVL 2

Author Closing Comment

ID: 41864576
Thanks for the help
0

## Featured Post

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.
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
Course of the Month12 days, 8 hours left to enroll