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?
Use WHERE Clause to Conditionally Select Rows in SQL SELECT Statement for MySQL. The WHERE clause indicates the condition or conditions that rows must satisfy to be selected. The WHERE condition is an expression that evaluates to true for each row to be selected.
When we have to select multiple columns along with some condition, we put a WHERE clause and write our condition inside that clause. It is not mandatory to choose the WHERE clause there can be multiple options to put conditions depending on the query asked but most conditions are satisfied with the WHERE clause.
Select the letter at the top to select the entire column. Or click on any cell in the column and then press Ctrl + Space.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With