SQL Dates

I cannot convert nvarchar(25) to datetime.

I need to create two columns
datetime dd-mm-yyyy-mmmm - 15-Mar-2015-1512

second column named time
1512 pm.

i've tried CONVERT(nvarchar(25),CONVERT(DATE,column,106),103) AS AdmittedDatetime,

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Vitor MontalvãoMSSQL Senior EngineerCommented:
You need to have the column in this format: '20150317 15:12'
Vitor MontalvãoMSSQL Senior EngineerCommented:
So, if you can't change the data then you'll need to put the column values in the above format. For that you'll need to use some string functions:
CONVERT(nvarchar(25),CONVERT(DATE,LEFT(column,8) + ' ' + SUBSTRING(column,9,2) + ':' + RIGHT(column,2),106),103) AS AdmittedDatetime

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Phillip BurtonDirector, Practice Manager and Computing ConsultantCommented:
declare @column as nvarchar(25)
set @column = '201503171512'
select CONVERT(DATEtime,substring(@column,1,8)+N' ' +substring(@column,9,2) + N':' + substring(@column,11,2) + N':00',106) AS AdmittedDatetime
select convert(time,substring(@column,9,2) + N':' + substring(@column,11,2) + N':00',130) AS Admittedtime
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

aneilgAuthor Commented:
thanks guys.
Scott PletcherSenior DBACommented:
SELECT REPLACE(REPLACE(CONVERT(varchar(17), col1_datetime, 113), ' ', '-'), ':', '') AS datetime,
    RIGHT(col1, 4) + ' ' + CASE WHEN RIGHT(col1, 4) >= '12' THEN 'pm' else 'am' END + '.' AS time
    SELECT CAST('201503171512' AS nvarchar(25)) AS col1
) AS test_data
    SELECT CAST(STUFF(STUFF(col1, 9, 0, ' '), 12, 0, ':') AS datetime) AS col1_datetime
) AS assign_alias_names
aneilgAuthor Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.