Open In App

Node.js MySQL CONCAT_WS() Function

Improve
Improve
Like Article
Like
Save
Share
Report

CONCAT_WS() function is a built-in function in MySQL that is used to concatenate a set of strings with a commonly given separator.

Syntax:

CONCAT_WS(separator, string_1, string_2, ...)

Parameters: It takes two parameters as follows:

  • separator: This separator will be used to concatenate strings.
  • string: It is the set of given input strings separated by comma(‘,’).

Return Value: It returns a string which is the concatenation of a set of strings with common given separator.

Module Installation: Install the mysql module using the following command:

npm install mysql

Database: Our SQL publishers table preview with sample data is shown below:

Example 1:

index.js




const mysql = require("mysql");
  
let db_con  = mysql.createConnection({
    host: "localhost",
    user: "root",
    password: '',
    database: 'gfg_db'
});
  
db_con.connect((err) => {
    if (err) {
      console.log("Database Connection Failed !!!", err);
      return;
    }
  
    console.log("We are connected to gfg_db database");
  
    // Here is the query
    let query = 
"SELECT CONCAT_WS(' # ', 'Geeks', 'for', 'Geeks') AS Output";
  
    db_con.query(query, (err, rows) => {
        if(err) throw err;
  
        console.log(rows);
    });
});


Run the index.js file using the following command:

node index.js

Output:

Example 2:

index.js




const mysql = require("mysql");
  
let db_con  = mysql.createConnection({
    host: "localhost",
    user: "root",
    password: '',
    database: 'gfg_db'
});
  
db_con.connect((err) => {
    if (err) {
      console.log("Database Connection Failed !!!", err);
      return;
    }
  
    console.log("We are connected to gfg_db database");
  
    // Here is the query
    let query = 
"SELECT CONCAT_WS(': $', name, salary) AS Info FROM publishers";
  
    db_con.query(query, (err, rows) => {
        if(err) throw err;
  
        console.log(rows);
    });
});


Run the index.js file using the following command:

node index.js

Output:



Last Updated : 17 Feb, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads