SQL Report Builder 3.0 Multi Value Parameter - tsql

I have a report that is required to accept number ranges and comma delimited values in a text field parameter. The parameter is for 'Account Type' and they want to be able to enter "1,2,5-9" and that will take integer values of 1,2,5,6,7,8,9. I know how to do this with a single value but never with a range.
The example code I would use for a single value is:
SELECT
arcu.vwARCUAccount.AccountType
,arcu.vwARCUAccount.ACCOUNTNUMBER
FROM
arcu.vwARCUAccount
WHERE
arcu.vwARCUAccount.AccountType = #AccountType
Any information would be extremely helpful. Someone on my team already estimated it and said it could be done without even realising that they wanted a range so now I am stuck figuring it out. I bet everyone here has been in my position so I thank everyone in advance.

There are several things you want to do.
A. Set up a parameter that is data type 'integer' and ensure you select the checkbox 'Allow multiple values'. Set it's value to be 'Ints'. Hit okay for now.
This essentially set up an array available list of a data set that you define for that type of data type that can be passed more than one dataset.
B. Create a simple dataset called 'values' that is like so
declare #Ints table ( id int);
insert into #Ints values (1),(2),(5),(6),(7),(8),(9)
C. Go back to your variable in step one and open up it's properties. Select 'Available Values' on the side pane. Choose radio button 'Get values from a query'. List your data set as 'values' and your value and label to be 'id'.
You have now bound your parameter array to values you specified. However a user DOES NOT have to just choose one or all of these but choose one or many of them.
D. You need to set up your main dataset(which I assume you already did before coming here). For the purpose of my example I will make a simple one up. I create a dataset called person:
declare #Table Table ( personID int identity, person varchar(8));
insert into #Table values ('Brett'),('Brett'),('Brett'),('John'),('John'),('Peter');
Select *
from #Table
where PersonID in (#Ints)
The important part is the predicate showing:
'where PersonID in (#Ints)'
This tells the dataset that it is dependent on the user to select a value in this array parameter.

I'm not completely proficient with tsql but what about using reg expressions?
See LIKE (Transact-SQL)
Such as:
arcu.vwARCUAccount.AccountType like '[' + replace(#AccountType, ',','') + ']'

This could work. As a broud brush try:
Remove the filter of the account type from the tsql.
Create a vb function that inputs a number, this being the account type, and tests to see if it is in the parameter string supplied by the user and outputs a 1 or 0. Vb Functions
Function test(byval myin as integer, byval mylimits as string) as integer
'results as 1 or 0
dim mysplit as string()= Split(mylimits, ",")
dim mysplit2 as string(1)
'look through all separated by a ","
For Each s As String In mysplit
'does there exists a range, i.e. "-"?
if s like "%-%" then
mysplit2 = split(s, "-")
'is the value between this range?
if myin >= mysplit(0) andalso myin <= mysplit(1) then
return 1
end if
'is the value equal to the number?
elseif s = myin then
return 1
end if
Next s
return 0
End
3. Create a filter on the dataset using the vb function with the account type as the input equal to 1.
=code.test(Fields!AccountType.Value, Paramaters!MyPar.Value)

Related

I need to change the output of a query so that instead of it coming back as the abbreviation 'em' it says 'employee'. Tsql

I have the correct result coming back. I just need to convert 6 abbreviations in that result to their correct names. There are 20k names assigned to 1 of 6 abbreviated names.
I tried aliasing but that seems to only work for table names.
I tried doing a case statement but that didn't work.
You need to provide more details (like some sample input and output), but if you have data like EM100, and you want to make it EMPLOYEE 100, then you could use an expression such as:
CASE WHEN ColumnName like 'EM%' THEN 'EMPLOYEE ' + SUBSTRING (ColumnName,3,100)
WHEN ColumnName like 'RN%' THEN 'REGNURSE' + SUBSTRING (ColumnName,3,100)
else ColumnName END
But providing more details will help provide a more specific answer.

PostgreSQL - JSONB in WHERE

I have a field that is defined to be JSONB, though in reality it has just text (no, I can't change the type as a solution).
This query:
SELECT value FROM property WHERE id = 1;
returns this:
-[ RECORD 1 ]--
value | "IP"
Now, I want to query by this value e.g. SELECT value FROM property WHERE value = 'IP'. I tried several different casting (value::TEXT = 'IP', value::VARCHAR = 'IP') but they all return no results. What is the correct way to do this query?
SELECT value
FROM property
WHERE value = to_jsonb('IP')
If you prefer casting value to string then you have to fix your right side and pass "IP" instead of IP. However if the right side is actually not fixed (i.e. values other than IP are possible) then you would have to properly escape it, e.g. for A"B the actual value would be "A\"B". Thus I suggest my original solution (which takes care of that).
Converting a scalar JSON value to text, isn't unfortunately as straight-forward as one might wish. Using to_jsonb() on the right hand (as shown in frekish's answer) side is probably the best solution.
If you are looking for alternatives, you can convert the JSON value to text, using the #>> operator.
select value
from property
where value #>> '{}' = 'IP'
'{}' essentially means "the top level value"
The problem with casting such a value to text is that the double quotes are retained. Another option would therefor be:
select value
from property
where trim('"' from value::text) = 'IP'

Passing a column name as an argument for KDB select query?

I would like to pass a column name into a Q function to query a loaded table.
Example:
getDistinct:{[x] select count x from raw}
getDistinct "HEADER"
This doesn't work as the Q documentation says I cannot pass column as arguments. Is there a way to bypass this?
When q interprets x it will treat it as a string, it has no reference to the column, so your output would just be count "HEADER".
If you want to pass in the column as a string you need to build the whole select statement then use value
{value "select count ",x," from tab"} "HEADER"
However, the recommended method would be to use a functional select. Below I use parse to build the functional select equivalent using the parse tree.
/Create sample table
tab:([]inst:10?`MSFT`GOOG`AAPL;time:10?.z.p;price:10?10f)
/Generate my parse tree to get my functional form
.Q.s parse "select count i by inst from tab"
/Build this into my function
{?[`tab;();(enlist x)!enlist x;(enlist `countDistinct)!enlist (#:;`i)]} `inst
Note that you have to pass the column in as a symbol. Additionally the #:i is just the k equivalent to count i.
Update for multiple columns
tab:([]inst:10?`MSFT`GOOG`AAPL;time:10?.z.p;price:10?10f;cntr:10`HK`SG`UK`US)
{?[`tab;();(x)!x;(enlist `countDistinct)!enlist (#:;`i)]} `inst`cntr
To get the functional form of a select statement, I recommend using buildSelect. Also, reduce the scope of parenthesis, i.e. use enlist[`countDistinct] instead of (enlist `countDistinct).

simple where clause SSRS 2005 parameter not working

this should be a simple thing but I've spent hours to no avail. Basically, I need to look up a salesrep # in a SQL database using the user's Window's user id. The format of the user id is
"Norstar\kjones" and I need the "kjones" portion of it.
using the split function, I am able to pull just the 'kjones' part out:
split(User!UserID,"\").GetValue(1)
I've created a parameter called SlsmnNum and created a dataset to be used to look up the salesrep # using the user id (the slsm_num field is a varchar, not an integer):
select slsm_num from Salesman_Msid where slsm_msid = ''' + split(User!UserID,"\").GetValue(1) + '''
However, I get no results. How can I get the select to work?
alternatively, I tried the following:
in parameter SlsmnNum, I set the default to an expression using:
split(User!UserID,"\").GetValue(1) and this returns 'kjones', as expected.
I created a SECOND parameter (which is positioned BELOW the SlsmnNum parameter), SlsmnNum2, that has a default (and an available) value using a query, which is a dataset containing the following select statement:
select slsm_num from Salesman_Msid where slsm_msid = (#SlsmnNum)
When I run the query on the Data tab, when I type in 'kjones' into the parameter box, it returns '1366', the salesrep # I'm expecting.
But, when I Preview the report, all I get in SlsmnNum2 box is Select a Value and nothing is there (it should return '1366').
Any help would be greatly appreciated!
Try your first approach with Query Text as
="select slsm_num from Salesman_Msid where slsm_msid = '" & split(User!UserID,"\").GetValue(1) & "'"

Get a text value from a form in Access using a VBA module

I currently want to get a value from a form to set it in an SQL query I make in a module under Access using VBA. I tried to use
value = Forms![NameOfForm]![NameOfTextbox]
sqlquery = "...." & value & "....."
It make an error (2450) saying it cannot found the specified form. How can I get the value of this textbox so I could use it in my module?
Thx
Modify your VBA code to ensure the form is open.
DoCmd.OpenForm "NameOfForm"
That should prevent error #2450.
Afterwards, you don't need to store the value of [NameOfTextbox] to a variable, then use that variable to build your SQL statement. You can use its value directly.
sqlquery = "SELECT * FROM YourTable " & _
"WHERE some_field = '" & Forms![NameOfForm]![NameOfTextbox] & "';"
Or embed a reference to the textbox itself (instead of the textbox's value) in the query.
sqlquery = "SELECT * FROM YourTable " & _
"WHERE some_field = Forms![NameOfForm]![NameOfTextbox];"
I assumed some_field is a text data type field, so enclosed the textbox value with single quotes in the first query example. Notice the second example doesn't need the quotes because it refers to the textbox by name rather than its value.
However, should you continue with your original approach (storing the textbox value to a variable), don't name your variable "value" because value can be confused with a property of many objects.