views:

184

answers:

5

Hello,

I have a variable called @status which I set before this select statement:

Select ordr_num as num, ordr_date as date, ordr_ship_with as shipwith From order where ordr_num = @ordrNum

I only want to select ordr_ship_with column if @status <> 'Cancelled', otherwise I want to select null for shipwith. How do I accomplish this?

+3  A: 
SELECT ordr_num as num, ordr_date as date, 
    CASE WHEN @status<>'Cancelled' THEN ordr_ship_with ELSE NULL END as shipwith 
FROM order 
WHERE ordr_num = @ordrNum
Joel Coehoorn
+1  A: 

Try this out

Select 
    ordr_num as num, 
    ordr_date as date, 
    CASE 
     WHEN @Status <> 'Cancelled' THEN ordr_ship_with 
     ELSE NULL END
    as shipwith 
From order 
where ordr_num = @ordrNum

Although I have a feeling that you STATUS is an actual column in the Order table. In that case, do this:

Select 
    ordr_num as num, 
    ordr_date as date, 
    CASE 
     WHEN Status <> 'Cancelled' THEN ordr_ship_with 
     ELSE NULL END
    as shipwith 
From order 
where ordr_num = @ordrNum
Raj More
A: 

SELECT CASE
WHEN @status <> 'cancelled' THEN ordr_ship_with
ELSE null
END AS shipwith, ... other fields

Russell Steen
+1  A: 

Use Case : http://msdn.microsoft.com/en-us/library/ms181765.aspx

Stephen lacy
+1  A: 
Select
    ordr_num as num,
    ordr_date as date,
    CASE WHEN @status <> 'Cancelled' THEN ordr_ship_with ELSE NULL END as shipwith
From
    order where ordr_num = @ordrNum
gbn