What is SUBSTRING Function in SQL? [ Explained with Examples]

avcontentteam 21 Jul, 2023 • 4 min read

Have you ever come across the need to extract a specific portion of a string in SQL? You can retrieve the first few characters of a column or isolate a substring based on a specific position or length. In such cases, the SUBSTRING SQL comes to the rescue. You may quickly alter strings with this effective tool, giving your database queries more flexibility. This comprehensive tutorial will cover all aspects of SUBSTRING SQL and provide a thorough overview of its syntax, application, performance advice, and practical examples.

What is a SUBSTRING Function in SQL?

The SUBSTRING function is a powerful tool in SQL that allows us to extract a portion of a string based on specified criteria. It allows us to efficiently manipulate and analyze textual data, whether we need to extract a specific number of characters, retrieve a substring from a particular position, or extract a substring based on a pattern. In this article, we will explore the various applications and syntax of the SUBSTRING function in SQL.

 Alt text: SQL
Source: onemonth

Syntax of Substring in SQL

The syntax of the SUBSTRING function in SQL varies slightly depending on which DBMS you are using However, the general syntax is as follows:

For most DBMS

SUBSTRING(string_expression, start_position, length)

For some DBMS (e.g., Oracle)

SUBSTR(string_expression, start_position, length)
  • string_expression: The input string from which the substring will be extracted.
  • start_position: The starting position of the substring within the input string. It is typically an integer value.
  • length: (Optional) The length of the substring to be extracted. If omitted, the substring will be extracted from the start_position to the end of the input string.

It’s important to note that the start_position parameter is usually 1-based, meaning the first character of the input string is at position 1.

Also Read: Top 10 SQL Projects for Beginners, Intermediate and Experienced Learners

SUBSTRING SQL Queries and Examples

To further solidify your understanding of SUBSTRING in SQL, let’s delve into practical examples of SUBSTRING queries that demonstrate its versatility and application in various scenarios. These examples will cover a wide range of use cases, ranging from simple extractions to more advanced queries.

Example 1: Extracting First Names

Suppose you have a column called full_name in a table that stores the full names of individuals. You want to extract only the first names from this column. You can achieve this using Substring as follows:

SELECT SUBSTRING(full_name, 1, CHARINDEX(' ', full_name) - 1) AS first_name
FROM table_name;

In this example, the Substring function is combined with CharIndex to locate the position of the first space character, which separates the first name from the last name. The Substring function then extracts the substring from the beginning of the full_name column until the position of the space character minus one.

Example 2: Trimming URLs

Suppose you have a column called url that contains URLs with unnecessary characters at the beginning and end. You want to extract the core part of the URL by removing the protocol (e.g., “http://” or “https://”) and any trailing slashes. You can accomplish this with Substring as follows:

SELECT SUBSTRING(url, CHARINDEX('//', url) + 2, LEN(url) - CHARINDEX('//', url) - 1) AS trimmed_url
FROM table_name;

In this example, the Substring function is used to extract the substring starting from the position after the “//” characters until the end of the URL. By subtracting the position of the “//” characters from the length of the URL, we ensure that any trailing slashes are excluded.

Example 3: Extracting Phone Number Area Codes

Suppose you have a column called phone_number that contains phone numbers in the format “(XXX) XXX-XXXX”, where XXX represents the area code. You want to extract the area codes from these phone numbers. You can achieve this using Substring as follows:

SELECT SUBSTRING(phone_number, 2, 3) AS area_code
FROM table_name;

In this example, the Substring function is utilized to extract the substring starting from the second character (omitting the opening parenthesis) with a length of three characters. This effectively captures the area code portion of the phone numbers.

Explore advanced SQL topics here.

Difference between Substring and CharIndex

Here’s a table comparing the SUBSTRING and CHARINDEX functions in SQL:

 SUBSTRING FunctionCHARINDEX Function
PurposeExtracts a portion of a string based on a specified position and lengthFinds the starting position of a specified substring in a string
SyntaxSUBSTRING(string_expression, start_position, length)CHARINDEX(substring, string_expression)
ExampleSUBSTRING(‘Hello, World!’, 8, 5) returns “World!”CHARINDEX(‘World’, ‘Hello, World!’) returns 8
UsageUseful for extracting a specific portion of a stringHelpful for locating the position of a substring in a string

Using these functions together, you can perform various string manipulation tasks in SQL, such as extracting substrings and finding their positions within strings.

SQL Full Course

Conclusion

The Substring function in SQL provides a powerful tool for manipulating strings, extracting specific portions, and performing data transformations. By mastering the Substring syntax, understanding its usage, and following performance optimization techniques, you can unlock the full potential of this function in your SQL queries. Armed with the knowledge gained from this ultimate guide, you can confidently tackle string manipulations and extract valuable insights from your database.

Frequently Asked Questions

Q1. What is Substring () in SQL?

A. You can separate a substring from a longer string using SQL’s Substring() string function. It is frequently used to get a certain string segment based on the length and starting position.

Q2. How do I select the first 3 characters in SQL?

A. In SQL, you can use the Substring function with a start position of 1 and a length of 3 to retrieve the first 3 characters of a string. Here’s an illustration:

SELECT SUBSTRING(column_name, 1, 3) AS first_three_characters
FROM table_name;

Q3. How to extract the first 2 characters in SQL?

A. In SQL, you may use the Substring function with a start position of 1 and a length of 2 to extract the first two characters from a string. Here’s an illustration:

SELECT SUBSTRING(column_name, 1, 2) AS first_two_characters
FROM table_name;

avcontentteam 21 Jul 2023

Frequently Asked Questions

Lorem ipsum dolor sit amet, consectetur adipiscing elit,

Responses From Readers

Recommended for you
  • [tta_listen_btn class="listen"]