Skip to content
Related Articles

Related Articles

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

MID() Function is a Builtin Function in MySQL which is used to get substring of input string between given range inclusive.

Syntax:

MID(input_string, from, length)

Parameters: MID() function accepts three parameters as mentioned above and described below.

  • input_string: Substring of this input will be calculated
  • from:Substring will be taken from this position
  • length: Length of substring

Return Value:

MID() function returns substring of input string between given starting position and length. If length is out of string then the extra part is ignored.



Modules:

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

SQL publishers Table Preview:

Example 1:

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 MID("Geeks For Geeks", 7, 20) AS MID_Output`;
  
  db_con.query(query, (err, rows) => {
    if (err) throw err;
  
    console.log(rows);
  });
});

Output:

Example 2:

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 name, MID(name, 1, 3) AS MID_Output FROM publishers`;
  
  db_con.query(query, (err, rows) => {
    if (err) throw err;
  
    console.log(rows);
  });
});

Output:

My Personal Notes arrow_drop_up
Recommended Articles
Page :