Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Format specific datetime string to datetime datatype in SQL Server

I have a string which stands for a specific format of datetime.
Format: yyyymmddhhmmss
For example: 20220504111621

I want it to convert to a SQL Server datetime datatype: yyyy-mm-dd hh:mm:ss (Format 120)

Is there a way to do that?
I tried something like:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

select [SYNC_TIME] 
replace(convert(varchar, [SYNC_TIME],101),'/','') + replace(convert(varchar, [SYNC_TIME],108),':','')

FROM [DB].[dbo].[DATABASENAME]

>Solution :

Lots of ways, you could use stuff to format the string and convert:

declare @string varchar(15)='20220504111621'
select Convert(datetime,Stuff(Stuff(Stuff(@string,9,0,' '),12,0,':'),15,0,':'),112)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading