-3

I have 2 columns, an int and a tiny int, and I want to update another column, a varchar one, with the value column1 + '-' + column2. Something like this pseudo code:

UPDATE COLUMN3 varchar = CONCAT (COLUMN1 INT AND + COLUMN2 TINYINT);

How can I do it with real code?

4

Need to reference your table. CONCAT function will handle conversions between INT and TINY INT.

UPDATE YourTableName SET Column3 = CONCAT(Column1, '-', Column2)

NULL values are treated as empty strings ('') with CONCAT. If you want to filter out these records add IS NOT NULL as WHERE condition.

UPDATE YourTableName SET Column3 = CONCAT(Column1, '-', Column2) 
WHERE Column1 IS NOT NULL AND Column2 IS NOT NULL

You can also do explicit conversion to VARCHAR, but keep in mind that any NULL values concatenated this way (with +) will yield a NULL result.

UPDATE YourTableName SET 
    Column3 = CONVERT(VARCHAR(50), Column1) + '-' + CONVERT(VARCHAR(50), Column2)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.