SUBSTRING()

Anonymous contributor's avatar
Anonymous contributor
Published Sep 13, 2024
Contribute to Docs

In SQL, the SUBSTRING() function takes a slice from a string containing data in binary, character, text, or image format. Upon extraction, the SELECT statement can be used to select and manipulate the extracted substring according to the data type. The data type is the same as the original string except for the next expressions.

Syntax

SUBSTRING(string, start, length)
  • string: The string to extract a substring from.
  • start: The string index from which the substring starts. The minimum possible value is 1.
  • length: The number of characters to extract from string.

Note: Not all services support this function. Oracle and SQLite use SUBSTR() to accomplish the same goal.

Example

The below example shows how to use SUBSTRING():

SELECT SUBSTRING('Codecademy', 1, 4) AS ExtractString;

The code above generates the following output:

ExtractString
Code

All contributors

Contribute to Docs

Learn SQL on Codecademy