bux
bux

Reputation: 45

How to make query statement case in variable SQL Server

This works:

select 
    idpelamar,
    case 
       when jk = 'L' then 'Laki' 
       when jk = 'P' then 'Perem' 
    end as XJENIS
from tblpelamar

How to apply the case statement to initialize a variable?

Like this below:

declare @jenis nvarchar(1)
declare @perintah nvarchar(100)
set @jenis = 'L'

set @perintah = 'case when jk=@jenis then Laki when jk=@jenis then Prem end as XJENIS'

select 
    idpelamar,
    +@perintah
from tblpelamar

Upvotes: 0

Views: 60

Answers (1)

Sander
Sander

Reputation: 4042

Does this answer your question?

declare @jenis1 as nvarchar(1) = 'L';
declare @jenis2 as nvarchar(1) = 'P';

select idpelamar,
       case jk
         when @jenis1 then 'Laki'
         when @jenis2 then 'Prem'
       end as XJENIS
from tblpelamar;

Fiddle

Upvotes: 2

Related Questions