Andreas
Andreas

Reputation: 137

SQL copy data from one column to another

In one table called xt_products with several columns.

The ones I am interested are

products_ean (varchar 128) and products_model (varchar 255)

I would like to copy the values from products_model to products_ean IF the the entry in products_model has more than 10 characters.

e.g. products_model has the value of '1-2-3' then not copy if products_model has the value of '12345678910' then copy

Thanks Andreas

Upvotes: 0

Views: 46

Answers (1)

forpas
forpas

Reputation: 164064

Use the function CHAR_LENGTH() in the WHERE clause of the UPDATE statement to check the number of chars in products_model:

UPDATE xt_products
SET products_ean = products_model
WHERE CHAR_LENGTH(products_model) > 10

You may also want to add another condition to the WHERE clause:

AND CHAR_LENGTH(products_model) <= 128

because the max length allowed for products_ean is 128.

Upvotes: 1

Related Questions