Node.js MySQL - RIGHT()

In this post, we will discuss how to select substring from right in a column in a MySQL table from XAMPP Server using Node.js with RIGHT() function.

It is important to install mysql package in node.js.

Command to install the mysql package:

CopiedCopy Code
npm install mysql
				

RIGHT() Function:

RIGHT() will return the substring from the right in a string type column upto particular characters from a table. It will take two parameters.

Syntax:

RIGHT(column, length)

  1. The first parameter is the column name that hold string type values.
  2. The second parameter specifies the numner of characters to be returned from right.

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 RIGHT() function
  8. CopiedCopy Code
    
    connection_data.connect(function(error) {
      connection_data.query("SELECT RIGHT(column,length),.... 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=

RIGHT Example 1:-

Let's return the substring from right in description upto 5th character.

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 substrings from right.
  connection_data.query("SELECT description, RIGHT(description,5) 
  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',
    'RIGHT(description,5)': 'orial'
  },
  RowDataPacket {
    description: 'Hello GKIndex Welcome to my tutorial',
    'RIGHT(description,5)': 'orial'
  },
  RowDataPacket {
    description: 'Hello Node.js',
    'RIGHT(description,5)': 'de.js'
  },
  RowDataPacket {
    description: 'Hello Ujjain',
    'RIGHT(description,5)': 'jjain'
  }
]

So from right, the substrings upto 5th character were returned. Indirectly the length of the substring is equal to the length.

RIGHT Example 2:-

Let's return the substring from right in description upto 10th character.

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 substrings from right.
  connection_data.query("SELECT description, RIGHT(description,10) 
  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',
    'RIGHT(description,10)': 'y tutorial'
  },
  RowDataPacket {
    description: 'Hello GKIndex Welcome to my tutorial',
    'RIGHT(description,10)': 'y tutorial'
  },
  RowDataPacket {
    description: 'Hello Node.js',
    'RIGHT(description,10)': 'lo Node.js'
  },
  RowDataPacket {
    description: 'Hello Ujjain',
    'RIGHT(description,10)': 'llo Ujjain'
  }
]

So from right, the substrings upto 10th character were returned. It will consider single space also as one character.

RIGHT Example 3:-

Let's return the substring from right in district column upto 3rd character where district is 'np.sagar'.

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 substrings from right.
  connection_data.query("SELECT district, RIGHT(district,3) 
  FROM village2 where district='np.sagar'", function (error, result) {
//Display the records one by one
    console.log(result);
  });
});

Output:

CopiedCopy Code

[ RowDataPacket { district: 'np.sagar', 'RIGHT(district,3)': 'gar' } ]

So from right, there is only one string that matches the condition and three characters were returned from right.

Summary

In this post, we seen how to use RIGHT() function on MySQL table in XAMPP Server and also it can be possible to specify WHERE clause along this function.