JournalSQLFunctions and dates4 min

For Those Who Do Not Know SUBSTR, LEFT, RIGHT, STRPOS, and POSITION

The SUBSTR(), LEFT(), and RIGHT() functions are used to extract parts of text (strings).

  • SUBSTR
  • LEFT
  • RIGHT
  • STRPOS
  • POSITION

Topic Contents

  1. Definition of LEFT() and its usage
  2. Definition of RIGHT() and its usage
  3. Definition of SUBSTR() and its usage (Advanced Material)
  4. Definition of STRPOS() and POSITION()
  5. Using STRPOS() (Advanced Material)

Introduction

The SUBSTR(), LEFT(), and RIGHT() functions are used to extract parts of text (strings).

Text/strings are anything enclosed in quotation marks.

They can contain letters only, like 'SQL'.

They can contain letters and numbers, like 'SQL123'.

They can contain numbers only, like '1'. In this case, we treat the number 1 as text (a character), not a number, because it is written inside quotation marks as we said. Therefore, we cannot perform arithmetic operations on it (such as addition, multiplication, or division).

Why Do We Need SUBSTR(), LEFT(), and RIGHT()?

These functions are very famous for handling text, especially SUBSTR, which exists in many programming languages.

The goal of using them in SQL is to clean data and fix data that has incorrect values. To understand more about data cleaning, check out this topic: https://macdiscussions.udacity.com/t/data-cleaning/128179/1

What Is LEFT()?

The LEFT() function extracts a part of the text from the left side.

Its syntax is:

LEFT(text, n)

Where n is the number of characters we take from the left side.

Using LEFT()

If we want to extract a word at the beginning of the text like 'One', we write:

SELECT LEFT('One Million Arab Coders', 3)

The output will be:

One

Because we extracted the first 3 characters from the left.

What Is RIGHT()?

RIGHT() is the same idea as LEFT(), except we extract a part of the text from the right side.

Its syntax is:

RIGHT(text, n)

Where n represents the number of characters we take from the end of the text on the right (not from the beginning).

Using RIGHT()

If we want to extract a word at the end of the text like 'Coders', we write:

SELECT RIGHT('One Million Arab Coders', 6)

The expected output will be:

Coders

What Is SUBSTR()?

The SUBSTR() function is more comprehensive than LEFT() and RIGHT(). For example, if you noticed, LEFT() and RIGHT() cannot extract a part in the middle of the text, whereas SUBSTR() can 馃挭

Its syntax (according to the documentation) is:

SUBSTR(string, from [, count])

Where from is the starting position from which we want to extract the text. If count is omitted, it will extract the text from the starting position we specified until the end of the text.

What is in brackets [, count] is optional鈥攎eaning we can include it or omit it. If we include it, we don't want to extract to the end; instead, we specify a certain number of characters to extract.

We'll see this in the examples, don't worry 馃檪

Using SUBSTR()

If we want to extract a word that is neither at the end nor at the beginning of the text, like 'Arab', we need to know which position the starting letter is at (the character index) and how many characters the word we want to extract consists of.

As you can see in the image, indexing starts at 1 (and spaces are counted too):

Using SUBSTR()

So we write:

SELECT SUBSTR('One Million Arab Coders', 13, 4)
Using SUBSTR()

Because the length of the word 'Arab' we want to extract is 4 characters, and the letter 'A' starts at position 13 in the text. So we are all set, and the output will be:

Arab

Okay, it's easy now! Let's extract the word 'Million'. We write:

SELECT SUBSTR('One Million Arab Coders', 5, 7)
Using SUBSTR()

I put 5 for from because the letter 'M' starts at that position, and 7 for count because 'Million' consists of 7 characters. The output:

Million

We use these to find the exact position of a character or substring.

This is the syntax of STRPOS() (according to the documentation):

STRPOS(text, substring)

Where substring can be one character or more鈥攎eaning we can write part of a word (made of multiple characters).

And this is the syntax of POSITION() (according to the documentation):

POSITION(substring IN text)

Using STRPOS()

If we want to find the position of the first space in the text, we write:

SELECT STRPOS('One Million Arab Coders', ' ')
Using STRPOS()

The output will be:

4

Now, what if we don't want the first space鈥攚hat if I ask you to find the second space? I will show you how, and don't worry, I'll explain it step by step in detail:

SELECT STRPOS(
              SUBSTR(
                     'One Million Arab Coders', STRPOS('One Million Arab Coders', ' ') + 1
              ), ' '
       )
       + STRPOS('One Million Arab Coders', ' ')
Using STRPOS()

The expected output (which is the position of the second space) is:

12

Because I found the position of the first space when I wrote:

STRPOS('One Million Arab Coders', ' ')

Which is 4, and I added 1 to that number so I don't include that space when I use SUBSTR to slice the text starting right after the space:

SUBSTR('One Million Arab Coders', STRPOS('One Million Arab Coders', ' ') + 1)
Using STRPOS()

As a result, I got the text:

Million Arab Coders

I will treat it as a new string and find the first space in it:

STRPOS(SUBSTR('One Million Arab Coders', STRPOS('One Million Arab Coders', ' ') + 1), ' ')

It will return 8 relative to the sliced text 'Million Arab Coders'. Then I add back the number of characters I excluded ('One ' including the space), which is 4 characters:

STRPOS(SUBSTR('One Million Arab Coders', STRPOS('One Million Arab Coders', ' ') + 1), ' ') + STRPOS('One Million Arab Coders', ' ')

I know it looks a bit big at first, but if you break it down, read the explanation, and go step by step, you will find it easy.

Summary

  1. LEFT() extracts a number of characters from the left side.
  2. RIGHT() extracts a number of characters from the right side.
  3. STRPOS() and POSITION() find the positions of characters/substrings.