Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql concat function

Tags:

mysql

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?

many thanks

like image 252
Adamski Avatar asked Dec 20 '10 22:12

Adamski


People also ask

What is concat function in MySQL?

The CONCAT() function adds two or more expressions together.

Can we use to concat in MySQL?

CONCAT() function in MySQL is used to concatenating the given arguments. It may have one or more arguments. If all arguments are nonbinary strings, the result is a nonbinary string. If the arguments include any binary strings, the result is a binary string.

Is there a concat function in SQL?

The CONCAT function in SQL is a String function, which is used to merge two or more strings. The Concat service converts the Null values to an Empty string when we display the result. This function is used to concatenate two strings to make a single string.


3 Answers

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)
like image 175
AgentConundrum Avatar answered Oct 07 '22 14:10

AgentConundrum


Just add a space in there.

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

EDIT oops, bad spelling there... ;p

like image 17
Brad Christie Avatar answered Oct 07 '22 12:10

Brad Christie


Use this, no version dependencies

concat(name,Char(32),venue)
like image 5
Sam Avatar answered Oct 07 '22 12:10

Sam