I have what seems to be a really easy SQL query I can't figure out and its driving me nuts. This is SQL 2008. Basically, there is a status field where the can pick "pending", "satisfied" or all. If they send in "pending" or "satisfied" there's no problem. But when they pick all I'm having problems. Mostly because I can't figure out how to get the records where this field is null to show up (because it has to be 'is null' instead of '= null'. (This is the way the data will come over; I have no control over that.)
The code I've been using does not work for nulls.
SELECT * FROM Payment_Table where Payment.Status_code = @status_id
You can try
SELECT Col1, Col2,...,Coln --Required Columns
FROM Payment_Table
where (Payment.Status_code = @status_id OR @status_id IS NULL)
Try:
SELECT *
FROM Payment_Table
WHERE Payment.Status_code = ISNULL(@status_id, Status_code)
This will return all payments.
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