How to convert yyyyMMddhh (2017092018) string to Date in SQL Server 2012?
Is it possible to do without using T-SQL to put the sentence into the INSERT clause?
INSERT INTO [dbo].[Table](SomeColumn)
VALUES (CONVERT(DATETIME, '2017092018', 'yyyyMMddhh'));
Example
Declare @S varchar(50)='2017092018'
Select convert(datetime,left(@S,8)) + convert(datetime,right(@S,2)+':00')
Returns
2017-09-20 18:00:00.000
If 2012+, I would suggest try_convert() just in case you have some unexpected values.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With