1

I was wondering if I could alter an SQL table column from decimal to varchar.

My table name is called "products", and I want to change the column "qty" to varchar.

What I want the final result to be:

  • The column qty in table products to be varchar instead of decimal
Dharman
  • 30,962
  • 25
  • 85
  • 135
Manu G
  • 158
  • 5
  • 13
  • 1
    Tag your question with the database you are using. However, why would you want `qty` to be a string? A number seems very reasonable. – Gordon Linoff Dec 02 '20 at 16:43
  • 1
    Do you really want to hold a quantity as a varchar? Like qty = 'TEN' ?? I doubt it will be a good idea in the long run – RiggsFolly Dec 02 '20 at 16:45
  • 1
    This sounds like bad design to me. If you need an extra information it will be better to create a new column to hold this. For example, if you need to store "2kg", store 2 in qty and kg in this new varchar column. – Felippe Duarte Dec 02 '20 at 17:30
  • Can you explain why you want to change the data type? That could help us answer the question / give you a better design choice. – BrianAtkins Dec 02 '20 at 17:44
  • Ok! Thank you all for the ideas. I should try the one you told @FelippeDuarte – Manu G Dec 02 '20 at 19:49
  • And, the reason why I wanted to change it is because I wanted people to enter a label after the number (for example: 1 box, or 1 container), not just the number – Manu G Dec 02 '20 at 19:50

1 Answers1

0

Based on answer from How to change MySQL column definition?

Follow syntax ALTER TABLE table_name MODIFY COLUMN <column_name> <type>;

Therefore in your case it will be:

ALTER TABLE products MODIFY COLUMN qty VARCHAR(desired_characters);

desired_characters should define how many characters your varchar should contain.

However I recommend use int for quantity type fields. Additionally you can mark it as unsigned since quantity cannot be negative.

ALTER TABLE products MODIFY COLUMN qty int unsigned;
Chrzanek
  • 197
  • 3
  • 15