what is CONCAT?
CONCAT()
CONCAT function is a String function, which is used to merge two or more strings and return a single string value. Example:-
SELECT CONCAT("ittutorial ", "is ", "a ", "best ","tutorial") AS Concatenated_String;
output :-
ittutorial is a best tutorial
SELECT CONCAT(Name, " ", address, " ", City) AS Address FROM ittutorial;
SELECT Name, CONCAT(Address, " ", pincode, " ", City) AS Address FROM ittutorial;
GROUP_CONCAT()
GROUP_CONCAT() is a type of an aggregate function. This function is used to concatenate string from multiple rows into a single string using various clauses. If the group contains at least one non-null value, it always returns a string value. Otherwise, you will get a null value.
SELECT cust_id, cust_fname, cust_lname, dept_id,
GROUP_CONCAT(designation) as "designation"
FROM customer group by cust_id;
Using DISTINCT Clause
SELECT cust_fname, cust_id,
GROUP_CONCAT(DISTINCT designation) as "designation"
FROM customer group by cust_id;
Using Separator Clause
SELECT emp_fname, GROUP_CONCAT(DISTINCT designation SEPARATOR '; ') as "designation" FROM employee group by emp_id;
Example
SELECT Id, Department, CONCAT(FirstName, ‘ ‘ , LastName) AS Fullname FROM Employee;
Previous
Next