Hi @Debilon ,
If you convert the data type of a column in a SELECT statement, in fact, you can only temporarily change the data type of the column during the SELECT process, and you cannot change the actual data type of the column in the table.
If you want to change the actual data type of the column, you need to change the column definition.(ALTER TABLE (Transact-SQL))
Inserting data is normal:
CREATE TABLE [2019]([APPT_START_DATE] nvarchar(50));
INSERT INTO [2019] VALUES
('2015-04-27 13:30:00.0000000'),
('2015-04-27 14:30:00.0000000'),
('2015-04-30 14:00:00.0000000'),
('2015-04-30 14:00:00.0000000'),
('2016-04-12 11:00:00.0000000');
INSERT INTO [2019]
SELECT CAST(APPT_START_DATE AS datetime2) as NewDate
FROM [2019];
SELECT * FROM [2019];
Output:
Splitting Date into 2 Columns (Date + Time):
ALTER TABLE [2019] ADD App_Start_Time time NULL;
UPDATE t
SET APPT_START_DATE=convert(date, APPT_START_DATE),
App_Start_Time=convert(varchar(8), convert(time, APPT_START_DATE))
FROM [2019] t;
SELECT *
FROM [2019];
DROP TABLE [2019];
Output:
Regards,
Echo
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".