Home > Software design >  Trying to convert datetime format in SQL
Trying to convert datetime format in SQL

Time:03-17

This is the date time data I've obtain from a JSON file:

220215010100

I wish to convert it into a date time format using SQL. The date time data provided is based on this format:

YYMMDDHHRRSS

Is there a way to convert it into a datetime format recognized by SQL? For example:

2022-02-15 01:01:00

CodePudding user response:

I think it will work

     declare @string nvarchar(12)='220215010100' 
     declare @stringfomated nvarchar(15)=( select '20' SUBSTRING(@string,1,2)  '/' 
     SUBSTRING(@string,3,2)  '/' 
     SUBSTRING(@string,5,2)  ' ' 
     SUBSTRING(@string,7,2)  ':' 
     SUBSTRING(@string,9,2)  ':' 
     SUBSTRING(@string,11,2) )
     select CONVERT(datetime,@stringfomated)

CodePudding user response:

using

to_timestamp('220215010100','YYMMDDHHMISS') as "Date Time"

to convert it into the date time format requested.

  • Related