SQL LIKE

L'operatore LIKE viene utilizzato con la clausola WHERE per ottenere un set di risultati che corrisponda al modello di stringa specificata. Per esempio:

        SELECT *
            FROM customers
            WHERE country LIKE 'UK';
    

SQL NOT LIKE

Possiamo invertire il funzionamento dell'operatore LIKE ed ignorare il set di risultati che corrispondono allo schema di stringa specificato utilizzando l'operatore NOT. Per esempio:

        SELECT *
            FROM customers
            WHERE country NOT LIKE 'USA';
    

LIKE CON CARATTERI JOLLY

L'operatore LIKE viene spesso utilizzato con i caratteri jolly per abbinare un modello di stringa. Un carattere jolly viene utilizzato per sostituire un singolo o un insieme di caratteri in qualsiasi stringa. Per esempio:

        SELECT *
            FROM customers
            WHERE surname LIKE 'R%';
    

CARATTERE JOLLY %

Il carattere jolly % viene utilizzato per rappresentare zero o più caratteri. Per esempio:

        SELECT *
            FROM customers
            WHERE surname LIKE 'R%';
    
stringa matched?
R match
Run match
Mere no match
Summer no match

CARATTERE JOLLY _

Il carattere jolly _ viene utilizzato per rappresentare esattamente un carattere in una stringa. Per esempio:

        SELECT *
            FROM customers
            WHERE surname LIKE 'U_';
    
stringa matched?
U no match
UK match
USA no match

CARATTERE JOLLY []

Il carattere jolly [] viene utilizzato per rappresentare qualsiasi carattere tra parentesi. Per esempio:

        SELECT *
            FROM customers
            WHERE country LIKE 'U[KA]%';
    
stringa matched?
U no match
UK match
UAE match
USA no match

CARATTERE JOLLY !

Il carattere jolly ! viene utilizzato per escludere i carattere da una stringa. Per esempio:

        SELECT *
            FROM customers
            WHERE country LIKE '[!DR]%';
    
stringa matched?
Doe no match
Reinhardt no match
Luna match
D no match
O match
R no match