i have table:
id timestamp
1 31.10.2020 16:32:11
2 09.09.2020 09:15:49
3 22.04.2020 02:48:15
Table have huge amount these data. Column timestamp is data type "nvarchar". And i need to sort datas by date or use in clauses WHERE for constraint by date, so i need to convert "timestamp" to datetime. But I can't. I tried convert and cast but still failed.
Any advice?
Thx.
Fix your design, and change the column to a date and time data type. Considering your data is accurate to 1 second, a
datetime2(0)
seems appropriate here. First we need to change the "format" of thenvarchar
value to an ISO format. We're going to use the ISO8601 format (yyyy-mm-ddThh:mi:ss.mmm) as it's unambiguous:Now you can
ALTER
the table and change the data type to adatetime2(0)
:I also recommend using a different name than
timestamp
for your column.timestamp
is a deprecated synonym forrowversion
, so it's use can make it quite confusing; especially asrowversion
is not a date and time value but abinary
value and cannot be converted to a date and time data type.