'Trying to convert datetime format in SQL
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
Solution 1:[1]
using
to_timestamp('220215010100','YYMMDDHHMISS') as "Date Time"
to convert it into the date time format requested.
Solution 2:[2]
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)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Philip |
| Solution 2 | Arash Ghazi |
