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:

CopiedCopy Code
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

  1. First start your XAMPP Server (Both Apache and MySQL).
  2. Open Notepad or any text-editor and write the Node.js script
  3. In that script, first we have to load the mysql package using the below syntax
  4. var mysql_package = require('mysql');
  5. Create the connection using the server,username and password.
  6. CopiedCopy Code
    
    var connection_data = mysql_package.createConnection({
      host: "localhost",
      user: "root",
      password: "",
      database:"database_name"
    });
    
  7. Write the sql query that uses LENGTH() function
  8. CopiedCopy Code
    
    connection_data.connect(function(error) {
      connection_data.query("SELECT RIGHT(column),.... FROM table_name 
      WHERE condition/s...", function (error, result) {
         console.log(result);
      });
    });
    
  9. Now type the following command in your command prompt to run the script.
  10. node file_name.js

Consider the village2 table with the following records:

alt=

LENGTH Example 1:-

Let's return the length of values from description column.

CopiedCopy Code

// 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:

CopiedCopy Code

[
  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'.

CopiedCopy Code

// 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:

CopiedCopy Code

[
  RowDataPacket { name: 'pnp', 'LENGTH(name)': 3 },
  RowDataPacket { name: 'pnp', 'LENGTH(name)': 3 }
]

There are two strigs with 'pnp' and length is 3.

Summary

In 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.