Search code examples
sql-serversql-server-2014

Inverse format function


Is there an inverse format function in SQL Server ?.

I have to import some datetime values as 'yyyyMMddTHHmmss' strings (example '20220406T123043' for the 06/04/2022 12:30:43), but I cannot convert easily to datetime those strings because I can't see such format (style) available for the CONVERT function.

So, is there a way to convert from string to datetime indicating a format mask ?.

It would be something like :

convert(datetime, '20220406T123043', 'yyyyMMddTHHmmss')
unformat('20220406T123043','yyyyMMddTHHmmss')
...

... or I have no other option than using datefromparts and extract each part using substrings ?.

Thank you.


Solution

  • Following Larnu advice, I have replaced 'T' with an space, and injected ":" separators with the STUFF function, so the strings can now be converted to datetime using the 112 style.

    convert(datetime,stuff(stuff(replace('20220406T123043','T',' '),12,0,':'),15,0,':'),112)
    

    Thank you.