String Functions

SQL comes with many built-in functions for reading, writing, updating, and generally manipulating data in any way you see fit. The examples below utilize the SELECT statement to work with strings, allowing us to practice using these functions by only reading the data initially. A major benefit of this approach is that it provides a visual of what would happen if you decide to manipulate the same data.

String Functions

Below, you will find some of the more common SQL string functions that we will focus on in this class. For a more exhaustive list of string functions, you can visit SQL String Functions .

RTRIM

RTRIM removes whitespaces from the right of the last character.

Example
SELECT RTRIM('Too many trailing spaces.     ');

Value Returned

Too many extra spaces.

LTRIM

LTRIM removes whitespaces from the left of the first character.

Example
SELECT LTRIM('         Leading spaces');

Value Returned

Leading spaces

LEFT

LEFT returns the length of characters starting at the provided character index and moving left.

Example
SELECT LEFT('hello there!', 7);

Value Returned

hello t

RIGHT returns the length of characters starting at the provided character index and moving right.

Example
SELECT RIGHT('hello there!', 3);

Value Returned

re!

LEN

LEN returns the length of a string based on characters, not including trailing spaces.

Example
SELECT LEN('hello there!'');

Value Returned

12

DATALENGTH

DATALENGTH returns the length of a string based on bytes, including trailing spaces.

Example
SELECT DATALENGTH('hello there!  ')

Value Returned

14

CHARINDEX

CHARINDEX can use to find a specific character within a string and returns it’s index location.

Example
SELECT CHARINDEX('log', 'catalogue');

Value Returned

5

SUBSTRING

SUBSTRING returns part of a string. The first number is the starting index location and the second number is the ending index location.

Example
SELECT SUBSTRING('Strings are fun!', 4, 9);

Value Returned

ings are

REVERSE

REVERSE returns the provided string backwards.

Example
SELECT REVERSE('Data Analysis');

Values Returned

sisylanA ataD

UPPER

UPPER returns a string in all upper case letters.

Example
SELECT UPPER('taco');

Value Returned

TACO

LOWER

LOWER returns a string in all lower case letters.

Example
SELECT LOWER('tACO');

Value Returned

taco

REPLACE

REPLACE replaces part of a string using the provided patterns.

Example
SELECT REPLACE('Beach Streat', 'ea', 'ee');

Value Returned

Beech Street

CONCAT

CONCAT is commonly used to combine strings together and work with null values.

Examples
SELECT CONCAT('Alyce','Cat', 'Frey');
SELECT CONCAT('Alyce', NULL, 'Frey');

Values Returned

-- Example 1 output:
AlyceCatFrey

-- Null example output:
AlyceFrey

CONCAT_WS

CONCAT_WS allows you to combine strings together with a specified separator value. The separator can be anything you want.

Examples
SELECT CONCAT_WS(' =::= ',  'Alyce', 'Frey');

Works with NULL values similar to CONCAT.

SELECT CONCAT_WS('  -  ',  'Alyce', NULL, 'Frey');

Values Returned

-- Example 1 output:
Alyce=::=Frey
-- Null example output:
Alyce - Frey

STUFF

STUFF inserts a string into another string. The first number indicates where to insert the new characters.

Examples
SELECT STUFF('Pumpkin Pie', 3, 0, 'Add Chars');

The second number indicates how many original characters will be deleted upon insertion.

SELECT STUFF('Pumpkin Pie', 3, 5, 'Delete Chars');

Values Returned

-- Example 1 Output:
PuAdd Charsmpkin Pie
-- Example 2 Output:
PuDelete Chars Pie

Check Your Understanding

Question

Alyce is working with a dataset that contains information about local businesses. They want to create a query that returns titles of local businesses that contain “&”. Which string function should they use?

  1. LEN
  2. STUFF
  3. CHARINDEX
  4. RTRIM
Question

Alyce wants to create consistency in the formatting of the state abbreviations of the local business addresses contained in their dataset. What string function could help with this?

  1. REVERSE
  2. UPPER
  3. CONCAT
  4. LEFT
Question

Alyce’s dataset breaks down the addresses of local businesses into the following columns: "Street", "City", "State", and "Zipcode". They would like to create a column that has all items joined to return a complete address in a single column. Which function would best help with this?

  1. CONCAT or CONCAT_WS or STUFF
  2. LOWER
  3. REPLACE
  4. LEFT
Question

Alyce is working with a column of phone numbers. They only need the 7 digit number, not the area code. However, some of the numbers include the area code, some include the country code and area code, and some only include the seven-digit phone number. Which function could help them select the 7 digits that she needs?

  1. CONCAT_WS
  2. CHARINDEX
  3. RIGHT
  4. RTRIM