How to have a multi value parameter text box in SSRS that also allows blanks? - sql

I have an SSRS report that the user wants to be able to search upon multiple sites as supposed to just the one site that the report usually accepts.
If they don't enter a site in the parameter text box then the report searches for all sites.
I've handled this successfully within my SQL query in the where clause as shown below
WHERE
( ( LEN(#ProjectId) > 0 AND p.ProjectId = #ProjectId ) OR ( LEN(#ProjectId) =
0 ) )
This worked fine for searching just the one site or all sites if they didn't enter a value.
They want to be able to enter multiple sites and split them with a comma.
I handled this within my SSRS report as defining the parameter in my data set to be
=JOIN(Parameters!ProjectId.Value, ",")
However this doesn't work for blank values or null values as expected.
I've done some fancy expressions to try and get round this like
=IIF(ISNothing(Parameters!ProjectId.Value), "", JOIN(Parameters!ProjectId.Value))
To set the parameter passed to the SQL query to be blank and not to do the join if there isn't a value encountered.
Basically I want to know if there is a way I can have a multi set parameter split by commas but passed as a blank if a value is not chosen.
at the moment I've tried setting the parameter to allow multivalues which hasn't worked either I get the error message as shown below

First set up your multivalue parameter:
Then within your Dataset define a new parameter, in this instance I have called it #PCount:
#PCount is defined with the expression:
=IIF(Parameters!P1.Count=0 Or
(Parameters!P1.Count = 1 And Parameters!P1.Value(0) = ""),
0,
Parameters!P1.Count)
This will return 0 if no values are entered, or a blank value is entered. Then your query becomes something like:
SELECT Col1, Col2, Col3
FROM YourTable
WHERE ProjectID IN (#P1)
UNION ALL
SELECT Col1, Col2, Col3
FROM YourTable
WHERE #PCount = 0
You could do this with OR, as follows:
SELECT Col1, Col2, Col3
FROM YourTable
WHERE ProjectID IN (#P1)
OR #PCount = 0
But I think this is more likely to encounter performance issues.

Related

Adding a condition to the showcase

The task is as follows. I have a code that has a huge number of attributes. And to one of the attributes, let's say this is the card type card_type='universal', you need to add the following condition:
case when card>='129897' and card<='293965'then 'unnamed' and card>='093750' and card <='903750' then 'personal' end as parameter
The attribute itself is as follows :select case when card_sybtype in ('VISA','MS') then 'universal'
At the same time, I do not need to output this to the final script, but I need this feature to be present in the script. That is, I need it to be linked only to the card type.
I think you can use a nested case when.
And if you dont want to select parameter column, you can use a subquery to hide the columns. So, in case you want it, just add it to the outside select list.
SELECT col1, col2
FROM
(SELECT
col1, col2,
case when card_sybtype in ('VISA','MS') THEN -- universal case
case when card>='129897' and card <='293965' then 'unnamed'
when card>='093750' and card <='903750' then 'personal' end
else null
end as param_adhoc
FROM tab
) rs

How to enable multiple variables to be searched when having a Colon search box

Unable to make multiple search with = :variable.
I have a Select statement where I want to pull data, to do it in the most efficient way I have many places to gather the data from therefore I want null unless otherwise stated.
SELECT * FROM table WHERE (
(:nameColmn is null or nameColmn = :nameColmn)
AND
(:nameColmn2 is null or nameColmn2 = :nameColmn2)
AND
(:nameColmn3 is null or nameColmn3 = :nameColmn3)
)ORDER BY variable desc
there are 9 in the one I actually have, however it's the same.
A box prompts up where I can fill my data unless nothing is filled it stays blank (null)
I need to be able to add more then 1 variable, such as when doing
( nameColmn IN ('xyz','xyy','xyx'))
However this does not prompt a box. as they are set.
If you want to use Bind variable with multiple values, it can be passed as CSV string and split into rows.
SELECT * FROM yourtable
WHERE namecolmn IN ( SELECT regexp_substr(:vals,'[^,]+',1,level)
FROM dual CONNECT BY
regexp_substr(:vals,'[^,]+',1,level) IS NOT NULL
)
Note that the argument to this (:vals) should be xyz,xyy,xyx, i.e there can't be quotes in between.
Another option is to use Substitution variable (&vals), which will also prompt you for input and you can pass 'xyz','xyy','xyx' to get the desired result.
SET VERIFY OFF --diables substitution output.
SELECT * FROM yourtable
WHERE namecolmn IN (&vals);

How to replace where clause dynamically in query (BIRT)?

In my report query I have a where clause that needs to be replaced dynamically based on the data chosen in the front end.
The query is something like :
where ?=?
I already have a code to replace the value - I created report parameter and linked to the value ? in the query.
Example:
where name=?
Any value of name that comes from front end replaces the ? in the where clause - this works fine.
But now I need to replace the entire clause (where ?=?). Should I create two parameters and link them to both the '?' ?
No, unfortunately most database engines do not allow to use a query parameter for handling a dynamic column name. This is for security considerations.
So you need to keep an arbitrary column name in the query:
where name=?
And then in "beforeOpen" script of the dataset replace 'name' with a report parameter value:
this.queryText=this.queryText.replace("name",params["myparameter"].value);
To prevent SQLIA i recommend to test the value of the parameter in this script. There are many ways to do this but a white list is the strongest test, for example:
var column=params["myparameter"].value;
if (column=="name" || column=="id" || column=="account" || column=="mycolumnname"){
this.queryText=this.queryText.replace("name",column);
}
In addition to Dominique's answer and your comment, then you'll just need a slightly more advanced logic.
For example, you could name your dynamic column-name-value pairs (column1, value1), (column2, value2) and so on. In the static text of the query, make sure to have bind variables for value1, value2 and so on (for example, with Oracle SQL, using the syntax
with params as (
select :value1 as value1,
:value2 as value2 ...
from dual
)
select ...
from params, my_table
where 1=1
and ... static conditions....
Then, in the beforeOpen script, append conditions to the query text in a loop as needed (the loop left as an exercise to the reader, and don't forget checking the column names for security reasons!):
this.queryText += " and " + column_name[i] + "= params.value" + i;
This way you can still use bind variables for the comparison values.

Replace null with zero in crosstab query in Telerik

I just created 2 cross table using wizard function of Telerik Standalone report designer tool
since ISCED 5 has values for private and public its showing properly
using same query I created second cross tab and
but since ISCED 6 table doesnt have values for "public" section its showing like this
how to show as zero for public section 2nd cross tab (when no values for specific row)
You should modify the value of the field using an expression to evaluate your condition.
Select the textbox containing the data corresponding to the column you like to format when the value is null.
In the property pan on the right select "Value"
Write in there your expression, it should be something like this:
=Iif(Fields.MyField IS Null,"0",Fields.MyField)
You should also consider if the value instead of null is empty and eventually cover this case in the expression if applicable.
= Iif(Fields.MyField IS Null OR Fields.MyField = "", "0", Fields.MyField)
More information on conditional formatting can be found here.
Let us know if this works for you.

how can I replace blank value with zero in MS-Acess

I have below query in Ms-Access but I want to replace Blank value with zero but I can't get proper answer. Is there any way to replace blank value in zero.
(SELECT
SUM(IIF(Review.TotalPrincipalPayments,0,Review.TotalPrincipalPayments))+
SUM(IIF(Review.TotalInterestPayments,0,Review.TotalInterestPayments ))
FROM
tblReviewScalars as Review
INNER JOIN tblReportVectors AS Report ON(Review.LoanID=Report.LoanID)
WHERE Report.AP_Indicator="A" AND Report.CashFlowDate=#6/5/2011# AND Review.AsofDate=#6/5/2011# AND ( Review.CreditRating =ReviewMain.CreditRating)) AS [Cash Collected During the Period],
I assume TotalPrincipalPayments and TotalInterestPayments are both numeric types, hence the 'blanks' in question is the NULL value.
In SQL, the set function SUM will disregard NULL values, unless all values resolve to NULL in which case NULL is returned (erroneously and the error is with SQL not Access for a change :)
To use a simple example, SELECT SUM(a) FROM T; will only return NULL when a IS NULL is TRUE for all rows of T or when T is empty. Therefore, you can move the 'replace NULL with zero' logic outside of the SUM() function. Noting that "NULLs propagate" in calculations, you will need to handle NULL for each SUM().
You haven't posted the whole of your query e.g. the source of the correlation name ('table alias') ReviewMain is not showm. But it seems clear you are constructing a derived table named "Cash Collected During the Period", in which case your calculated column needs an AS clause ('column alias') such as TotalPayments e.g.
...
(
SELECT IIF(SUM(Review.TotalPrincipalPayments) IS NULL, 0, SUM(Review.TotalPrincipalPayments))
+ IIF(SUM(Review.TotalInterestPayments) IS NULL, 0, SUM(Review.TotalInterestPayments))
AS TotalPayments
FROM tblReviewScalars as Review
INNER JOIN tblReportVectors AS Report
ON Review.LoanID = Report.LoanID
WHERE Report.AP_Indicator = 'A'
AND Report.CashFlowDate = #2011-05-06#
AND Review.AsofDate = #2011-05-06#
AND Review.CreditRating = ReviewMain.CreditRating
) AS [Cash Collected During the Period], ...
An alternative to #onedaywhen's answer is to use the nz function, which is specifically for null-substitution:
SELECT
SUM(NZ(Review.TotalPrincipalPayments,0))+
SUM(NZ(Review.TotalInterestPayments,0))
...
As onedaywhen pointed out, this is functionally equivalent to putting the function outside the aggregate, which may perform better (the function is called once, rather than once per un-aggregated row):
SELECT
NZ(SUM(Review.TotalPrincipalPayments),0)+
NZ(SUM(Review.TotalInterestPayments),0)
...
To change a null value to a zero in an Access 2010 database, open your table, go to design view, click on the field and set the default value to: =0.