I have data 20160526094432, and I want to convert into datetime in SQLServer The result will be 2016-05-26 09:44:32
Is there simple way to do that ?
Thanks
I have data 20160526094432, and I want to convert into datetime in SQLServer The result will be 2016-05-26 09:44:32
Is there simple way to do that ?
Thanks
If you use MS SQL Server 2012 or newer then you can enjoy format
function.
select cast(format(20160526094432,'####-##-## ##:##:##') as datetime) [date-time]
If your long number is a string then you have to convert it.
declare @d varchar(20)='20160526094432'
select cast(format(cast(@d as bigint),'####-##-## ##:##:##') as datetime) [date-time]
Hmmm. I don't think there is a really clean way, but something like this should work:
select (convert(datetime, left(col, 8) as datetime) +
convert(datetime, convert(time,
stuff(stuff(right(col, 6), 5, 0, ':'), 3, 0, ':')
)
)
)
Maybe you can try in this way, for example Date is the column of your table, 103 is the format of Date you want to convert, google for more details.
CONVERT(datetime, Date, 103)