This question already has answers here:
Conditional WHERE statement SQL Server
(4 answers)
Closed 4 years ago.
I have a stored procedure that I'm trying to do a conditional WHERE clause on. I believe the solution is easy but for some reason it's escaping me.
What I'm trying to achieve is: if a ID (#pbid in this case) is passed to the stored procedure I only want to return a single record matching that parameter otherwise I'd like to return all results in the table.
Here is the what I have come up with and it's obviously not going to work because I'm trying to do an equals evaluation at PBID (PBID =) and I'm looking to conditionally make that
where pb.PBID =
CASE WHEN #pbid is not null THEN
#pbid --just return results with this ID
ELSE
is not null --return all results
END
If it was C# or something I'd write it like:
if(intPBID > 0) --parameter set, only return results with that param
{
pb.PBID = intPBID
}
else
{
pb.PBID > 0 --return everything
}
I hope this isn't too confusing and I'd appreciate any feedback.
Thanks
I'm not sure about performance implications, but I typically end up doing something like:
SELECT *
FROM table
WHERE (#pbid IS NULL OR pb.PBID = #pbid)
Related
This question already has answers here:
SQL Server : check if variable is Empty or NULL for WHERE clause
(6 answers)
Closed 6 months ago.
I'm not sure if CASE is what I actually need to use here, but basically what I am trying to do is apply "WHERE ba.batchid = #batchId" but only if #batchId is not null.
Basically something like this, but this gives an error around the ba.batchid = '2' area ('2' is what the #batchId would come out to).
CASE WHEN '2' != '' THEN ba.batchid = '2' ELSE null END
It helps to think of Case statements as a scalar value. The problem you are experiencing is caused by doing a comparison within the case statement. Basically, you need to pull the comparison out of the case statement.
I'm not sure of the logic you are trying to use, but the following syntax should be correct.
CASE WHEN '2' != '' THEN ba.batchid ELSE null END = '2'
Note that the = '2' is moved to the end. This way, the case statement will return batchid or null depending on the WHEN statement. This value is then compared to '2'.
Try this:
WHERE NULLIF(#batchId, '') IS NOT NULL AND ba.batchid = #batchid
First condition avoids empty('') as well as NULL with #batchid. Second will match the table value.
Take it out of the SELECT part of your statement entirely and put it in the WHERE clause (after FROM)
WHERE ba.batchid = #batchId
AND ba.batchid IS NOT NULL
Note that the AND ba.batchid IS NOT NULL is not necessary here as the first line will filter any nulls. However there are versions of SQL where this is not the case and retaining it in the query still works, hence I have left it in.
I have a QRY im developing in Oracle for spotfire. In the where statement, I have a decision case statement and if its True, im trying to pass a list of items to match a column, below is what I have, but its throwing a missing right parenthesis error and I cannot determine why. In short, when a variable is determined True (in this case 9>8 for the example, I need it to result those items, else, result the entire column with no limits.
Note: This works fine when its only 1 item being passed, i.e. 'BOB' but as soon as its multiple, this error occurs.
and Column1 = (CASE When 9>8 Then ('BOB','TOM') Else Column1 END)
Case expressions are best avoided in the where clause. Instead, write the logic with AND and OR:
And (
(9>8 AND Column1 IN ('BOB','TOM'))
OR 9<=8 -- You say you check a variable here, don't forget to check for NULL
)
Oracle does not have a boolean type for use in SQL queries.
Instead, just use basic logic:
and ( (9 > 8 and Column1 in ('BOB','TOM')) or
9 <= 8
)
Problem while placing oracle query inside of the oracle if else statements
select workgroupid, maxchats,
case
when a.maxchats ='-1' then
(select propvalue from ofproperty where name = 'xmpp.live.defaults.maxchats')
else
null (1)
end as aaaa
from fpworkgroup a;
when im placing maxchats at 1 position im getting error,,
how can i resolve this....?
help me..
thanks in advance
It's hard to understand your question but it sounds like you're trying to put maxchats in place of null (1) and it gives you an error?
If that is correct, I'm guessing that the error is that fpworkgroup.maxchats and ofproperty.propvalue have different column types that do not line up. All branches of a CASE must return the same column type. You cannot, for example, return an int from one branch and return a varchar from another.
You'll have to cast one or the other so that they return the same type.
In most RDBMS:es, this work:
select (5 > 3)
and evaluates to true. It doesn't work in MS Transact SQL and the only workaround I've found is to write:
select case when 5 > 3 then 1 else 0 end
Which kind of sucks because it is much more verbose. Is there a better way to write the above kind of checks?
If the problem is arithmetic comparison:
select (5 - 3)
Then at the application level test for < or = or > 0.
You could write it as a scalar-valued function, but it will be very slow on large datasets.
If your program often requires such case constructs you could create your set of functions that will have user functions like Bool_IsGreater(left, right) that will return you your desired 0 or 1.
SQL Server doesn't support boolean value type anyway even for basic column use.
If you will need performance and those 5 and 3 values come naturally from some select query you might want to create a new column and set its value to 1 or 0 by trigger or something, which could help with performance.
I have something similar to the following WHERE clause in my SQL statement:
AND (ipdp.UserID!=dbo.fn_userid(ipdp.ItemID) OR dbo.fn_userid(ipdp.ItemID) IS NULL)
However, I don't like how dbo.fn_userid function is being ran twice. How can I improve this line of code so that it only has to be ran once?
ipdp.UserID is always a Non-NULL Integer value
dbo.fn_userid can return a NULL or an Integer value
Cheers.
Try:
AND ipdp.UserID != ISNULL(dbo.fn_userid(ipdp.ItemID), -1)