Node.js MySQL - LENGTH()
In this post, we will discuss how to return length of values in a column of MySQL table in XAMPP Server through Node.js application with LENGTH() function
It is important to install mysql package in node.js.
Command to install the mysql package:
npm install mysql
LENGTH() Function:
LENGTH() will return the total number of characters present in string values present in a column.It takes only one parameter IE column name.
Syntax:
LENGTH(column)
Steps for Node.js script:Now let's see steps
- First start your XAMPP Server (Both Apache and MySQL).
- Open Notepad or any text-editor and write the Node.js script
- In that script, first we have to load the mysql package using the below syntax var mysql_package = require('mysql');
- Create the connection using the server,username and password.
- Write the sql query that uses LENGTH() function
- Now type the following command in your command prompt to run the script. node file_name.js
var connection_data = mysql_package.createConnection({
host: "localhost",
user: "root",
password: "",
database:"database_name"
});
connection_data.connect(function(error) {
connection_data.query("SELECT RIGHT(column),.... FROM table_name
WHERE condition/s...", function (error, result) {
console.log(result);
});
});
Consider the village2 table with the following records:
LENGTH Example 1:-
Let's return the length of values from description column.
// Load the mysql package
var mysql_package = require('mysql');
// Create the connection using the server,username and password.
//In my scenario - server is the localhost,
//username is root,
//password is empty.
//database is facility
var connection_data = mysql_package.createConnection({
host: "localhost",
user: "root",
password: "",
database:"facility"
});
connection_data.connect(function(error) {
// Write sql query to return length of strings in description column
connection_data.query("SELECT description, LENGTH(description)
FROM village2", function (error, result) {
//Display the records one by one
console.log(result);
});
});
Output:
[
RowDataPacket {
description: 'Hello GKIndex Welcome to my tutorial',
'LENGTH(description)': 36
},
RowDataPacket {
description: 'Hello GKIndex Welcome to my tutorial',
'LENGTH(description)': 36
},
RowDataPacket {
description: 'Hello Node.js',
'LENGTH(description)': 13
},
RowDataPacket {
description: 'Hello Ujjain',
'LENGTH(description)': 12
}
]
The lenth of each string is returned. It will consider one space as one.
LENGTH Example 2:-
Let's return the length of values from name column where name is 'pnp'.
// Load the mysql package
var mysql_package = require('mysql');
// Create the connection using the server,username and password.
//In my scenario - server is the localhost,
//username is root,
//password is empty.
//database is facility
var connection_data = mysql_package.createConnection({
host: "localhost",
user: "root",
password: "",
database:"facility"
});
connection_data.connect(function(error) {
// Write sql query to return length of strings
//in name column with name as 'pnp',
connection_data.query("SELECT name, LENGTH(name) FROM
village2 where name='pnp'", function (error, result) {
//Display the records one by one
console.log(result);
});
});
Output:
[
RowDataPacket { name: 'pnp', 'LENGTH(name)': 3 },
RowDataPacket { name: 'pnp', 'LENGTH(name)': 3 }
]
There are two strigs with 'pnp' and length is 3.
SummaryIn this post, we seen how to use LENGTH() function on MySQL table in XAMPP Server and also it can be possible to specify WHERE clause along this function.