SQL SUBSTRING


David Haertzen David Haertzen, Tutorial Author
Check out our
Data Warehousing Tutorial.
Previous | Next

SQL > SQL Advanced > SQL SUBSTRING


What is SQL SUBSTRING?

The SQL Substring feature is a function that enables parts of strings to be accessed.

For example, the function SUBSTRING('212-555-1234', 9 , 4) returns:

'1234'

It returns 4 characters starting in position 9.

Why Use SQL SUBSTRING?

Substring is useful when accessing a column that consists of meaningful subcomponents, such as a telephone number that contains area code, prefix and phone number body.

How To Use SQL SUBSTRING

SQL SUBSTRING is used as follows.

SQL SUBSTRING Syntax

 
SELECT SUBSTRING(<column_name>, position, length)
FROM <table_name>
 

SQL SUBSTRING Example

This example returns three characters of the region_name column starting in position 2.

Here are the contents of the table:

Table: REGION
region_nbrregion_name
100East Region
200Central Region
300Virtual Region
400West Region

This SQL Statement with SUBSTRING is executed:

 
SELECT region_name, SUBSTRING (region_name, 2, 3) as substring_name
FROM dbo.region
ORDER BY 1

Here is the result.

region_namesubstring_name
East Regionast
Central Regionent
Virtual Regionirt
West Regionest

Copyright© 1999-2015, First Place Software, Inc.