SQL SERVER

SQL Server SUBSTRING Function: A Complete Guide

intro

Let’s learn everything you need to know about the SQL Server SUBSTRING function to extract a portion of a specified character, binary, text, or image expression.

Tools used in the tutorial
Tool Description Link
Dbvisualizer DBVISUALIZER
TOP RATED DATABASE MANAGEMENT TOOL AND SQL CLIENT

If you have ever written code or worked with strings, you know how common it is to extract substrings. It is such a frequent task that SQL Server includes a built-in function specifically for it. In particular, the SQL Server SUBSTRING function lets you extract specific portions of data from a string.

In this guide, you will learn what the SUBSTRING SQL Server function is, how it works, and how to use it through practical examples.

Let’s dive in!

What Is SUBSTRING in SQL Server?

In SQL Server, SUBSTRING is a built-in function that extracts a portion of a character, binary, text, or image expression. As you can see, the SQL Server SUBSTRING function works with different data types, making it a versatile tool for string manipulation.

This function is commonly used to extract specific parts of a string, especially when those parts have a fixed length. For example, it can be useful for extracting the date portion from a standard timestamp stored as a text string.

SQL Server SUBSTRING: Syntax and Notes

This is the syntax of SUBSTRING in SQL Server:

Copy
        
1 SUBSTRING ( expression , start , length )

Where:

  • expression is a character, binary, text, ntext, or image SQL Server expression—a combination of symbols and operators that SQL Server evaluates to return a single data value.
  • start is an integer or bigint that specifies the position (1-based index) where the returned portion begins. Note that indexing starts at 1, not 0.
  • length is a positive integer or bigint that determines how many characters or bytes to return from the expression.

The return type is:

  • Character data if expression is one of the supported character data types. Specifically, the returned string will have the same string type as the specified expression.
  • Binary data if expression is one of the supported binary data types.

For example, this is how you can use the SUBSTRING function in SQL Server to extract “Hello” from “Hello, World”:

Copy
        
1 SELECT SUBSTRING('Hello, World', 1, 5);

The result will be:

Copy
        
1 'Hello'

The result is “Hello” as that string goes from index 1 (“H”) to 5 (“o”) using a 1-based index.

Notes:

  • If start is less than 1, the returned expression begins at the first character in the expression. The number of characters returned is the larger value between (start + length - 1) or 0. For instance, SELECT SUBSTRING('Hello, World', -5, 8); is equivalent to SELECT SUBSTRING('Hello, World', 1, 2);, as both produce 'He'.
  • If start exceeds the number of characters in expression, an empty string '' is returned.
  • If length is negative, the following error is produced “Invalid length parameter passed to the substring function.”
  • If the length exceeds the number of characters in expression, the entire substring starting from start is returned.
  • If the function is called with fewer than 3 arguments, it fails with “The substring function requires 3 argument(s).”
  • If either start or length is NULL, SUBSTRING in SQL Server produces NULL.

SUBSTRING SQL Server Examples

Now that you know what the SUBSTRING SQL Server function is, you are ready to explore some real-world examples.

Note: The examples below are executed in SQL Server database client DbVisualizer, a top-rated database client. However, any other database client can also be used.

Time to see SQL Server SUBSTRING in action!

Use SUBSTRING on String Data for Programmatic Substring Extraction

Imagine you have a Users table containing user data, including email addresses:

Exploring the data in the Users table in DbVisualizer
Exploring the data in the Users table in DbVisualizer

Exploring the data in the Users table in DbVisualizer

Your goal is to retrieve the domain name from each email. This is how you can achieve that with SUBSTRING:

Copy
        
1 SELECT SUBSTRING(Email, CHARINDEX('@', Email) + 1, LEN(Email)) AS Domain 2 FROM Users;

The CHARINDEX function finds the position of the @ symbol, and SUBSTRING starts from the character after @ and extracts the rest of the email as the domain.

The result will be as follows:

Note that the result set is a list of domains
Note that the result set is a list of domains

As you can see, the query managed to extract the email address domains as desired.

Use SUBSTRING with Datetimes

In the Users table, the SubscribeDate column is a datetime value that stores the exact date and time a user subscribed. If you want to extract just the date portion (the first 1 character), you might be tempted to write:

Copy
        
1 SELECT SUBSTRING(SubscribeDate, 1, 10) AS SubscribeDate_Date 2 FROM Users;

However, this will result in an error because SubscribeDate is a datetime, not a string:

Copy
        
1 Argument data type datetime is invalid for argument 1 of substring function.
Note how DbVisualizer clearly reports the error
Note how DbVisualizer clearly reports the error

To fix that error, you need to cast the datetime data to a string first with the

Extracting the dates as desired
Extracting the dates as desired

This time, the query will work as expected and return only the date. Learn more in our guide on extracting time and date in MS SQL Server.

Use SUBSTRING on Binary Data

The SQL Server SUBSTRING function also works with binary data, as in this example:

Copy
        
1 SELECT SUBSTRING(0x48656C6C6F20576F726C64, 1, 5) AS Result;

0x48656C6C6F20576F726C64 is the binary representation of the “Hello, World” string. So, the result will be “Hello” in binary format, as you can verify in the special data binary viewer in DbVisualizer:

Exploring binary data in DbVisualizer
Exploring binary data in DbVisualizer

Note that the result is in binary data as the input is in binary data.

Conclusion

In this guide, you learned about the SQL Server SUBSTRING function, including how it works, its syntax, and when to use it. As shown, DbVisualizer makes working with these functions easier thanks to its full support for SQL Server, detailed error messages, and visual representation of both binary and text data. Other helpful features include SQL formatting, ERD-style schema diagrams, and tools for query optimization. Download DbVisualizer for free today!

FAQ

Does the SQL Server SUBSTRING function also work on non-text data?

Yes, in addition to text types, the SUBSTRING function in SQL Server works on non-text data types like binary, varbinary, and image. It extracts a portion of the value based on the specified starting position and length.

What other databases support the SUBSTRING function?

The SUBSTRING function is supported by many popular databases, including MySQL, PostgreSQL, Oracle, and SQLite. Keep in mind that the syntax may vary slightly, but the core functionality remains the same. Note that some databases also support the alias SUBSTR. For example, learn more in our guide on the MySQL SUBSTRING function.

Can you use SUBSTR in SQL Server?

No, SUBSTR is not available in SQL Server as an alias for SUBSTRING as it occurs in other databases like MySQL and PostgreSQL.

What is the alternative to SUBSTRING_INDEX SQL Server function?

SQL Server does not have a built-in SUBSTRING_INDEX function like MySQL. However, you can achieve similar results using a combination of CHARINDEX, SUBSTRING, and sometimes PARSENAME or STRING_SPLIT, depending on your use case. These functions help extract parts of a string based on a delimiter.

What are some useful string manipulation functions in SQL Server?

  • LEFT: Returns the left part of a string with the specified number of characters.
  • LTRIM: Removes leading spaces from a string.
  • RIGHT: Returns the right part of a string with the specified number of characters.
  • RTRIM: Removes trailing spaces from a string.
  • TRIM: Removes both leading and trailing spaces.

Why use a visual database client?

A visual database client like DbVisualizer simplifies working with databases and managing data, including text and binary data. It provides an intuitive interface for managing tables, exploring schema relationships, and writing/debugging queries with a powerful SQL editor. Specifically, features like autocomplete, ER diagrams, and data export tools set DbVisualizer apart from its competitors. Try the Pro version with a 21-day free trial!

Dbvis download link img
About the author
Antonello Zanini

Antonello is a software engineer, and often refers to himself as a technology bishop. His mission is to spread knowledge through writing.

The Table Icon
Sign up to receive The Table's roundup
More from the table
Title Author Tags Length Published
title

SQL Boolean Type: How to Use It in All Major Relational Databases

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 8 min 2025-09-23
title

SQL Server Vector Data Type, Search, and Indexing

author Antonello Zanini tags AI SQL SERVER Vectors 8 min 2025-08-25
title

SQL IS NOT NULL Condition: Definitive Guide

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 6 min 2025-08-13
title

Can a Primary Key Be NULL in an SQL Table?

author TheTable tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 5 min 2025-08-12
title

ISNULL vs COALESCE: Comparing NULL Handling Functions

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 6 min 2025-08-06
title

The SELECT INTO TEMP TABLE Mechanism in SQL

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 6 min 2025-08-05
title

A Guide to the CREATE TEMPORARY TABLE SQL Statement

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 7 min 2025-07-28
title

The RANK Window Function in SQL: A Complete Guide

author Leslie S. Gyamfi tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 6 min 2025-07-21
title

SQL Order By Random Strategies and Queries

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 7 min 2025-07-16
title

Top Real-Time Data Pipeline and CDC Tools in 2025

author Lukas Vileikis tags DbVisualizer MySQL POSTGRESQL SQL SQL SERVER 9 min 2025-07-14

The content provided on dbvis.com/thetable, including but not limited to code and examples, is intended for educational and informational purposes only. We do not make any warranties or representations of any kind. Read more here.