Adventures in Machine Learning

Manipulating Text in SQL: A Guide to the SUBSTRING() Function

Working with Text Data in SQL: Understanding the SUBSTRING() Function

In data management, it is essential to have knowledge of how to manipulate data to obtain the required information accurately. One tool that is commonly used in SQL is the SUBSTRING() function.

The SUBSTRING() function is used to extract a section of characters from a string, given its starting position and length. In this article, we will explore the basics of text functions, standard SQL functions, and the syntax and explanation of the SUBSTRING() function.

Additionally, we will provide examples of how to use the function in various Text Functions

Text functions, as the name implies, are used to manipulate text strings. These functions enable the user to modify the content of a string by adding, removing, or editing its characters.

There are several text functions available in SQL, and understanding how they work is a valuable skill in data management.

Standard SQL Functions Course

Understanding standard SQL functions is critical in comprehending the SUBSTRING() function. Standard SQL functions include arithmetic, date-time, aggregate, comparison, and text functions.

It is essential to note that different database platforms have varying functions that may not work universally.

Syntax and Explanation of SUBSTRING() Function

The syntax of the SUBSTRING() function is as follows:

SUBSTRING(string, start, length)


– “string” represents the string of text that requires subsetting. – “start” indicates the starting position of the section extracted.

– “length” indicates the number of characters to extract from “string.”

The SUBSTRING() function is not case sensitive, so the user does not have to initiate a distinction between upper- and lower-case letters. The start parameter of this function is usually a numeric value with an integer type.

However, start and length can also be expressions that return integer values.

Examples of Using SUBSTRING() Function

Let us go through several examples of how to use the SUBSTRING() function to extract relevant information from strings. Example 1: Substring From a String Literal

Consider the following example where you want to extract characters 4 to 10 from the string “Computer Science.” Using the SUBSTRING() function, the following syntax can be used:

SELECT SUBSTRING(‘Computer Science’, 4, 7);

Output: “puter S”

Primary Keyword(s): SUBSTRING(), string literal

Example 2: Substring From a Column

The following example will illustrate how to extract initials from a full name column.

We will assume that the full name column values are consistent in having the first letter of the name followed by a period. Given this requirement, we will use the SUBSTRING() function to extract the initials:

SELECT SUBSTRING(full_name, 1, 1) || ‘.’ || SUBSTRING(full_name, CHARINDEX(‘ ‘, full_name)+1, 1) || ‘.’ as initials

FROM employees;

Output: “J.


Primary Keyword(s): SUBSTRING(), column, initials

Example 3: Substring Without the Length Argument

Assume we want to extract the year from a date variable “start_date” in the following format: “yyyy-mm-dd.” In this case, we can determine the length of the year in the date string format, which is four. Using this knowledge, we can obtain the year by extracting the first four digits from “start_date” using the SUBSTRING() function, as illustrated below:

SELECT SUBSTRING(start_date, 1, 4) as year from monthlies;

Output: “2022”

Primary Keyword(s): SUBSTRING(), length argument, start_date, year

Example 4: POSITION() and CHARINDEX()

In this example, we’ll use the POSITION() function, which is an equivalent of the CHARINDEX() function, to extract the username from users’ email addresses.

SELECT SUBSTRING(email, 1, POSITION(‘@’ IN email) – 1) as username

FROM users;

Output: “johndoe”

Primary Keyword(s): POSITION(), CHARINDEX(), email, username

Example 5: LENGTH() + POSITION()

Suppose the job titles in an organization’s employee table is a combination of the job position and the job title, all separated by a hyphen. To isolate the job position, we can use the SUBSTRING() function.

Here, we will need to use the LENGTH() function in conjunction with POSITION() to determine the position of the hyphen:

SELECT SUBSTRING(job_title, 1, POSITION(‘-‘ IN job_title) – 1) as job_position

FROM employees;

Output: “Project Manager”

Primary Keyword(s): LENGTH(), POSITION(), job_title, job position


The SUBSTRING() function is an essential tool in manipulating and extracting vital information from text data in SQL. The user must understand how the function operates to ensure that they utilize it effectively.

In this article, we have covered the basics of text functions and standard SQL functions, described the syntax and explanation of the SUBSTRING() function, and provided use case examples. These examples show how to use the function in different scenarios to extract relevant data from strings.

(Note: This article does not contain a conclusion as per the instruction given)

In conclusion, the SUBSTRING() function is a valuable tool for manipulating and extracting important information from text data in SQL. This article has covered the basics of text functions, standard SQL functions, and the syntax and explanation of the SUBSTRING() function.

Furthermore, we provided examples of the function being used in different scenarios. It is important to understand how to use this function effectively to ensure that necessary information is extracted correctly for data management.

Overall, knowing how to use text functions is an essential skill, and the SUBSTRING() function is a useful tool to have in your data analysis toolkit.

Popular Posts