SUBSTRING function in SQL Server

The article will explore and guide you how to use the SUBSTRING function in SQL Server to extract a substring from a specified string.

Describe

The SUBSTRING function in SQL Server allows you to extract a substring of the specified length starting from a position in the input string.

Syntax

To use the SUBSTRING function in SQL Server, we use the following syntax:

 SUBSTRING(string, start, length) 

Parameters :

  1. string: can be a string, variable or column you want to extract.
  2. start: is an integer specifying the location where the substring starts to be returned. Note that the first character in the string is 1, not 0.
  3. length: a positive integer that specifies the number of characters of the substring returned from the string.

Note :

  1. If the length parameter has a negative value, SUBSTRING will have an error
  2. If start + length > the length of the string , the substring will start from start and include the remaining characters of the string.
  3. See also LEFT and RIGHT functions to extract a substring from a specified string.
  4. The SUBSTRING function can be used in later versions of SQL Server: SQL Server 2017, SQL Server 2016, SQL Server 2014, SQL Server 2012, SQL Server 2008 R2, SQL Server 2008, and SQL Server 2005.

For example

Take a look and explore some examples of SUBSTRING functions in SQL Server.

 SELECT SUBSTRING('QuanTriMang.com', 1, 4); 
Result: 'Quan'

SELECT SUBSTRING('QuanTriMang.com', 5, 3);
Result: 'Tri'

SELECT SUBSTRING('QuanTriMang.com', 5, 15);
Result: 'TriMang.com'

Previous lesson: STUFF function in SQL Server

Next lesson: UPPER function in SQL Server

4 ★ | 1 Vote

May be interested

  • DATEPART function in SQL ServerDATEPART function in SQL Server
    the datepart function in sql server returns a time value of the input argument, which can be day, month, year, quarter, hour, minute, second, millisecond ... the return value is an integer type (int)
  • DATENAME function in SQL ServerDATENAME function in SQL Server
    the datename function in sql server returns a time value of the input argument, which can be day, month, year, quarter, hour, minute, second, millisecond ... the return value is a string type (ascii)
  • ROUND function in SQL ServerROUND function in SQL Server
    the article will give you detailed instructions on how to use the sql server round () function with the syntax and specific examples to make it easier to visualize and capture the function better.
  • CEILING function in SQL ServerCEILING function in SQL Server
    the ceiling function in sql server returns the smallest integer value greater than or equal to the transmitted numeric expression.
  • RIGHT function in SQL ServerRIGHT function in SQL Server
    the article will explore and guide you how to use the right function in sql server to extract some characters from the right side of a given string.
  • FLOOR function in SQL ServerFLOOR function in SQL Server
    the floor function in sql server returns the largest integer value but less than or equal to the transmitted numeric expression.
  • MONTH function in SQL ServerMONTH function in SQL Server
    this article will show you in detail how to use the datetime month () processing function in sql server with specific syntax and examples to better visualize and capture functions.
  • DATEDIFF function in SQL ServerDATEDIFF function in SQL Server
    this article will show you in detail how to use sql server's datetime datediff () function with syntax and specific examples to make it easier to visualize and capture functions.
  • GETUTCDATE function in SQL ServerGETUTCDATE function in SQL Server
    this article will show you in detail how to use datetime getutcdate () function in sql server with specific syntax and examples to better visualize and capture functions.
  • SQL Server YEAR functionSQL Server YEAR function
    sql server's year function returns a 4-digit integer that is the year value in the timestamp passed.