I am writing a select case statement for a complex If Then Else statement where the logic is
If (wp.LINE_OF_BUSINESS = 'A' or wp.LINE_OF_BUSINESS = 'U') and (wr.Relate_Code = 'in') then bf.age
else if (wp.LINE_OF_BUSINESS = 'A' or wp.LINE_OF_BUSINESS = 'U') and (wr.Relate_Code = 'je' or wr.Relate_Code = 'ji') then then bf.age2
else ba.age
how can I write this logic in case when then statement?
Thanks
Try this
CASE WHEN wp.LINE_OF_BUSINESS IN ( 'A' , 'U') and wr.Relate_Code = 'in' THEN bf.age
WHEN wp.LINE_OF_BUSINESS IN ( 'A' , 'U') and wr.Relate_Code IN ('je', 'ji') THEN bf.age2
ELSE ba.age
END
The cleanest and simplest way is to write a UDF
CREATE FUNCTION myAGE(@bType CHAR, @code CHAR(2), @age1 int, @age2 int) RETURNS Int
AS
BEGIN
If (@bType = 'A' or @bType = 'U') and (@code = 'in') then
return @age1
else if (@bType = 'A' or @bType = 'U') and (@code = 'je' or @code = 'ji') then
return @age2
else
return @age1
END
then simply do
SELECT ..., myAge(wp.LINE_OF_BUSINESS, wr.Relate_Code, bf.age, bf.age2),...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.