CASE Operator:
Scenario:
We used face a regular scenario to update the values based on the data available in the table. For this kind of situation, we can go for CASE operator.
Sytax:
Case
when expresssion or column1 = value1 Then assign this value to column1
when expresssion or column1 = value2 Then assign this value to column1
end
Example:
Am trying to create a table,
drop table venkat
create table venkat(id int, nam varchar(10))
insert into venkat values (1,'a')
insert into venkat values (2,'aa')
insert into venkat values (3,'aaa')
select * from venkat
select * from
(
select idval =
case id
when 1 then '1a'
when 2 then '2a'
when 3 then '3a'
end
from venkat
)tbl
drop table venkat
create table venkat(id int, nam varchar(10))
insert into venkat values (1,'a')
insert into venkat values (2,'aa')
insert into venkat values (3,'aaa')
select * from venkat
select * from
(
select idval =
case id
when 1 then '1a'
when 2 then '2a'
when 3 then '3a'
end
from venkat
)tbl
The output resembles,

Thanks and Regards,
Venkatesan Prabu .J
No comments:
Post a Comment