Conditionally include a line in a T-SQL WHERE clause - sql-server

Honestly, I've gone over other questions posted here that mention conditional where clauses and I'm not seeing any answers that seem to work for my situation. If one does, please point me to it!
In a stored procedure called by an SSRS report, the user can select one of two values in a dropdown - say "Yes" or "No" and that goes into the variable #color.
In the WHERE clause of the sproc I need to filter differently depending on the user's selection.
So, if #color = 'Yes', then I need to have a line in the WHERE clause that reads something like tb1.somecolumn = 'BLUE'. But if #color = 'No' then I don't need that line in the where clause at all (tb1.somecolumn can be any value).
How do I conditionally include a line in the WHERE clause?

Include the check for #color in the WHERE as follows.
WHERE (#color = 'No')
OR (#color = 'Yes' AND tb1.somecolumn = 'BLUE')
Explanation:
If the value of #color is 'No', then all rows are returned.
If the value of #color is 'Yes', then only the 'BLUE' rows are returned.

Try following condition
WHERE (#color = 'Yes' AND tb1.somecolumn = 'BLUE')
OR (#color = 'No' AND tb1.somecolumn = tb1.somecolumn)

Related

SSRS report filtering based on a parameter

I have an SSRS report with a field called SetGroup. Each row has a SetGroup value of either 'Group 1' or 'Group 2'. I have a parameter called Term. If Term = 'All' then only show Group 1 records, otherwise show Group 2. I am trying to set up a filter to do this, but it seems I need it to be like IF-THEN-ELSE. I have the filter here, but how do I now tell it to only show the Group 1 records ELSE Group 2?
I have two ideas and I'm not sure if either are possible or how to do them. Maybe one of you has an idea.
1.) Could I put a CASE in my WHERE clause? Something along the lines of this? SQL Server says this is bad syntax because of the extra = operators.
WHERE CASE WHEN #Term = 'All' THEN j.SetGroup = 'Group 1' ELSE j.SetGroup = 'Group 2' END
2.) Could I put IIF formulas in the Expression and in the Value boxes of my filter with the bottom playing off the top one?
I knew it there was a simple solution in there somewhere.
WHERE j.SetGroup = CASE
WHEN #Term = 'All' THEN 'Group 1'
ELSE 'Group 2'
END

SQL Multi-condition WHERE clause with one optional parameter?

Reworking an existing Stored Procedure to make a previously required parameter optional.
The query is massive, and deals with much more than my current issue, so I won't post it in it's entirety but I believe I can get my fix by editing one of the WHERE clauses.
WHERE
O.wsID = #wsid AND
((M.UpdateDate BETWEEN #olddate AND #newdate) OR (MI.UpdateDate BETWEEN #olddate AND #newdate))
The first parameter #wsid is the value I need to make optional, it's initially declared as 0 and given a new value from a select box on the front-end of the website. I have attempted to use a CASE statement to alternate between to WHERE clauses depending on whether the value is 0 or an actual ID.
WHERE
#wsid = CASE
WHEN #wsid <> 0 THEN ((O.wsID = #wsid) AND ((M.UpdateDate BETWEEN #olddate AND #newdate) OR (MI.UpdateDate BETWEEN #olddate AND #newdate)))
ELSE ((M.UpdateDate BETWEEN #olddate AND #newdate) OR (MI.UpdateDate BETWEEN #olddate AND #newdate)))
END
But I get a bunch of syntax errors in the WHERE clause itself and also an error stating Expecting CONVERSATION or TRY for the SELECT statement below.
Is there an easier option for making the O.wsid = #wsid parameter optional?
WHERE
(O.wsID = #wsid OR #wsid = 0) AND
((M.UpdateDate BETWEEN #olddate AND #newdate) OR (MI.UpdateDate BETWEEN #olddate AND #newdate))

SQL Server : only display results of stored procedure under certain conditions

I'm currently working on a stored procedure that creates a table with many columns with many conditions. One of the requirements of this proc is that when column A has a value of YES then Column B must have a value. I have no idea how to code this but I was leaning towards a case statement in the where clause along these lines
CASE WHEN Table.A = 'YES' then Table.B is not NULL end
You can transform it to
Where (Table.A = 'YES' and Table.B is not NULL)
OR Table.A <> 'YES'
May be you should add the full query to get it added in right way to the query
I'm assuming you need to include other logic in your process, which you can achieve by nesting case statements like the below. If not, Prdp's answer will be sufficient for your needs.
where case when Table.A = 'YES'
then case when Table.B is not null
then 1
else 0
end
else 0
end = 1

Determine if T-SQL select statement returns any rows

I am using Microsoft SQL Server 2012. I want to set a boolean value in a stored procedure. I want to set the value to true if a certain select query returns more than zero rows.
Declare #ShowQuotingTool as bit
(select count(*) from tblAgentsLicensedState where AgentCode = '016893' and StateCode in ('AZ','GA','IA', 'TN', 'SC', 'KS', 'MI' ,'NC', 'UT')) > 0;
How do I set #ShowQuotingTool to true or false based on the select query? I get the error Incorrect syntax near ">".
You can just assign #ShowQuotingTool to the count query. This is based on the fact that any count > 0 will set the bit variable #ShowQuotingTool to true and count = 0 will set it to false
Declare #ShowQuotingTool as bit
SELECT #ShowQuotingTool = (select count(*) from tblAgentsLicensedState where AgentCode = '016893' and StateCode in ('AZ','GA','IA', 'TN', 'SC', 'KS', 'MI' ,'NC', 'UT'))
You should preferably use EXISTS which would have better performance than COUNT query above.
IF EXISTS(select * from tblAgentsLicensedState where AgentCode = '016893' and StateCode in ('AZ','GA','IA', 'TN', 'SC', 'KS', 'MI' ,'NC', 'UT'))
SET #ShowQuotingTool = 1
ELSE
SET #ShowQuotingTool = 0
Good Day,
Please post DDL+DML if this is not what you are looking for. In any case please remember next time to post DDL+DML (queries to create the relevant tables and to insert some sample data, in order to give us the tools to reproduce the issue).
Back to your question:
If you have an SP that return the result of select query (or any DML query), that mean that you need this query to execute as it is. The idea of using COUNT function on the results mean that you execute another query, which is not logic action to do, since we already have the information in the headers of the result.
If you notice, by default, every time that you execute a query in the SSMS for example you get the information regarding number of rows: "(x row(s) affected)". There is no need to use another query.
If you are using external application that you develop, and your SP do not use "SET NOCOUNT ON", then this information coming back from the server :-)
You can see examples here:
https://msdn.microsoft.com/en-us/library/ms171921.aspx?f=255&MSPPError=-2147217396

Filtering by ALL or by selected attribute

Excuse my language or SQL/Reporting wording
I am generating a report using Reporting Services where I have 2 drop down lists in order to show only by period and by category. I want it so if I select and I am shown all entries and if selected from any of the 2 drop downs then filter by those selections. I have a stored procedure which states the following on it's WHERE clause:
WHERE (dbo.PERIOD_LIST.PERIOD_DESC = #period) OR (dbo.CATEGORY.CATEGORY_DESC = #category)
However I cannot get this to work on Reporting Services/Visual Studio. I am shown ALL the entries instead of the filtered ones. I initialize #period and #category as NULL. So how can I make it so the report shows all rows when both attributes are null and still be able to filter by each or both of them?
How can I achieve this?
Edit: I used the suggestion Filip Popović gave me. If you're having this problem modify your prepared statement and make sure you refresh fields on your data sets since there's additional clauses on your prepared statement!
Note that in SQL, NULL = NULL is never evaluated to true. You can take it as NULL is not nothing it is something but unspecified. And You can't compare two unspecified values. You have to test COL1 IS NULL to get true if column has null value.
Try this:
WHERE ((dbo.PERIOD_LIST.PERIOD_DESC = #period) OR (#period IS NULL))
AND ((dbo.CATEGORY.CATEGORY_DESC = #category) OR (#category IS NULL))
Check the parameter for NULL as part of the condition:
WHERE ((#period IS NULL) OR (dbo.PERIOD_LIST.PERIOD_DESC = #period))
AND ((#category IS NULL) OR (dbo.CATEGORY.CATEGORY_DESC = #category))

Resources