Splitting data of one column into more columns

Cleaning Data in SQL Server Databases

Miriam Antona

Software Engineer

Splitting products and units

paper_shop_monthly_sales
| product_name_units | year_of_sale |
|--------------------|--------------|
| notebooks-150      | 2018         |
| notebooks-200      | 2019         |
| notebooks-30       | 2019         |
| pencils-100        | 2018         |
| pencils-50         | 2018         |
| pencils-130        | 2019         |
| crayons-80         | 2018         |
| ...                | ...          |
Cleaning Data in SQL Server Databases

Using SUBSTRING and CHARINDEX

| product_name_units |
|--------------------|
| notebooks-150      |
| product_name | units |
|--------------|-------|
| notebooks    | 150   |
SUBSTRING(string, start, length)
CHARINDEX(substring, string [,start])
Cleaning Data in SQL Server Databases

Using SUBSTRING and CHARINDEX

SELECT SUBSTRING ('notebooks-150', 1, CHARINDEX('-', 'notebooks-150') - 1) AS product_name
Cleaning Data in SQL Server Databases

Using SUBSTRING and CHARINDEX

SELECT SUBSTRING ('notebooks-150', 1, 9) AS product_name
| product_name |
|--------------|
| notebooks    |
Cleaning Data in SQL Server Databases

Using SUBSTRING and CHARINDEX

SELECT CAST(
    SUBSTRING('notebooks-150', CHARINDEX('-', 'notebooks-150') + 1, LEN('notebooks-150')) 
    AS INT) units
Cleaning Data in SQL Server Databases

Using SUBSTRING and CHARINDEX

SELECT CAST(
    SUBSTRING('notebooks-150', 11, LEN('notebooks-150')) 
    AS INT) units
Cleaning Data in SQL Server Databases

Using SUBSTRING and CHARINDEX

SELECT CAST(
    SUBSTRING('notebooks-150', 11, 13) 
    AS INT) units
| units |
|-------|
| 150   |
Cleaning Data in SQL Server Databases

Using SUBSTRING and CHARINDEX

SELECT 
    SUBSTRING('notebooks-150', 1, CHARINDEX('-', 'notebooks-150') - 1) product_name, 
    CAST
        (SUBSTRING('notebooks-150', CHARINDEX('-', 'notebooks-150') + 1, LEN('notebooks-150')) 
        AS INT) units
| product_name | units |
|--------------|-------|
| notebooks    | 150   |
Cleaning Data in SQL Server Databases

Using LEFT, RIGHT and REVERSE

LEFT(string, number_of_chars)
  • Gets a number of characters from the left of a given string
RIGHT(string, number_of_chars)
  • Gets a number of characters from the right of a given string
REVERSE(string_expression)
  • Reverses a string
Cleaning Data in SQL Server Databases

Using LEFT, RIGHT and REVERSE

SELECT
    LEFT('notebooks-150', CHARINDEX('-', 'notebooks-150') - 1) AS product_name,
    RIGHT('notebooks-150', CHARINDEX('-', REVERSE('notebooks-150')) - 1) AS units
Cleaning Data in SQL Server Databases

Using LEFT, RIGHT and REVERSE

SELECT
    LEFT('notebooks-150', 9) AS product_name,
    RIGHT('notebooks-150', CHARINDEX('-', REVERSE('notebooks-150')) - 1) AS units
Cleaning Data in SQL Server Databases

Using LEFT, RIGHT and REVERSE

SELECT
    LEFT('notebooks-150', 9) AS product_name,
    RIGHT('notebooks-150', 4 - 1) AS units
Cleaning Data in SQL Server Databases

Using LEFT, RIGHT and REVERSE

SELECT
    LEFT('notebooks-150', 9) AS product_name,
    RIGHT('notebooks-150', 3) AS units
| product_name | units |
|--------------|-------|
| notebooks    | 150   |
Cleaning Data in SQL Server Databases

Let's practice!

Cleaning Data in SQL Server Databases

Preparing Video For Download...