Many programming languages like VB6 support IIF function which is used to return a value based on a particular expression/condition. In SQL Server the equivalent is to make use of CASE Expression, which is used to evaluate many conditions and return a single value. The following examples will give you some ideas on how to use CASE expression effectively declare @t table(emp_id int, emp_name varchar(100), sex char(1))
insert into @t
select 1,'Suresh','M' union all
select 1,'John','M' union all
select 1,'Clara','F' 1 Set Male or Female based on the value of sexselect emp_id,emp_name,
case
when sex='M' then 'Male'
when sex='F' then 'Female'
end as sex
from
@t Result:emp_id emp_name sex
----------- -------------- -----
1 Suresh Male
1 John Male
1 Clara Female Read more: Beyond Relational
insert into @t
select 1,'Suresh','M' union all
select 1,'John','M' union all
select 1,'Clara','F' 1 Set Male or Female based on the value of sexselect emp_id,emp_name,
case
when sex='M' then 'Male'
when sex='F' then 'Female'
end as sex
from
@t Result:emp_id emp_name sex
----------- -------------- -----
1 Suresh Male
1 John Male
1 Clara Female Read more: Beyond Relational
0 comments:
Post a Comment