Skip to content
Related Articles

Related Articles

NodeJs MySQL TRIM() Function
  • Last Updated : 17 Feb, 2021

TRIM() function is a built-in function in MySQL that is used to trim all spaces from both sides of the input string.

Syntax:

TRIM(string)

Parameters: It takes one parameter as follows:

  • string: It is the string to be trimmed from both sides.

Return Value: It returns string after trimming from both sides.

Module Installation: Install the mysql module using the following command:



npm install mysql

Example 1: Hard-Coded Query

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 TRIM('   Geeks For Geeks   ') AS TRIM_Output`;
  
    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: Dynamic Query

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");
  
    // Notice the ? in query
    let query = `SELECT TRIM(?) AS TRIM_Output`;
      
    // Dynamic Input
    let input_string = "    Pratik Raut   ";
  
    // Notice Second Dynamic Parameter
    db_con.query(query, input_string, (err, rows) => {
        if(err) throw err;
  
        console.log(rows);
    });
});

Run the index.js file using the following command:

node index.js

Output:


Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :