Concatenate text to an existing SQL column

Asked

Viewed 9,062 times

2

I got the following SQL:

UPDATE tabela SET peso = peso "KG"

I need to add to the weight field, the tag KG, But if I do it the way I put it, it doesn’t work. How could I do it?

  • Depends on the DBMS, in Sqlserver accepts the + to concatenate the strings, already in Sqlite I think it is with |

  • 1

    @Andrébaill Which is the database manager?

2 answers

5


Use simple quotes to concatenate the text 'KG'

SQL SERVER:

Beyond the function CONCAT() you can use the + to concatenate the current value of the column with KG:

UPDATE tabela SET peso = peso + 'KG'

Sqlite:

Use the || for concatenate, the CONCAT here doesn’t work.

UPDATE Tabela Set Coluna = Coluna || 'KG'

Mysql:

Utilize CONCAT():

UPDATE tabela SET peso = CONCAT(peso, 'KG');

Note: It is also possible to do with || as in the Sqlite but for this you must configure the Mysql to accept, setando sql_mode for PIPES_AS_CONCAT.

Postgresql:

Beyond the function CONCAT() you can use the || to concatenate.

UPDATE Tabela Set Coluna = Coluna || 'KG'

Oracle:

Beyond the function CONCAT() you can use the || to concatenate.

UPDATE Tabela Set Coluna = Coluna || 'KG'

All commands above have been tested on Sqlfiddle.

3

You have to concatenate what is stored most to tag KG:

UPDATE tabela SET peso = CONCAT(peso, " KG");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.