Hello, I am using nvarchar
column to store datetime
as string, but I would like to convert this column from nvarchar into datetime in sql server. How can I do it.
Current time format is '2019-01-29 14:55:50'
Suppose you have column name 'dattime' in 'newTable', then you can try below code to alter column table and update all string nvarchar date into datetime
alter table newTable alter column dattime datetime null;
if this doesn't work, you can also try this
UPDATE newTable
SET dattime = CONVERT(NVARCHAR(50),CONVERT(SMALLDATETIME, dattime,105))
ALTER TABLE newTable
ALTER COLUMN dattime SMALLDATETIME
// where dattime is column name and newTable is table name
Thanks
It depends on your current column datetime saved values also
DECLARE @ndate nvarchar(32) = N'15/03/2010'
SELECT CONVERT(datetime, @ndate, 103)
/**** 103 is datetime format for above example ***/
Hope it helps.
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly