Corrispondenza di pattern

Pulizia dei dati nei database SQL Server

Miriam Antona

Software Engineer

Corrispondenza di pattern - Introduzione

SELECT * FROM series
| id  | name            | contact_number | ... |
|-----|-----------------|----------------|-----|
| 1   | Adventure Time  | 555-906-8845   | ... |
| 2   | Dexter          | 555-156-8845   | ... |
| 3   | Futurama        | 555-210-9951   | ... |
| 4   | Game of Thrones | 555-543-6641   | ... |
| ... | ...             | ...            | ... |
  • Numeri validi: ###-###-####
    • primo e quarto numero tra 2 e 9
    • gli altri tra 0 e 9
  • Numero non valido: 555-156-8845
Pulizia dei dati nei database SQL Server

Corrispondenza di pattern - Introduzione

  • SQL Server non offre un set completo di espressioni regolari.
  • SQL Server può confrontare pattern con LIKE.
  • Per un set completo di espressioni regolari -> crea e installa estensioni.
Pulizia dei dati nei database SQL Server

Corrispondenza di pattern - LIKE

  • Verifica se una stringa corrisponde a un pattern specifico
Pulizia dei dati nei database SQL Server

Corrispondenza di pattern - LIKE

  • Verifica se una stringa corrisponde a un pattern specifico
Carattere jolly Descrizione Esempio
% Qualsiasi stringa di zero o più caratteri WHERE contact_number LIKE '555-%'
_ (underscore) Un singolo carattere WHERE contact_number LIKE '___-___-____'
[] Un singolo carattere nell'intervallo o set dato WHERE contact_number LIKE '[2-9][0-9][0-9]-[2-9][0-9][0-9]-[0-9][0-9][0-9][0-9]
[^] Un singolo carattere fuori dall'intervallo/set WHERE contact_number LIKE '[^2-9]'
Pulizia dei dati nei database SQL Server

Corrispondenza di pattern - esempio con %

SELECT name, contact_number
FROM series
WHERE contact_number LIKE '555%'
| name            | contact_number |
|-----------------|----------------|
| Adventure Time  | 555-906-8845   |
| Dexter          | 555-156-8845   |
| Futurama        | 555-210-9951   |
| Game of Thrones | 555-abc-6641   |
| ...             | ...            |
Pulizia dei dati nei database SQL Server

Corrispondenza di pattern - esempio con %

SELECT 
    name, 
    contact_number
FROM series
WHERE contact_number NOT LIKE '555%'
| name            | contact_number |
|-----------------|----------------|
| The Good Doctor | 000-930-1274   |
Pulizia dei dati nei database SQL Server

Corrispondenza di pattern - esempio con [] (parentesi)

SELECT 
    name, 
    contact_number
FROM series
WHERE contact_number LIKE '[2-9][0-9][0-9]-[2-9][0-9][0-9]-[0-9][0-9][0-9][0-9]'
| name           | contact_number |
|----------------|----------------|
| Adventure Time | 555-906-8845   |
| Futurama       | 555-210-9951   |
| Homeland       | 555-985-6314   |
| Westworld      | 555-456-1234   |
| ...            | ...            |
Pulizia dei dati nei database SQL Server

Corrispondenza di pattern - esempio con [] (parentesi)

SELECT 
    name, 
    contact_number
FROM series
WHERE contact_number NOT LIKE '[2-9][0-9][0-9]-[2-9][0-9][0-9]-[0-9][0-9][0-9][0-9]'
| name            | contact_number |
|-----------------|----------------|
| Dexter          | 555-156-8845   |
| Game of Thrones | 555-abc-6641   |
| The Good Doctor | 000-930-1274   |
Pulizia dei dati nei database SQL Server

Ayo berlatih!

Pulizia dei dati nei database SQL Server

Preparing Video For Download...