Search code examples
mysqlcharvarcharalter-tablealter

What does MySQL converting varchar to char?


When we add a new table, we made a mistake : a field was a varchar(255), but we have to write join queries. So we alter table to make a char(6).

My question is : what does MySQL in this case ? It trims right ?


Solution

  • Rather than worry about what MySQL does or doesn't do why not just convert the data yourself before the change.

    e.g.

    UPDATE YourTable
    SET YourField = LEFT(YourField,6);
    
    ALTER TABLE YourTable MODIFY COLUMN YourField Char(6)
    

    You should note that if your column data is too long it won't let you do the alter assuming enable strict SQL mode see Shef's complete answer