my date column value looks like:
2023-04-23 00:00:00
2023-04-23 00:50:00
''
NULL
I want to convert it to
20230423000000
20230423005000
''
NULL
When I used
Select FORMAT (DAT,'yyyyMMddHHmmss' from table give error as
Argument datatype nvarchar is invalid for argument 1 of format function.
3条答案
按热度按时间kadbb4591#
You couldn't use FORMAT because datatype filed date is nvarchar
first you must cast with datetime and you must check empty and null field dates
You can insert Base data with the following statements:
8iwquhpp2#
From the error message, your column is of a string-like datatype, not datetime-like. If so, you probably want to use string functions:
Or using
replace()
multiple times:kdfy810k3#
The integer value returned in this conversion refers to the number of days since Jan 01, 1900. In your second query, the date you have entered is stored as a varchar, thus the conversion error you're seeing. You can cast or convert this to a datetime and the query will succeed.
Ex: