SQL Server SUBSTRING() Function


  • In SQL Server , the SUBSTRING functions allows you to extract a substring from a string that is varchar data-type.
  • The SUBSTRING function takes three arguments.
  • The source string to extract from and that we want to query on.
  • The second argument is the starting character and The first position in the string is always 1
  • and the third argument is the The number of characters to extract

 


The SUBSTRING function syntax in SQL Server is as follows:-

SUBSTRING( String, Start_position, Length )

string: – Input source string
start_position: – The position to start extraction from. The first position in the string is always 1 and the initial position also be negative integer.
length: –  The number of characters to extract and it should be a positive integer value. and It also specifies the ending limit and determines how many characters are going to be extracted from the given string.

Note:-SQL Substring function traverse from left to right.


Below example shows how to use the SUBSTRING function in SQL Server :-

SELECT SUBSTRING('Tutorialspoint4all.com', 1, 8);


SELECT SUBSTRING('Tutorialspoint4all.com',3,9);


SELECT SUBSTRING('Tutorialspoint4all.com',20, 22);

SELECT SUBSTRING('Tutorialspoint4all.com',20, 1);

 

Output:-


Simple data handling with the Substring function in SQL Server

SELECT first_name, substring(first_name,1,3), last_name FROM employee

 

Output:-

 


 

Leave a Reply

Your email address will not be published. Required fields are marked *