T-SQL 101: 46 Using LEFT, RIGHT, and SUBSTRING to extract substrings in SQL Server

T-SQL 101: 46 Using LEFT, RIGHT, and SUBSTRING to extract substrings in SQL Server

You might need to extract part of a string from a given string. T-SQL has a number of functions to help you do that. Take a look at these examples:

LEFT, RIGHT, and SUBSTRING are all functions that return parts of strings .

In the first example, we asked for 3 characters from the left of the string so SQL Server returned ABC.

The second example shows returning 2 characters from the right of the string, so we get KL.

The third example same to return 4 characters starting at position 5, so we get EFGH.

It’s worth noting that unlike languages such as C#, SQL Server doesn’t complain if you ask for more characters than are present in the string. It just returns as many as are available.

Learning T-SQL

It’s worth your while becoming proficient in SQL. If you’d like to learn a lot about T-SQL in a hurry, our Writing T-SQL Queries for SQL Server course is online, on-demand, and low cost.

2019-12-02