Skip to content
Related Articles

Related Articles

Improve Article
Node.js MySQL CHAR_LENGTH() Function
  • Last Updated : 17 Feb, 2021

CHAR_LENGTH() function is a built-in function in MySQL that is used to get a number of characters in a given string.

Syntax:

CHAR_LENGTH(input_string)

Parameters: It takes one parameter as follows:

  • input_string: It is the given string whose length is to be determined.

Return Value: It returns a number of characters in a given string.

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 name, CHAR_LENGTH(name) 
                AS name_length 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:

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 salary, CHAR_LENGTH(salary) 
                AS salary_length 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:




My Personal Notes arrow_drop_up
Recommended Articles
Page :