Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've a table in MySQL database which contains a field of type string (varchar(20)). Its content is of type time, like 23:30, 14:00, etc. Although its length is 20, only 5 characters are required. Anyway, I prefer to change it to a field of type int with content 0 to 47 to indicate 00:00 to 23:30. But I need to keep contents of customers' databases. I may simply run:
SQL
alter table Schedules modify Start tinyint(1);

But this resets contents of the field in rows. I need a MySQL script to do it without clearing the contents. Maybe I've to add an intermediate column.

What I have tried:

Don't know how to keep the field contents in rows. This is a question I've no clue about.
Posted
Updated 29-Jun-17 8:05am
Comments
Mehdi Gholam 29-Jun-17 5:20am    
First the data types should be compatible, otherwise you should convert them first then change the data type.
ilostmyid2 29-Jun-17 8:39am    
how?
Andy Lanng 29-Jun-17 10:18am    
Update the column contents:
Update "table"
Set "column" = "0" where "column"="00:00"
Best would be to use case tho
ilostmyid2 1-Jul-17 6:43am    
thank u

1 solution

The values in time format don't have any numerical representation. Thus, if you convert the column's type it will wipe clean whatever it can't convert. By comparison, a table of char types, that are all, for example, just plain numbers, will convert when the column's changed. It's a matter of 'can it be represented in the new column type?'.

Best solution is to create an new column of type int, update your table so you convert your times to int format, and then drop the time column.

Alternative: use a computed column value in the new column and handle the work in a function so that you can enter ab:cd format and it will be converted in the alternate column automatically.
 
Share this answer
 
Comments
ilostmyid2 1-Jul-17 6:43am    
thank u. it's better that i read and learn more about sql script language.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900