Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

I want to create a .net equivalent function in sql server. .Net function is as follows
C#
private string getEligibility(int? elig, string overridden)
        {
            string ret = elig == 0 ? "Red" : elig == 1 ? "Green" : elig == 2 ? "Yellow" : elig == 3 ? "Purple" : elig == 4 ? "P" : elig == 5 ? "NA" : elig == 6 ? "PB" : elig == 7 ? "EX" : elig == 8 ? "PA" : elig.ToString();
            if (!string.IsNullOrEmpty(overridden) && overridden == "Y")
            {
                ret += "^";
            }
            return ret;
        }


Same like above I need to create in sql server.
I have tried as below but only getting eligibility. I also need to check its overridden value and then need to return final value. Bold part of .net code I am not able to write in sql server.

What I have tried:

create function [dbo].[udfgetEligibility]
(
@eligibilty as int
)
RETURNS varchar (100)

as
begin
return CASE
WHEN @eligibilty = 0 THEN 'RED'
WHEN @eligibilty = 1 THEN 'GREEN'
WHEN @eligibilty = 2 then 'YELLOW'
WHEN @eligibilty = 3 then 'Purple'
WHEN @eligibilty = 4 then 'P'
WHEN @eligibilty = 5 then 'NA'
WHEN @eligibilty = 6 then 'PB'
WHEN @eligibilty = 7 then 'EX'
WHEN @eligibilty = 8 then 'PA'
-- etc...
ELSE CONVERT(varchar(10), @eligibilty)
end
end
Posted
Updated 20-Aug-17 22:04pm

1 solution

I'd set up a table, to do it rather than using CASE:
ID	Description
0	RED       
1	GREEN     
2	YELLOW
...

Then it's pretty trivial:
SQL
SELECT ISNULL((SELECT Description FROM Mappings WHERE ID = @eligibilty), @eligibilty)
 
Share this answer
 
Comments
Telstra 21-Aug-17 4:56am    
Hi,
Thanks for response. As written in .Net code it would first take the eligibility value and then also checking for its overridden value. Finally returning the string value. In my way or your way its taking a 0,1,2,.. and so on and then returning it in colour. Once I get the color,need to check its overridden value and then final value should get returned.
OriginalGriff 21-Aug-17 5:25am    
So reverse it! The table works both ways, so swap the SELECT and WHERE criteria.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900