Data uit één kolom splitsen in meerdere kolommen

Gegevens opschonen in SQL Server-databases

Miriam Antona

Software Engineer

Producten en aantallen splitsen

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         |
| ...                | ...          |
Gegevens opschonen in SQL Server-databases

SUBSTRING en CHARINDEX gebruiken

| product_name_units |
|--------------------|
| notebooks-150      |
| product_name | units |
|--------------|-------|
| notebooks    | 150   |
SUBSTRING(string, start, length)
CHARINDEX(substring, string [,start])
Gegevens opschonen in SQL Server-databases

SUBSTRING en CHARINDEX gebruiken

SELECT SUBSTRING ('notebooks-150', 1, CHARINDEX('-', 'notebooks-150') - 1) AS product_name
Gegevens opschonen in SQL Server-databases

SUBSTRING en CHARINDEX gebruiken

SELECT SUBSTRING ('notebooks-150', 1, 9) AS product_name
| product_name |
|--------------|
| notebooks    |
Gegevens opschonen in SQL Server-databases

SUBSTRING en CHARINDEX gebruiken

SELECT CAST(
    SUBSTRING('notebooks-150', CHARINDEX('-', 'notebooks-150') + 1, LEN('notebooks-150')) 
    AS INT) units
Gegevens opschonen in SQL Server-databases

SUBSTRING en CHARINDEX gebruiken

SELECT CAST(
    SUBSTRING('notebooks-150', 11, LEN('notebooks-150')) 
    AS INT) units
Gegevens opschonen in SQL Server-databases

SUBSTRING en CHARINDEX gebruiken

SELECT CAST(
    SUBSTRING('notebooks-150', 11, 13) 
    AS INT) units
| units |
|-------|
| 150   |
Gegevens opschonen in SQL Server-databases

SUBSTRING en CHARINDEX gebruiken

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   |
Gegevens opschonen in SQL Server-databases

LEFT, RIGHT en REVERSE gebruiken

LEFT(string, number_of_chars)
  • Haalt aantal tekens links uit een string
RIGHT(string, number_of_chars)
  • Haalt aantal tekens rechts uit een string
REVERSE(string_expression)
  • Keert een string om
Gegevens opschonen in SQL Server-databases

LEFT, RIGHT en REVERSE gebruiken

SELECT
    LEFT('notebooks-150', CHARINDEX('-', 'notebooks-150') - 1) AS product_name,
    RIGHT('notebooks-150', CHARINDEX('-', REVERSE('notebooks-150')) - 1) AS units
Gegevens opschonen in SQL Server-databases

LEFT, RIGHT en REVERSE gebruiken

SELECT
    LEFT('notebooks-150', 9) AS product_name,
    RIGHT('notebooks-150', CHARINDEX('-', REVERSE('notebooks-150')) - 1) AS units
Gegevens opschonen in SQL Server-databases

LEFT, RIGHT en REVERSE gebruiken

SELECT
    LEFT('notebooks-150', 9) AS product_name,
    RIGHT('notebooks-150', 4 - 1) AS units
Gegevens opschonen in SQL Server-databases

LEFT, RIGHT en REVERSE gebruiken

SELECT
    LEFT('notebooks-150', 9) AS product_name,
    RIGHT('notebooks-150', 3) AS units
| product_name | units |
|--------------|-------|
| notebooks    | 150   |
Gegevens opschonen in SQL Server-databases

Laten we oefenen!

Gegevens opschonen in SQL Server-databases

Preparing Video For Download...