Link to home
Start Free TrialLog in
Avatar of Phil Chapman
Phil ChapmanFlag for United States of America

asked on

MSSQL Convert Char To Date

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
SOLUTION
Avatar of Pawan Kumar
Pawan Kumar
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Phil Chapman

ASKER

Thanks for the help