Reputation: 532
consider the following stored procedure :
alter procedure getRisks @id int, @convertedRiskValue int
as
select top 1 table1.id, idRisk, riskValue from table1
left join table2
on table1.id = table2.id
where table1.id= @id
order by table2.id desc
The idRisk
value will always vary from 1-4 and the riskValue
will vary from 0-1000 .
What i want to do ?
riskValue
to idRisk
"type" as follows :, then put it into @convertedRiskValue
and return it from the select statement.
Any ideas ?
Upvotes: 0
Views: 48
Reputation: 34
Try This :
alter procedure getRisks @id int, @convertedRiskValue int
as
select top 1 table1.id, CASE WHEN RiskValue >= 0 AND RiskValue <= 250 THEN 1
WHEN RiskValue >= 251 and RiskValue <= 500 THEN 2
WHEN RiskValue >= 501 and RiskValue <= 750 THEN 3
WHEN RiskValue >= 751 and RiskValue <= 1000 THEN 4 end as idRiskType
, riskValue from table1
left join table2
on table1.id = table2.id
where table1.id= @id
order by table2.id desc
Upvotes: 1
Reputation: 2930
SELECT
riskValue,
CASE
WHEN riskValue > -1 AND riskValue < 251 THEN 1
WHEN riskValue > 250 AND riskValue < 501 THEN 2
WHEN riskValue > 500 AND riskValue < 751 THEN 3
WHEN riskValue > 750 AND riskValue < 1001 THEN 4
END AS idRisk
FROM Table
Upvotes: 1