Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any alternative for CONCAT() in MySQL?

I want to add a constant string to all result of one column. Here is my table:

// mytable

 id | name
----|--------
 1  | jack
 2  | peter
 3  | ali

I want this output:

select name from mytable order by id;

Your name: jack
Your name: peter
Your name: ali

As you see, I have combined all results of name column with this string: Your name:. I can do that like this:

... CONCAT('Your name: ', name) ... 

Now I want to know, is there any approach else? (something like combining by + in MySQL)

like image 487
stack Avatar asked Sep 13 '25 00:09

stack


1 Answers

CONCAT is the default way to do string concatenation. + is arithemtic operator and when you try:

SELECT 'a' + 'b'  -- you will get 0, because of implicit cast to number

There is also CONCAT_WS():

SELECT CONCAT_WS(SPACE(1), 'Your name:','Bob') AS result 

SqlFiddleDemo

like image 116
Lukasz Szozda Avatar answered Sep 15 '25 13:09

Lukasz Szozda