Cast/Convert nvarchar to datetime(2)

287 Views Asked by At

Since I described it poorly last time, again

i have table:

id   time_stamp
1    12.01.20 15:34:34,000000000 EUROPE/PRAGUE
2    10.01.20 10:15:15,000000000 EUROPE/PRAGUE
3    09.01.20 05:55:42,000000000 EUROPE/PRAGUE

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.

From @Larnu i got advice, but didnt work, because I'm stupid and inaccurately described the problem.

UPDATE dbo.YourTable
SET [timestamp] = CONVERT(nvarchar(20),TRY_CONVERT(datetime2(0), [timestamp], 104), 126);

Now you can ALTER the table and change the data type to a datetime2(0):

ALTER TABLE dbo.YourTable ALTER COLUMN [timestamp] datetime2(0) NULL;

any advice on how to change with respect to the time_stamp column.

Thx.

0

There are 0 best solutions below