We have a vendor database in which for some unknown reason all of the datetime values are actually nvarchar(14) in the format of "yyyymmddhhmiss". I have been hunting for a T-SQL function to do something like this....
FORMAT(TextDatetime, "yyyymmddhhmiss")
Unfortunately, it looks like FORMAT() only takes numeric or date and time datatypes.
I could create user-defined scalar function like this...
CREATE FUNCTION udf_Convert_TextDatetime
(
@TextDateTime nvarchar(14)
)
RETURNS datetime
AS
BEGIN
RETURN DATETIMEFROMPARTS (
SUBSTRING (@TextDateTime, 1, 4)
, SUBSTRING (@TextDateTime, 5, 2)
, SUBSTRING (@TextDateTime, 7, 2)
, SUBSTRING (@TextDateTime, 9, 2)
, SUBSTRING (@TextDateTime, 11, 2)
, SUBSTRING (@TextDateTime, 13, 2)
, '0'
)
END;
The volume of data in this database is small so I am not that concerned about performance.
Is this user-defined function the best solution here?