1

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?

8
  • What is Character(10)? Commented Nov 16, 2015 at 13:39
  • 4
    Why go to char(10) for datetime values?!? Commented Nov 16, 2015 at 13:42
  • 3
    Why do the clients want to change the datatype? If they want to enter data that represents the start date then the DateTime should 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. Commented Nov 16, 2015 at 13:44
  • 2
    Just say no. Since when is the client responsible for database design? if the values are datetime, or date, storing them in char can only do harm. you might want to change the database from datetime to date, but do not agree to change it to char under any circumstances. Commented Nov 16, 2015 at 13:44
  • 6
    @Bohn . . . Then default the day to 01 and only show it to them using the year and month. Commented Nov 16, 2015 at 13:46

2 Answers 2

2
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;
Sign up to request clarification or add additional context in comments.

2 Comments

Now how do I do the actual conversion? Thanks.
@SQLPolice , thanks! i appreciate these however anymore and they will get revesed by the up vote script i would imagine as its too many upvotes from the same user to the same user per day
1

A direct change could lead to troubles. You would need to

  1. Create a new column
  2. Update your table so that the new column is filled with YEAR(datecolname) + '-' + MONTH(datecolname) + '-' + DAY(datecolname).
  3. Delete the old column
  4. Rename the new column to the old name
  5. 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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.