Substring In SQL

Chapter: SQL Commands Last Updated: 27-11-2019 16:59:45 UTC

Program:

            /* ............... START ............... */
                
-------------------------------------------------------
SELECT SUBSTRING('SQL Substring example', 1, 3) result;
result
------
SQL
 
(1 row affected)

-------------------------------------------------------
SELECT SUBSTRING('SQL Substring example', 5, 9) result;
result
------
Substring
 
(1 row affected)
--------------------------------------------------------
SELECT SUBSTRING(ClientName, 2, 5) AS Name
FROM Client;  
/* This will extract the substring from the text in a column 
ClientName (start at position 2, extract 5 characters) */
--------------------------------------------------------
SELECT SUBSTRING("SQL Tutorial", -5, 5) AS ExtractString;

/* Extract a substring from a string (start from the end, 
at position -5, extract 5 characters) */
                /* ............... END ............... */
        

Notes:

  • The SUBSTRING() function in SQL is used to extracts characters from a string.
  • Syntax : SUBSTRING(string, start_index, length_of_character)
  • Above Syntax string parameter is the string passed for extraction, start_index is the index where the extraction starts length_of_character is the length of characters to extract from string.
  • start index of extraction for all string in SQL starting index is always 1.
  • If you pass negative number in length parameter it will throw and error.
  • For more information please refer the program section.

Tags

Substring in sql, sql substring charindex, SQL Server SUBSTRING() Function, substring function in sql

Similar Programs Chapter Last Updated
SQL SELECT Statement SQL Commands 16-03-2018

1