Skip to content
Related Articles

Related Articles

NodeJS MySQL LTRIM() Function
  • Last Updated : 18 Feb, 2021

LTRIM() Function is a Builtin Function in MySQL which is used to trim all spaces from left side of input string.

Syntax:

LTRIM(string)

Parameters: LTRIM() function accepts a single parameters as mentioned above and described below.

  • string: String needed to be trimmed from left side

Return Value:

LTRIM() function returns string after trimming from left side.



Modules:

  • mysql: To handle MySQL Connection and Queries
npm install mysql

Example 1: Hard Coded Query

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");
  
  // here is the query
  let query = `SELECT LTRIM('   Geeks For Geeks   ') AS LTRIM_Output`;
  
  db_con.query(query, (err, rows) => {
    if (err) throw err;
  
    console.log(rows);
  });
});

Output:

Example 2: Dynamic Query

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");
  
  // notice the ? in query
  let query = `SELECT LTRIM(?) AS LTRIM_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);
  });
});

Output:

My Personal Notes arrow_drop_up
Recommended Articles
Page :