How do I write an SSRS Wildcard search in the Report Parameters
SELECT *
FROM Table1
WHERE Table1.Name = LIKE '%'#Name'%'
or
WHERE Table1.Name = in (:Name)?
How do I do this in SSRS?
Say I have a very simple self-contained query in the report:
with val as
(
select val = 'ABC'
union all select 'DEF'
union all select '1ABC3'
)
select *
from val
where val like #Param
I also have a parameter called Param in the report.
By default, even though I have like in the query, there are no wildcards so only exact matches will be returned:
If we look at the Dataset Properties, we can update the parameter being passed to add wildcards. By default it looks like this:
Change the Parameter Value expression to:
="%" & Parameters!Param.Value & "%"
Now the query text will be using a parameter with wildcards, so partial matches are returning data in the report:
Alternative method
Actually, thinking about this, perhaps an easier way to achieve the above is to do something like this in the report query text:
SELECT *
FROM Table1
WHERE Table1.Name = LIKE '%' + #Name + '%'
i.e. Just concatenate the wildcards directly to the parameter string. This works identically to the first option.
This is how I did it:
Create a report parameter (data type text) named MyFilter
Create a DataSet ReportData with an expression for the source query, something like:
="SELECT FilteredField, FieldValue FROM DataTable " & IIF(Parameters!MyFilter.Value.ToString() = "", "", "WHERE
FilteredField LIKE '%" &
REPLACE(REPLACE(Parameters!MyFilter.Value.ToString(),"*","%"),"?","_")
& "%'")
As you can see, I have a basic SELECT statement which I concatenate with an empty string if the value for MyFilter is an empty string. If a value is filled in, I add a WHERE clause in which I replace the "*" by "%" and "?" by "_"
This is just a simplified version of what we actually did, allowing only "?" and "*" to be used as wildcards, but it works fine for our users.
Use this will solve ur issue
="" & Parametername.value & ""
Related
I am new to python and currently learning to use SQL with python. I have the following code:
word = input("Enter a word: ")
query = cursor.execute("SELECT * FROM Dictionary WHERE Expression LIKE '%s%' " % word)
results = cursor.fetchall()
The second line throws an error since I don't think I can use '%s%' like that? How would I change this so as to be able to make this work? I want to be able to return all related entries to the users input. So if the user inputs "rain", then I want the query to return all possible results e.g. "raining", "rainy" etc. Thank you.
You can try
query = cursor.execute(f"SELECT * FROM Dictionary WHERE Expression LIKE '%{word}%' ")
You should use cursor.execute() parameter substitution rather than string formatting, to prevent SQL injection.
Then use CONCAT() to surround the search string with %.
query = cursor.execute("SELECT * FROM Dictionary WHERE Expression LIKE CONCAT('%', %s, '%' "), (word,))
I'm trying to write a add query that will change depending on the parameter. I have several queries:
LastK1StatDate
LastK2StatDate
.
.
LastK15StatDate
LastK16StatDate
My criteria should change depending on the value entered for the parameter "qryKioskNum" when the query is run.
Currently my criteria is this:
>Max("[LastK" & [qryKioskNum] & "StatDate]![K" & [qryKioskNum] & "LastDate]")
qryKioskNum is type Short Text
It keeps giving me the error "The expression is typed incorrectly, or is too complex to be evaluated."
Here is the complete SQL statement for this query:
PARAMETERS qryKioskNum Short;
INSERT INTO K1DispRejStat ( K1StatDate, K1BillCount1, K1BillCount2,
K1BillCount3, K1BillCount4, K1BillCount5, K1BillCount6, K1BillRej1,
K1BillRej2, K1BillRej3, K1BillRej4, K1BillRej5, K1BillRej6 )
SELECT DateValue([responseFrames]![dispDateTime]) AS [Date],
Sum(responseFrames.billCount1) AS SumOfbillCount1,
Sum(responseFrames.billCount2) AS SumOfbillCount2,
Sum(responseFrames.billCount3) AS SumOfbillCount3,
Sum(responseFrames.billCount4) AS SumOfbillCount4,
Sum(responseFrames.billCount5) AS SumOfbillCount5,
Sum(responseFrames.billCount6) AS SumOfbillCount6,
Sum(responseFrames.BillRej1) AS SumOfBillRej1, Sum(responseFrames.BillRej2)
AS SumOfBillRej2, Sum(responseFrames.BillRej3) AS SumOfBillRej3,
Sum(responseFrames.BillRej4) AS SumOfBillRej4, Sum(responseFrames.billRej5)
AS SumOfbillRej5, Sum(responseFrames.billRej6) AS SumOfbillRej6
FROM responseFrames, LastK1StatDate
WHERE (((responseFrames.kioskID)="K1"))
GROUP BY DateValue([responseFrames]![dispDateTime])
HAVING (((DateValue([responseFrames]![dispDateTime]))>Max("[LastK" &
[qryKioskNum] & "StatDate]![K1LastDate]")))
ORDER BY DateValue([responseFrames]![dispDateTime]);
currently everything is set to "K1" but I would like all reference to K1 to be dynamic
I think it is just a syntax issue but can't find how exactly this should be typed out.
Any help is great. Thanks!
*edited for clarity
In msaccess, create a PassThru query (because it retains the multi-line nice format).
Create // QueryDesign // Close // rightClick // SQLSpecific // PassThru
Paste in the following sql.
INSERT INTO kxxdisprejstat
(kxxstatdate,
kxxbillcount1,
kxxbillcount2,
kxxbillcount3,
kxxbillcount4,
kxxbillcount5,
kxxbillcount6,
kxxbillrej1,
kxxbillrej2,
kxxbillrej3,
kxxbillrej4,
kxxbillrej5,
kxxbillrej6)
SELECT Datevalue([responseframes] ! [dispdatetime]) AS [Date],
SUM(responseframes.billcount1) AS SumOfbillCount1,
SUM(responseframes.billcount2) AS SumOfbillCount2,
SUM(responseframes.billcount3) AS SumOfbillCount3,
SUM(responseframes.billcount4) AS SumOfbillCount4,
SUM(responseframes.billcount5) AS SumOfbillCount5,
SUM(responseframes.billcount6) AS SumOfbillCount6,
SUM(responseframes.billrej1) AS SumOfBillRej1,
SUM(responseframes.billrej2) AS SumOfBillRej2,
SUM(responseframes.billrej3) AS SumOfBillRej3,
SUM(responseframes.billrej4) AS SumOfBillRej4,
SUM(responseframes.billrej5) AS SumOfbillRej5,
SUM(responseframes.billrej6) AS SumOfbillRej6
FROM responseframes,
lastkxxstatdate
WHERE (( ( responseframes.kioskid ) = "kxx" ))
GROUP BY Datevalue([responseframes] ! [dispdatetime])
HAVING (( ( Datevalue([responseframes] ! [dispdatetime]) )
> Max([lastkxxstatdate]![kxxlastdate]) ))
ORDER BY Datevalue([responseframes] ! [dispdatetime]);
Name it kxxInsert (or some such, using kxx to say that it is generalized).
Then add this to the program
Sub getKxx()
Dim qrykiosknum As Integer ' temp here to have something
qrykiosknum = 3 ' temp here for an example
Dim kxxSQL As String, strSQL As String
kxxSQL = CurrentDb.QueryDefs("kxxInsert").SQL
strSQL = Replace(kxxSQL, "kxx", "k" & qrykiosknum)
'MsgBox (strSQL) ' it is too big to see all of it
Debug.Print strSQL
' then run strSQL
End Sub
Having dynamic tablename in MSAccess or MSSQLServer is possible when the Replace is done before executing the SQL.
I doubt you can make this work by using a query with a parameter. You are much better off using VBA. Use InputBox to get the variable portion of the query and DoCmd.RunSQL to run the query.
I'm trying to add string to itself in MySQL in cursor, i need that for my dynamic SQL query.
I have my string set before cursor
ESQL3 = "FIRST PART OF QUERY ";
Now in cursor i want to add the rest of the query, im using CONCAT() but i feel like its not doing the job.
SET #ESQL3 = CONCAT(ESQL3, aggregate_function, "(", table_name, ")" as ", table_name, " , ");
The result is declared first part of query + last call of that function.
I've searched a lot for an answer.
Thank you
Looks like you want to reference the user defined variable, #ESQL3 as an argument in the CONCAT function, rather than the stored program variable ESQL3.
Change this:
SET #ESQL3 = CONCAT(ESQL3, ...
to this:
SET #ESQL3 = CONCAT(#ESQL3, ...
^
(MySQL stored program variables and MySQL user-defined variables are two different things. That is, ESQL3 and #ESQL3 are not the same variable. They are two fundamentally different variables.
EDIT
Also, the rest of the CONCAT arguments look kind of funky. These look okay:
, aggregate_function
, "("
, table_name
But this doesn't look right:
, ")" as ", table_name, " , ");
I don't think the "as" keyword is allowed in CONCAT, and that's not going to be seen as a string literal.
Maybe you mean to include "as" as part of the string value, like this:
, ") as "
, table_name
, " , "
FOLLOWUP
You need to initialize #ESQL3. That's not happening in the code you posted. The scope of the user-defined variable is the session, the value of that variable persists across statements. The next time it's referenced in the session, it's going to have whatever value was last assigned to it.
And when you initialize it, leave off the trailing comma. Add the comma when you append the next expression to the SELECT list.
SET #ESQL3 = "CREATE TABLE Obroty AS SELECT Towar";
^ ^
Note that we need to initialize the user-defined variable which is referenced later.
It matters not one whit what the stored procedure variable ESQL3 is set to. That has no relationship to the user-defined variable #ESQL3.
Inside the loop, when you are appending to #ESQL3, include the comma literal BEFORE the expression, rather than after it. Like this:
SET #ESQL3 = CONCAT(#ESQL3, ", ", funkcja, "(", miech, ") as ", miech );
^^^^
So, entering the loop, #ESQL3 is going to have the value
CREATE TABLE Obroty AS SELECT Towar
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
After the first trip through read_loop, it will have a value like
CREATE TABLE Obroty AS SELECT Towar, SUM(foo) AS foo
^^^^^^^^^^^^^^^^^
The next time through the loop, it will have a value like
CREATE TABLE Obroty AS SELECT Towar, SUM(foo) AS foo, SUM(bar) AS bar
^^^^^^^^^^^^^^^^^
When the loop is exited, you'll be ready to append "FROM whatever. (Make sure you have the space before the FROM.)
My situation is
I have a parameter, this is a list, allowing multi values. That mean the first record in the list is 'Select All'
When user select All I need to include in my report all records that match with the list plus those that are blank. (My dataset is returning these)
When user select only 1 or a few I want to include only these records. No those that are blank
My problem:
I have a filter in my dataset that evaluate a parameter in a list, but I need to add a conditional filter to include a blank records when the selection will be "Select All"
I tried to use expression but this doesn't work
Filter expression
Fields!NAME.Value in = Parameters!List.Value !!!!!!!!!!!! Work Fine
But I need to change it like as
If Parameters!List.Value = 'Select All' Then
Fields!NAME.Value in = Parameters!List.Value or Fields!NAME.Value = " "
Else
Fields!NAME.Value in = Parameters!List.Value
End
Can you give an advice who can I resolve it please !!!
I'm working in SSRS R2
Thanks!!
This worked for me
Expression: =IIF(Parameters!pLocation.Value <> " All Locations", Fields!LOCATION.Value, FALSE)
Operator: =
Value: =IIF(Parameters!pLocation.Value <> " All Locations", Parameters!pLocation.Value, FALSE)
If you use Filter on your Dataset, try this:
Expression: [NAME]
Operator: IN
Value (fx): =Split(Replace(Join(Parameters!List.Value, ","), "Select All", " "), ",")
Try to work along this path. Basically you can reconstruct the multi value items into a string with Join(), and deconstruct it again into array by using Split(); where in between, you can manipulate them, for modifying (e.g. converting "Select All" into " "), adding (imitating "OR"), or removing extra items.
There is an alternative for this.
Add one more item to the paramater dataset values say "Not Available" as Label and value with the null. then there will be no change in the stored procedure and you can retrieve the data.
If the user select the specific item then he will get those values only. If he selects all then he will get the data for the null also with the all the others.
Hope this will help
You can put the logic in just one location if you do it this way.
You filter on the parameter, unless it's all values then the filter always matches.
Just a little cleaner.
Expression: =IIF(Parameters!pLocation.Value <> " All Locations", Fields!LOCATION.Value, " All Locations")
Operator: =
Value: =Parameters!pLocation.Value
Can anyone tell me how to display all the selected value of my multi value parameter in SSRS report. When giving parameter.value option it gives error.
You can use the "Join" function to create a single string out of the array of labels, like this:
=Join(Parameters!Product.Label, ",")
=Join(Parameters!Product.Label, vbcrfl) for new line
I didn't know about the join function - Nice! I had written a function that I placed in the code section (report properties->code tab:
Public Function ShowParmValues(ByVal parm as Parameter) as string
Dim s as String
For i as integer = 0 to parm.Count-1
s &= CStr(parm.value(i)) & IIF( i < parm.Count-1, ", ","")
Next
Return s
End Function
Hopefully someone else finds this useful:
Using the Join is the best way to use a multi-value parameter. But what if you want to have an efficient 'Select All'? If there are 100s+ then the query will be very inefficient.
To solve this instead of using a SQL Query as is, change it to using an expression (click the Fx button top right) then build your query something like this (speech marks are necessary):
= "Select * from tProducts Where 1 = 1 "
IIF(Parameters!ProductID.Value(0)=-1,Nothing," And ProductID In (" & Join(Parameters!ProductID.Value,"','") & ")")
In your Parameter do the following:
SELECT -1 As ProductID, 'All' as ProductName Union All
Select
tProducts.ProductID,tProducts.ProductName
FROM
tProducts
By building the query as an expression means you can make the SQL Statement more efficient but also handle the difficulty SQL Server has with handling values in an 'In' statement.