Cum poți folosi in SQL comenzile SUBSTRING si CASE pentru SELECT

Configurare noua (How To)

Situatie

Cum poți folosi in SQL comenzile SUBSTRING si CASE pentru SELECT?

 

In general pentru o raportare care nu oferă concret toate datele si nu poți folosi doar data dintr-o coloana pentru a clasifica sau a filtra.

CASE –  este o funcție prin care se pot genera datele conform celor 3 componente principale:

  • WHEN(atunci când), se aplica o căutare, o filtrare, o condiție pentru datele unei coloane sau al mai multor coloane;
  • THEN(atunci), se declara ce trebuie sa fie afișat, fie un text sau o alta coloana;

SUBTRING – este o funcție de conversie al textului si de trunchiere pentru a folosi doar o parte din textul din coloana;

Solutie

Exemplul:

Avem următorul script de SQL pentru SELECT:

SELECTSUBSTRING(NUME,1,1) + ‘.’ AS ITINIALA_NUME,

PRENUME,

ADRESA

FROM CUST01

 

 

Se vor afișa următoarele date:

 

ITINIALA_NUME NUME Sector adresa
S. SAVA PROVINCIE STR. DE PE DEAL, BACAU,SAT. FILIPESTI
P. POPESCU S1 Bucuresti, Sector 1 , STR. EMINESCU
C. CIPRIAN S1 Bucuresti, Sector 1 , STR. TURTURELELOR
C. Ciorogariu S2 Bucuresti, Sector 2 , Str. Ciocarliei nr.24
C. CIOROGARIU S2 Bucuresti, Sector 2 , STR. CIOCARILEI
S. STEFAN S2 Bucuresti, Sector 2 , STR. DELFINULUI
S. STEFAN S2 Bucuresti, Sector 2 , STR. DELFINARIULUI
D. DANILA S2 Bucuresti, Sector 2 , STR. Potarnichi
P. POPESCU S3 Bucuresti, Sector 3 , STR. VRABIEI
S. SUCIU S4 Bucuresti, Sector 4 , STR. CORABIEI
A. ANDREI S4 Bucuresti, Sector 4 , STR. PLOPILOR
I. IONESCU S5 Bucuresti, Sector 5 , STR. SEBASTIAN
I. IONESCU S5 Bucuresti, Sector 5 , STR. DECEBAL
I. IONESCU S5 Bucuresti, Sector 5 , STR. DECEBAL
I. IONESCU S5 Bucuresti, Sector 5 , STR. DECEBAL
P. POPESCU S6 Bucuresti, Sector 6 , STR. ION CREANGA

 

Se creează următorul script pentru filtrarea si clasificare datelor cu ajutorul CASE si SUBSTRING:

 

SELECTSUBSTRING(NUME,1,1) + ‘.’ AS ITINIALA_NUME,

NUME,

‘Sector’ =

case  –// se initieaza case apoi se dau parametri

when adresa like ‘%Sector 1%’ then ‘S1’–// se caută la coloana adresa textul care are forma de Sector 1

when adresa like ‘%Sector 2%’ then ‘S2’

when adresa like ‘%Sector 3%’ then ‘S3’

when adresa like ‘%Sector 4%’ then ‘S4’

when adresa like ‘%Sector 5%’ then ‘S5’

when adresa like ‘%Sector 6%’ then ‘S6’

ELSE ‘PROVINCIE’ –// se folosește ELSE pentru cazul in care datele nu sunt găsite de parametri

end, –// finalizarea comenzi CASE

adresa

FROM CUST01

order by Sector –//ordonarea

asc –// tipul ordonări ascendent

 

 

 

Se vor afișa următoarele date:

ITINIALA_NUME NUME Sector adresa
S. SAVA PROVINCIE STR. DE PE DEAL, BACAU,SAT. FILIPESTI
P. POPESCU S1 Bucuresti, Sector 1 , STR. EMINESCU
C. CIPRIAN S1 Bucuresti, Sector 1 , STR. TURTURELELOR
C. Ciorogariu S2 Bucuresti, Sector 2 , Str. Ciocarliei nr.24
C. CIOROGARIU S2 Bucuresti, Sector 2 , STR. CIOCARILEI
S. STEFAN S2 Bucuresti, Sector 2 , STR. DELFINULUI
S. STEFAN S2 Bucuresti, Sector 2 , STR. DELFINARIULUI
D. DANILA S2 Bucuresti, Sector 2 , STR. Potarnichi
P. POPESCU S3 Bucuresti, Sector 3 , STR. VRABIEI
S. SUCIU S4 Bucuresti, Sector 4 , STR. CORABIEI
A. ANDREI S4 Bucuresti, Sector 4 , STR. PLOPILOR
I. IONESCU S5 Bucuresti, Sector 5 , STR. SEBASTIAN
I. IONESCU S5 Bucuresti, Sector 5 , STR. DECEBAL
I. IONESCU S5 Bucuresti, Sector 5 , STR. DECEBAL
I. IONESCU S5 Bucuresti, Sector 5 , STR. DECEBAL
P. POPESCU S6 Bucuresti, Sector 6 , STR. ION CREANGA

 

 

Tip solutie

Permanent

Voteaza

(21 din 52 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?