SQLforGeeks
  • Home
  • SQL
  • SQL Tutorial
  • SQL Syntax
  • Our Services
Contact
  1. SQL
  2. SUBSTRING function

Text copied!

« Previous
Next »

SUBSTRING function

June 7, 2023, 7:09 p.m. under SQL

  • Hi! You should check REPLACE function post first.

    In SQL, Substring function is used to extract a portion(substring) of a specified string value based on specified starting and ending length.

    What is a String?

    In SQL, A string is a data type used to store data that can contain combination of numbers, letters, whitespaces and symbols. In simple term, String is a word or a sequence of characters.

    What is a Substring?

    A substring is a consecutive sequence of characters or a portion of a string.

    The syntax of the SUBSTRING function generally looks like this :
    SUBSTRING(string, starting_position, length)
            

    • Specify the 'string' in single quotes ( ' ' ) or 'column' from which you want to extract the substring.

    • Specify the 'starting position' from where you want to begin substring extraction.

    • Specify the 'length' till what you want to extract the substring.

    Here's an example of how you might use the SUBSTRING function :

    1. Let's assume we have a table named "[Employees]".

    SUBSTRING function

    2. Let's assume you want to extract the first three characters from Employee_Name column.

    3. Run below SQL statement :

    SELECT SUBSTRING(Employee_Name, 1, 3)
    FROM [Employees];
            

    4. In above statement, SUBSTRING function extracts initial three characters from Employee_Name column.

    SUBSTRING function
    Remember :

    Using this function in a select statement won't modify the [Employees] table directly, but it will only be reflected in the select statement's output.

    To enhance your understanding of 'SQL functions', be sure to check out our next tutorial.

    Frequently Asked Questions :

    What is the substring function in SQL?

    The substring function in SQL extracts a portion of a string based on specified starting position and length.

    What is the substring function?

    Substring function is used to extract a portion of a string in SQL.

    How do I get the last 3 characters in a string in SQL?

    To get the last 3 characters in a string in SQL, use the substring function with the appropriate parameters.

    How do I get the first 5 characters of a string in SQL?

    To get the first 5 characters of a string in SQL, utilize the substring function with the starting position set to 1 and the length set to 5.
    Thank You! You should check CASE statement post next.
    « Previous
    Next »
    RELATED :

    What is SQL

    What is T-SQL

    Difference between DBMS and Data Warehouse

    Download SQL Server

    Install SQL Server

    Download SQL Server Management Studio SSMS

    SQL Server Management Studio

    SQL Database

    Download database

    Restore database

    Backup database

    Attach database

    Detach database

    Create database

    Delete database

    Rename database

    Select database

    Database offline

    Database online

    SQL Commands

    SQL Tables

    Create table

    Truncate table

    Delete table

    Rename table

    Select table

    Alter table

    SQL Data Types

    SQL Comments

    SQL Constraints

    SQL Joins

    SQL inner join

    SQL left join

    SQL right join

    SQL full join

    SQL cross join

    SQL self join

    INSERT INTO SELECT statement

    INSERT INTO statement

    SQL Clauses

    SELECT clause

    FROM clause

    WHERE clause

    GROUP BY clause

    HAVING clause

    ORDER BY clause

    JOIN clause

    UNION clause

    UNION ALL clause

    TOP clause

    DISTINCT clause

    SQL Operators

    SQL Arithmetic operators

    SQL Comparison operators

    SQL Logical operators

    UNION operator

    UNION ALL operator

    INTERSECT operator

    EXCEPT operator

    LIKE operator

    NOT LIKE operator

    IN operator

    NOT IN operator

    IS NULL operator

    IS NOT NULL operator

    EXISTS operator

    NOT EXISTS operator

    BETWEEN operator

    NOT BETWEEN operator

    SQL Functions

    SQL Built-In functions

    CHARINDEX function

    DATEADD function

    CONCAT function

    LEN function

    REPLACE function

    CASE statement

    GETDATE function

    DATEPART function

    DATEDIFF function

    CAST function

    TRY_CAST function

    CONVERT function

    TRY_CONVERT function

    ISNULL function

    NULLIF function

    COALESCE function

    SQL Window functions

    ROW_NUMBER function

    RANK function

    DENSE_RANK function

    IIF function

    CHOOSE function

    SQL Store Procedure

    Store Procedure vs. Function

    SQL Subquery

    SQL Aliases

    Temp table

    SQL Error Handling

    SQL Variables

    SQL Views

    SQL Merge

    SQL CTE

    SQL WITH TIES

    Define Transaction in DBMS

    ACID properties in DBMS

    Types of Triggers in DBMS


    • Have Some Questions?
    logo

    Elevate your data experience with SQL excellence

    Quick Links
    •  Home
    •  SQL Tutorial
    •  SQL Syntax
    •  Our Services
    Our Services
    • Web Development
    • BI Development
    • Data Warehousing
    • Data Integration ETL


    Follow Us

    GST Registered: XXAXXXXXXXZX
    Mumbai, Maharashtra, India support@sqlforgeeks.com

    © 2025 Copyright™ | All Rights Reserved | Privacy Policy