Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • NodeJS Tutorial
  • NodeJS Exercises
  • NodeJS Assert
  • NodeJS Buffer
  • NodeJS Console
  • NodeJS Crypto
  • NodeJS DNS
  • NodeJS File System
  • NodeJS Globals
  • NodeJS HTTP
  • NodeJS HTTP2
  • NodeJS OS
  • NodeJS Path
  • NodeJS Process
  • NodeJS Query String
  • NodeJS Stream
  • NodeJS String Decoder
  • NodeJS Timers
  • NodeJS URL
  • NodeJS Interview Questions
  • NodeJS Questions
  • Web Technology
Open In App
Next Article:
Node.js MySQL SUM() Function
Next article icon

Node.js MySQL SUBSTRING() Function

Last Updated : 17 Feb, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

SUBSTRING() function is a built-in function in MySQL that is used to get a substring of input string between given range inclusive.

Syntax:

SUBSTRING(input_string, from, length)

Parameters: It takes three parameters as follows:

  • input_string: It is the given string for which the substring will be executed.
  • from: The substring will be taken from this position.
  • length: It is the length of the substring.

Return Value: It returns a substring of input string between a given starting position and length. If length is out of string then the extra part is ignored.

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

npm install mysql

Database: Our SQL publishers table preview with sample data is shown below:

Example 1:

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 SUBSTRING("GeeksforGeeks",                   8, 20) AS SUBSTRING_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:

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 SUBSTRING(name, 1, 4)              AS SUBSTRING_Name FROM publishers`;      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:


Next Article
Node.js MySQL SUM() Function

P

pratikraut0000
Improve
Article Tags :
  • Technical Scripter
  • Web Technologies
  • Node.js
  • Technical Scripter 2020
  • NodeJS-MySQL

Similar Reads

  • Node.js MySQL TRIM() Function
    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 side
    2 min read
  • Node.js MySQL POSITION() Function
    POSITION() Function is a Builtin Function in MySQL which is used to get position of first occurrence of a pattern in a text when searched from specific position. Note: It is not Case Sensitive. Syntax: POSITION(pattern IN text) Parameters: POSITION() function accepts two parameters as mentioned abov
    2 min read
  • Node.js MySQL SUM() Function
    We use the SUM() function in MySQL to get Sum of the value of some columns. Syntax: SUM(column_name) Parameters: SUM() function accepts a single parameter as mentioned above and described below. column_name: Column’s name from which we have to return the max value. Module Installation: Install the m
    2 min read
  • Node.js substr() function
    The substr() function is a string function of Node.js which is used to extract sub-string from the given string. Syntax: string.substr(index, length) Parameters: This function accepts two parameters as mentioned above and described below: index: This parameter holds the starting index of the specifi
    1 min read
  • Node.js MySQL UPPER() Function
    UPPER() function is a built-in function in MySQL that is used to convert all characters of a given string to uppercase. Syntax: UPPER(input_string)Parameters: It takes one parameter as follows: input_string: It is the given string that is passed for conversion to uppercase.Return Value: It returns a
    2 min read
  • Node.js MySQL UCASE() Function
    UCASE() function is a builtin function in MySQL that is used to convert all characters of a given string to uppercase. Syntax: UCASE(input_string)Parameters: It takes one parameter as follows: input_string: It is the string that is passed for converting this string to uppercase.Return Value: It retu
    2 min read
  • Node.js MySQL MID() Function
    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 b
    2 min read
  • Node.js MySQL FIND_IN_SET() Function
    FIND_IN_SET() function is a built-in function in MySQL that is used to get the position of the first occurrence of value string in a list of strings separated by comma(','). Syntax: FIND_IN_SET(value, list_of_string)Parameters: It takes two parameters as follows: value: It is the value to be searche
    2 min read
  • Node.js MySQL Min() Function
    We will be using Min() function in MySQL to get Minimum value from some particular column. Syntax: MIN(column_name) Parameters: MIN() function accepts a single parameter as mentioned above and described below. column_name: Column’s name from which we have to return the min value. Return Value: MIN()
    2 min read
  • Node.js MySQL AVG() Function
    We use AVG Function to get the average value of all rows in the given column of MySQL Table. Syntax: AVG([column_name]) Parameters: AVG() function accepts a single parameter as mentioned above and described below. column_name: Column’s name from which we have to return the average value. Module Inst
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences