user1221989 user1221989 - 6 months ago 28
SQL Question

Return multiple data from Case statement condition

I have the below SQl. Right now I only get the return as

<td class="HeaderData"> Year 1 </td>
since all conditions are satisfied in the below sql how can I get all the ..Here is the fiddle Sql Fiddle

Declare @ContractTerm int
Set @ContractTerm = 5

Select Case When @ContractTerm >= 1 Then '<td class="HeaderData"> Year 1 </td>'
When @ContractTerm >= 2 Then '<td class="HeaderData"> Year 2 </td>'
When @ContractTerm >= 3 Then '<td class="HeaderData"> Year 3 </td>'
When @ContractTerm >= 4 Then '<td class="HeaderData"> Year 4 </td>'
When @ContractTerm >= 5 Then '<td class="HeaderData"> Year 5 </td>' End


Thanks

Answer Source

This will result to how many is declared on the @ContractTerm

DECLARE @ContractTerm INT
DECLARE @Counter INT
SET @ContractTerm = 5
SET @Counter = 1

WHILE @Counter <= @ContractTerm
BEGIN
   SELECT '<td class="HeaderData"> Year ' + CAST(@Counter AS VARCHAR) + ' </td>'

   SET @Counter += 1
END
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download