CONCAT function is mysql plays a vital role. The CONCAT() function combine or concatenate the list of strings together .
We have 2 types of concat function ->
- CONCAT() – This function will concate list of string together without any separator or space .It is capable of joining two or more number of strings together. We can use the below code to concate the strings.
CONCAT(string1,string2,string3,string4....etc.)
after running this code we will get the results like this
string1string2string3string4...
If we want to add space or anything to separate string we concate then we need to include an additional space or argument with this function to provide any space or any seperator argument between them. But this will create some confusions that is why we use another concat_WS() function
- CONCAT_WS() – This CONCAT function will join each of the give string with one another, with a separator string.This function accepts required separator string as its first argument and the rest of its arguments are the list of strings to be joined together. This function do not need any additional seperator in the query.
Example->
CONCAT_WS(<separator-string>,string1,string2);
after running the above code we will get the result
strin1<separator-string>string2
Here are some examples that we can see how to apply this concatenation function for MySQL SELECT, INSERT and UPDATE queries.
Insert Query:
INSERT INTO <table-name> (field1) VALUES (CONCAT(string1,string2,string3...))
Update Query:
UPDATE <table-name> SET field1=CONCAT(string1,string2,string3...)
Select Query:
SELECT CONCAT(string1,string2,string3...) FROM <table-name>
0 Comment(s)