Strings are one of the most common data types and can be difficult to work with. In this tutorial, we will go over some basic string manipulation techniques found in most RDBMSs. This tutorial will cover the following learning objectives:
In SQL, String Concatenation is the process of combining two or more columns of data type "VARCHAR" or "CHAR" to create a more comprehensible group of data in a single column. Watch the following video to get a better understanding of this concept:
SELECT [col1] || ' ' || [col2] AS [alias]
FROM [table_name];
SELECT CONCAT([col1], ' ', [col2]) AS [alias]
FROM [table_name];
SELECT [col1] + ' ' + [col2] AS [alias]
FROM [table_name];
When presenting query results to stakeholders in a tabular format, it helps to format the strings the way they were designed to be presented. Watch the following video to get a better understanding of this concept:
SELECT LOWER([col1]) AS [alias], LOWER([col2]) AS [alias]
FROM [table_name];
SELECT UPPER([col1]) AS [alias], UPPER([col2]) AS [alias]
FROM [table_name];
SELECT INITCAP([col1]) AS [alias], INITCAP([col2]) AS [alias]
FROM [table_name];
Although SQL isn't built for automation like other languages such as Python and Java, it does provide some automated features that allow you to quickly manipulate data. Watch the following video to get a better understanding of this concept:
SELECT LEFT([col1], 4) AS [alias], LEFT([col2], 2) AS [alias]
FROM [table_name];
SELECT RIGHT([col1], 4) AS [alias], RIGHT([col2], 6) AS [alias]
FROM [table_name];
When verifying outputs, it helps to present data in a comprehensible format. For example, if you're working with financial data, it may be helpful to add dollar signs to add some context to your result sets. Read the following article to get a better understanding of this concept:
SELECT FORMAT('[new_string] %s', [col_name]) AS [alias]
FROM [table_name];
SELECT FORMAT('|%-10s|', [col_name]) AS [alias]
FROM [table_name];
SELECT FORMAT('%1$s [value], %2$s [value], %1$s [value]', '[string1]', '[string2]') AS [alias]
FROM [table_name];
While the functions we've learned so far in this tutorial cover about 80% of all the string manipulation you'll ever need when querying data, there are some other functions available that cover the other 20%. View the following resources to get a better understanding of this concept:
SELECT SPLIT_PART([col_name], '[delimiter]', [chunk_number]) AS [alias]
FROM [table_name];
SELECT LENGTH([col_name]) AS [alias]
FROM [table_name];
SELECT TRIM([col1]) AS [alias], TRIM([col2]) AS [alias]
FROM [table_name];
SELECT LTRIM([col1]) AS [alias], LTRIM([col2]) AS [alias]
FROM [table_name];
SELECT RTRIM([col1]) AS [alias], RTRIM([col2]) AS [alias]
FROM [table_name];
SELECT REPLACE([col_name], '[old_string]', '[new_string]') AS [alias]
FROM [table_name];
SELECT SUBSTRING([col_name], [start_position], [end_position]) AS [alias]
FROM [table_name];
Congratulations! You just completed the String Functions Tutorial! To help test your knowledge, let's
see how these can be utilized by completeing a few practice problems. This exercise is based on the exercise of
the previous tutorial.
**It's highly recommended that you
complete the exercise outlined in the previous tutorial before beginning this exercise.**
Have any issues with the above exercise? Post your question on Discord!