Node.js MySQL LENGTH() Function
Last Updated :
07 Oct, 2021
LENGTH() Function is a Builtin function in MySQL which is used to get length of given string in bytes.
Syntax:
LENGTH(input_string)
Parameters: LENGTH() function accepts a single parameter as mentioned above and described below.
- input_string: We will get number of characters of this string
Return Value: LENGTH() function returns length of given string in bytes.
Modules:
- mysql: To handle MySQL Connection and Queries
npm install mysql
SQL publishers Table Preview:
Example 1:
Javascript
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" );
let query = `SELECT LENGTH( 'Geeks for Geeks' ) AS length_in_bytes`;
db_con.query(query, (err, rows) => {
if (err) throw err;
console.log(rows);
});
});
|
Output:
Example 2:
Javascript
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" );
let query = `SELECT name, LENGTH(name) AS length_in_bytes FROM publishers`;
db_con.query(query, (err, rows) => {
if (err) throw err;
console.log(rows);
});
});
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...