T-SQL - changing datetime to date data type?

2019-04-04 08:29发布

问题:

I have a column BirthDate in a table that is using the datetime data type. Currently, the values resemble the format 1987-12-30 00:00:00.000.

I would like to update all the rows of this table, changing them to the following format with the date data type: 1987-12-30

I can run a SELECT...

SELECT CONVERT(date, BirthDate) FROM CUSTOMERLIST

But this is only affecting the display. I would like for it to update the entire column and also change the data type of that attribute as well. Any assistance would be greatly appreciated!

回答1:

You will need to ALTER the table:

ALTER TABLE CUSTOMERLIST ALTER COLUMN BirthDate date not null

See Sql Fiddle with demo