Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding characters in values in an existing column

I need to add characters to values in a column. For example:

Price column
22.99
12.95
10.35

For my query output, I need this column to show as

Price column
Price is 22.99
Price is 12.95
Price is 10.35

I already used this to convert these values to char as I think I have to do this to insert characters in this column...

CONVERT (char(12), price) AS price

I just can't figure out how I can write the command to add "Price is" in every row :(

Please help, thank you!

like image 970
user2104163 Avatar asked Jan 14 '23 02:01

user2104163


1 Answers

You cannot permanently to do this if the data type of the column is INT or number but you can do it on SELECT statement,

In MySQL:

SELECT  CONCAT('Price is ', priceColumn) Price_Column
FROM    tableName

In TSQL

SELECT  'Price is ' + CAST(priceColumn AS VARCHAR(15)) Price_Column
FROM    tableName

But if you change the data type of that column into string (VARCHAR()) then you can simple execute UPDATE command

In MySQL,

UPDATE  tableName
SET     priceColumn = CONCAT('Price is ', priceColumn)

in TSQL

UPDATE  tableName
SET     priceColumn = 'Price is ' + CAST(priceColumn AS VARCHAR(15))
like image 177
John Woo Avatar answered Jan 17 '23 02:01

John Woo