Skip to content

SQL Case When Statement

The switch used in the CASE function programming languages ​​is similar to the Swtich Case, Select Case structures. Actually, we can say the same thing. Of course, the decision structure here does not lead to a conclusion, but the conditions that provide any condition allows us to show the way we want. In SQL, the CASE function is used in two ways. These are called simple Case and searched case.

Name Control:

SELECT DISTINCT Name,
CASE Name
	WHEN 'furkan' THEN 'I'
	WHEN 'osman'  THEN 'Father'
	WHEN 'zeynep' THEN 'My girlfriend'
	WHEN 'fatma' THEN 'Mother'
	ELSE 'I dont know'
END as info
FROM personel.dbo.Personeller
ORDER BY Name asc;

Age Control

SELECT DISTINCT Age,
CASE
	WHEN Age< 20  THEN 'Young'
	WHEN 20 < Age and Age < 30  THEN 'Young Adult'
	WHEN 30 < Age and Age < 50  THEN 'Adult'
	WHEN 50 < Age THEN 'Old'
	ELSE 'Hımmm'
END as info
FROM personel.dbo.Personeller
ORDER BY Age desc;

SQL TABLE

NameSurnameIDHometownAge
Ali            kaya           1254sinop          21
mehmet         nazmi          1247artvin         26
osman          yazar          1578hakkari        35
fatma          gündogdu       8972ankara         19
irem           yazar          4574yalova         23
idil           kirmizi        1258antalya        25
furkan         gulsen         1453sinop          21
zeynep         gozhan         1881yozgat         19
suheyda        kaya           3647izmir          28
galip          kose           8362ankara         24
mehmet         kucuk          2617konya          22
nazmi          kucuk          8595mardin         25
ayse           gul            1546ankara         26
ertugrul       gazi           1884bursa          32
zeynep         kalin          4768izmir          27
sumeyra        gulsen         5264sinop          31
muhammed       aski           4578istanbul       24
burkay         yazici         8195artvin         42
buray          testereci      2563ankara         35
seren          gelin          6985mardin         41
osman          topaloglu      9274hakkari        55
pelin          semt           1048usak           49
kubra          buyuran        4892izmir          38
samet          yesildag       7382sivas          39
selin          ozkan          3894antalya        33
samet          kalin          4828edirne         28
sabri          isik           5848trabzon        45
mahmut         tuncer         4838mardin         55
busra          kiraz          9205agri           52
huseyin        manti          6392rize           39
huseyin        abile          2045hatay          18
kemal          yanardag       4983canakkale      32
yamac          kosavalı       7282istanbul       26
vartolu        sadattin       4930bursa          23
mehmet         kirbiyik       9398sanliurfa      21
teker          selim          2102mardin         36
busra          belil          7420konya          27
nazmiye        kilic          5829balikesir      18
yavuz          karaselim      8529canakkale      37
selim          figen          2791artvin         62
ekrem          bola           8532edirne         36
mehmet         kamil          1939sivas          24
kamil          sedet          3953yozgat         31
yavuz          karasu         7420konya          45
ibrahim        kaptan         2350canakkale      39
nurgul         kizil          4930sivas          23

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: