Tách dữ liệu của một cột thành nhiều cột

Làm sạch dữ liệu trong cơ sở dữ liệu SQL Server

Miriam Antona

Software Engineer

Tách sản phẩm và số lượng

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         |
| ...                | ...          |
Làm sạch dữ liệu trong cơ sở dữ liệu SQL Server

Dùng SUBSTRING và CHARINDEX

| product_name_units |
|--------------------|
| notebooks-150      |
| product_name | units |
|--------------|-------|
| notebooks    | 150   |
SUBSTRING(string, start, length)
CHARINDEX(substring, string [,start])
Làm sạch dữ liệu trong cơ sở dữ liệu SQL Server

Dùng SUBSTRING và CHARINDEX

SELECT SUBSTRING ('notebooks-150', 1, CHARINDEX('-', 'notebooks-150') - 1) AS product_name
Làm sạch dữ liệu trong cơ sở dữ liệu SQL Server

Dùng SUBSTRING và CHARINDEX

SELECT SUBSTRING ('notebooks-150', 1, 9) AS product_name
| product_name |
|--------------|
| notebooks    |
Làm sạch dữ liệu trong cơ sở dữ liệu SQL Server

Dùng SUBSTRING và CHARINDEX

SELECT CAST(
    SUBSTRING('notebooks-150', CHARINDEX('-', 'notebooks-150') + 1, LEN('notebooks-150')) 
    AS INT) units
Làm sạch dữ liệu trong cơ sở dữ liệu SQL Server

Dùng SUBSTRING và CHARINDEX

SELECT CAST(
    SUBSTRING('notebooks-150', 11, LEN('notebooks-150')) 
    AS INT) units
Làm sạch dữ liệu trong cơ sở dữ liệu SQL Server

Dùng SUBSTRING và CHARINDEX

SELECT CAST(
    SUBSTRING('notebooks-150', 11, 13) 
    AS INT) units
| units |
|-------|
| 150   |
Làm sạch dữ liệu trong cơ sở dữ liệu SQL Server

Dùng SUBSTRING và 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   |
Làm sạch dữ liệu trong cơ sở dữ liệu SQL Server

Dùng LEFT, RIGHT và REVERSE

LEFT(string, number_of_chars)
  • Lấy số ký tự từ bên trái của chuỗi đã cho
RIGHT(string, number_of_chars)
  • Lấy số ký tự từ bên phải của chuỗi đã cho
REVERSE(string_expression)
  • Đảo ngược chuỗi
Làm sạch dữ liệu trong cơ sở dữ liệu SQL Server

Dùng LEFT, RIGHT và REVERSE

SELECT
    LEFT('notebooks-150', CHARINDEX('-', 'notebooks-150') - 1) AS product_name,
    RIGHT('notebooks-150', CHARINDEX('-', REVERSE('notebooks-150')) - 1) AS units
Làm sạch dữ liệu trong cơ sở dữ liệu SQL Server

Dùng LEFT, RIGHT và REVERSE

SELECT
    LEFT('notebooks-150', 9) AS product_name,
    RIGHT('notebooks-150', CHARINDEX('-', REVERSE('notebooks-150')) - 1) AS units
Làm sạch dữ liệu trong cơ sở dữ liệu SQL Server

Dùng LEFT, RIGHT và REVERSE

SELECT
    LEFT('notebooks-150', 9) AS product_name,
    RIGHT('notebooks-150', 4 - 1) AS units
Làm sạch dữ liệu trong cơ sở dữ liệu SQL Server

Dùng LEFT, RIGHT và REVERSE

SELECT
    LEFT('notebooks-150', 9) AS product_name,
    RIGHT('notebooks-150', 3) AS units
| product_name | units |
|--------------|-------|
| notebooks    | 150   |
Làm sạch dữ liệu trong cơ sở dữ liệu SQL Server

Ayo berlatih!

Làm sạch dữ liệu trong cơ sở dữ liệu SQL Server

Preparing Video For Download...