I have a DateTime(null) column as StartDate in one of my tables schema.
Clients already have entered lots of data in their table based on that.
Now they want that column to be of type Character(10)
Is it possible to do that? Won't it damage the data they already have entered? What is a safe way to do this?
2 Answers
SELECT StartDate, CONVERT(CHAR, StartDate, 10) AS NEWOUTPUT
FROM yourtable
If the results of the NEWOUTPUT column are fine then go ahead with..
alter table yourtable alter column StartDate char(10) not null;
A direct change could lead to troubles. You would need to
- Create a new column
- Update your table so that the new column is filled with
YEAR(datecolname) + '-' + MONTH(datecolname) + '-' + DAY(datecolname). - Delete the old column
- Rename the new column to the old name
- Recreate indexes
One thing though is that the searches for specific dates WILL probably be slower in any case if you use char instead of datetime and specific sql statements like "between" won't work any longer.
If it is just as you mentioned in a comment because they dont want to type in a day, then it would probably be better to change how they have to type in the dates so that the day is defaultwise set to 1. But depends on the application in question.
Character(10)?DateTimeshould be suitable; if they want to enter data that does not represent the start date then a new, appropriately-named column should be set up.