32

When I concat($name, $surname), is there a way of putting a space in between the $name $surname using my sql not php so when i get the result it formats a little cleaner?

0

8 Answers 8

85

You can concatenate string literals along with your fields, so you can add a space character in a string between the fields you're concatenating.

Use this:

 CONCAT(name, " ", surname)

This functionality is documented quite clearly on the MySQL manual page for the CONCAT() function.

There is also the CONCAT_WS function which allows you to specify a separator to be used between each of the other fields passed to the function. If you're concatenating more than two fields in the same way, this function might be considered cleaner than repeating the separator between each field.

For example, if you wanted to add a middle name field, you could use this function to specify the separator only once:

CONCAT_WS(" ", first_name, middle_name, surname)
Sign up to request clarification or add additional context in comments.

Comments

18

Just add a space in there.

SELECT CONCAT(name,' ',surname) AS full_name FROM table;

EDIT oops, bad spelling there... ;p

2 Comments

@OMGPonies: I'm going with luck on this one. ;-)
+1. Just to be safe, I'd expand this to: CONCAT(COALESCE(name,''),' ',COALESCE(surname,''))
5

Use this, no version dependencies

concat(name,Char(32),venue)

Comments

0

Use CONCAT(name, ' ', surname).

Comments

0

after trying this i tried to do this

 concat(name, _utf8 '  ' ,name1)

1 Comment

Does this answer the question ?
0

Hey i had the same problem and this is what i used and it really worked out great

CONCAT(column1," ",column2) the issue is you add physical space in between the double courts("") using the space bar on your keyboard and then thats it

1 Comment

What's different about this compared to the answers from a few years ago?
0

The above all options are not working ..

My query is

SELECT CONCAT(FIRSTNAME, ' ' , LASTNAME) AS FULLNAME FROM USERS;

getting the below error:

ORA-00909: invalid number of arguments 00909 00000- "invalid number of arguments" *Cause: *Action: Error at line 26 column :9

1 Comment

Try CONCAT(CONCAT(FIRSTNAME, ' '), LASTNAME), it can probably only handle two arguments at a time.
0

Use this methoid to add separator

EXAMPLE 01

CONCAT_WS(' ', first_string, second_string, n_string, ..., last_string)

Output:

"first_string second_string n_string ... last_string"

EXAMPLE 02

CONCAT_WS('-', first_string, second_string, n_string, ..., last_string)

Output:

"first_string-second_string-n_string-...-last_string"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.