Surround Number to interpret as Text - sql

I have a CSV files and one of the fields named period stores number. Now within SSRS, I need to make use of a multi-value parameter, due to the workings of SSRS to use this with a CSV I need to do Join(Parameters!Period.Value, ",").
This issue with this is then my data set has the following query:
="SELECT WarehouseZone, RevenueStream,ClientID,CONVERT(varchar(10),Period) FROM BudgetCSV.csv
WHERE WarehouseZone IN ('" & Join(Parameters!Warehouse.Value,"', '") & "')
AND (RevenueStream IN ('" & Join(Parameters!AnalysisCode.Value,"', '") & "'))
AND (ClientID IN ('" & Join(Parameters!Customer.Value,"', '") & "'))
AND (CONVERT(varchar(10),Period) IN ('"& Join(Parameters!Period.Value, "','") & "'))"
This code works fine for the zone, stream and client, I get a data type mismatch of period.
Is there any way using Powerquery to have the csv think that period is text. I have seen somewhere that by making the column say ="1" it would interpret that as text instead of an integer.

You can either use Text.From or Number.ToText to convert a number value to text.
Text.From(Period)
or
Number.ToText(Period)

EDIT: THE FOLLOWING DOES NOT WORK - Using this method only works on an individual level, luckily there is a better solution
So I mentioned in an earlier post that I could get around using the Join by creating a single parameter for each period, but this was causing an issue where if I selected 10, 1 would also be return because, strings.
This was due to me using the following code on each of these parameters
=iif(Join(Parameters!Period.Value,",").ToLowerInvariant().Contains("10"), "10",-999)
Simple changing that code to the following
=iif(Join(Parameters!Period.Value,",") = 1, 1,-999)
So the issue is solved, but any input on how I could get a more dynamic solution working would be great as there may be times in the future when I need to use a longer list of numbers.

Adding as another answer for posterity to see my mistakes.
There is in fact a much easier way to achieve this.
By adding the above filter onto the dataset you can achieve this without the need for any SQL or any extra parameters. This is probably obvious to anyone who has been working with SSRS for any length of time, but I'm rather new to it myself.

Related

Access Form Macro Where Condition

This is my first post here. Kind of a newbie at Access and I've searched forums to answer my question and tried using similar answers to get my macro working, but I have a syntax error.
I want to click the details button on one form (Employee Profile), and open another form (Employee Training Records) where it will pull up records pertaining to the current employee profile.
In my where condition currently I have:
="[st_no]=" & [st_no] & " AND [emp_id]='" & [emp_id] & "'"
I get a syntax error saying:
(missing operator) in query expression '[st_no]=IEC 62841-2-5 AND [emp_id]='3"
What am I missing?
Since the field st_no looks to be a string, you'll need to enclose the corresponding value with single or double quotes; conversely, since emp_id looks to be an integer, you don't need the surrounding quotes.
As such, I would suggest:
="[st_no]='" & [st_no] & "' AND [emp_id]=" & [emp_id]

How to build proper Access SQL LIKE operator expression?

I'm attempting to have a user search through a table in Microsoft Access 2010, but the SQL command isn't working. The command that loads and refreshes the table is this:
SELECT Equipment.equipmentID, Equipment.equipmentName, Equipment.model,
Equipment.make, Equipment.equipmentLocation FROM Equipment ORDER BY Equipment.equipmentName;
This works, but when I try to use a variable (or any normal criteria):
searchItem = Me.searchBox.Value
Me.List64.RowSource = "SELECT Equipment.equipmentID, Equipment.equipmentName,
Equipment.model, Equipment.make, Equipment.equipmentLocation FROM Equipment
WHERE Equipment.equipmentName LIKE '%searchItem%' ORDER BY Equipment.equipmentName;"
I've also tried something like "%10%" instead of the searchItem variable, but the command has the table come up blank with no errors. I suspect the problem is with the Equipment.eqiupmentName as the column name, but I can't quite figure out what's wrong here.
Here's a quick look at what the table looks like:
Try this:
Me.List64.RowSource = & _
"SELECT Equipment.equipmentID, Equipment.equipmentName," & _
" Equipment.model, Equipment.make, Equipment.equipmentLocation FROM Equipment" & _
" WHERE Equipment.equipmentName LIKE '*" & searchItem & "*'" & _
" ORDER BY Equipment.equipmentName;"
User rjt011000 has a valid solution, but I recommend using & for string concatenation in VBA (and Access). For an explanation of + and & see this thread.
Access will not recognize or substitute VBA variables inside an SQL statement. Furthermore, the LIKE operator is fed an SQL string value in this case (inside single quotes... which are inside the double quotes), so even if a VBA variable could be referenced directly inside SQL, Access does not interpret any such thing inside a string value.
Regarding the Access SQL LIKE operator, the multi-character matching pattern is * rather than %. Access also recognizes the operator ALIKE which does indeed honor the ANSI pattern %. See LIKE operator docs and this thread regarding ALIKE.
To be more thorough, the string delimiters and LIKE pattern-matching character should be escaped if you don't want the user inadvertently injecting invalid characters that cause errors in the SQL. Following is an example of escaping a couple of them. There are more elegant ways to handle this for all special characters, but the code and technique are beyond the scope of this answer.
...'" & Replace(Replace(searchItem, "*", "[*]"), "'", "''") & "'...
For the record, although Access SQL will not substitute a VBA variable, it will recognize and call a public VBA function. Normally such a public function must be defined in a normal module, but in context of a form's Record Source query, a form-module method can sometimes be called.
One last technique... It is possible to reference a form control's value directly in SQL. This can be very convenient and reduce extra code, but there are a couple caveats:
The form must of course be open, otherwise Access will interpret the reference as an unknown parameter and display a prompt. This will of course not be a problem if the SQL is always in context of the same form.
Access will sometimes automatically refresh the query when such a referenced control is changed, but it is not always guaranteed. The "timing" of automatic refreshes might not be immediately intuitive. You can call the Refresh method on the control or subform from various form events to force the query to refresh after the value is changed.
Notice that in the following example, the string concatenation is inside the VBA string, so that the concatenation actually happens in context of SQL and not beforehand like in the first code snippet. There is no problem with this, just something to consider since this entire answer revolves around proper string interpretation and concatenation.
But really, the same concern exists for un-escaped pattern-matching characters in the user text. Rather than making the SQL text long and ugly with calls to Replace(), instead create a custom function (e.g. EscapePattern()) that does this for any text and then wrap the control reference with that function. The example does this, although I don't include the code for the special function. Such a function could also be used in the first VBA code snippet to simplify building the SQL text.
Me.List64.RowSource = & _
"SELECT Equipment.equipmentID, Equipment.equipmentName," & _
" Equipment.model, Equipment.make, Equipment.equipmentLocation FROM Equipment" & _
" WHERE Equipment.equipmentName LIKE ('*' & EscapePattern(Forms![Form Name]![Control Name]) & '*')" & _
" ORDER BY Equipment.equipmentName;"
There is always more! Did you see the VBA line continuation in my example? It makes the SQL text much easier to view within VBA editor.
I suspect you are not setting your searchItem variable correctly in the SQL string. I am not too familiar with access string concatenation but try separate the searchItem out of the SQL string and then checking if your RowSource has the value you suspect.
Me.List64.RowSource = "SELECT Equipment.equipmentID, Equipment.equipmentName,
Equipment.model, Equipment.make, Equipment.equipmentLocation FROM Equipment
WHERE Equipment.equipmentName LIKE '%" + searchItem + "%' ORDER BY Equipment.equipmentName;"

Invalid Identifier for column showing in schema

I am having trouble querying a column in an Oracle view that shows up when I pull the schema. In fact, it appears as column number 2 when I list it out.
The error indicates ORA-00904 invalid identifier, which from what I have read says the column name I am referencing is incorrect, but I have copied the name directly from Oracle Developer, MSAccess, and the datareader.Schema, all of which appear to have no issues getting to that column.
If I query the column just using a linked table in MSAccess the data also comes right up. All of the examples I have seen referencing a similar issue in which the field is incorrectly typed, which though I acknowledge is still a possibility, seems unlikely in this case given the direct copy from the column list as mentioned.
Other solutions mention putting the name in double quotes, which I am uncertain how to do in VB.NET or if it is even necessary.
Code below:
'Open And Query
oledbCon.ConnectionString = strCon
oledbCon.Open()
oledbCom.Connection = oledbCon
oledbCom.CommandType = CommandType.Text
oledbCom.CommandText = "SELECT AREA_CODE FROM CSITAPPS.DAYSIN_1057"
oledbda.SelectCommand = oledbCom
oledbda.Fill(gdt)
I was able to find a solution working with a co-worker for a couple days. The issue stems from the fact that Oracle Column references are Case Sensitive. Because of this the double quotations were required, which is tricky for VB.net given quotation marks indicate and encapsulate String entries. The solution was to break the string and concatenate chr(34) into it. That in combination with ensuring that the column reference case matched what was in the table it came right up.
"SELECT " & Chr(34) & "Area_Code" & Chr(34) & " FROM CSITAPPS.DAYSIN_1057 ORDER BY " & Chr(34) & "Area_Code" & Chr(34) & " DESC;"

Access 2010 Issues with "IS NULL" in Select statement [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am a novice programmer attempting to build a database for work in Access 2010 in an effort to better manage our data. Our filing system involves documents nested under one another in various branching subjects. The branches are all varying lengths, in my database each record always has a collection number, series number, and file unit number but there are several sub-series numbers that they may or may not have depending on how many subjects branch off of the previous subject. All of this information collects in a File Unit table.
I'm working on setting up a search form with a combobox for collection number, series number, each possible sub-series number, and then file unit number, so that from the first combobox the user chooses their collection number which then triggers a query that fills the series combobox with all series numbers available for that collection number, and so on and so forth down the line. My problem is with the file unit number combobox.
I need the file unit number combobox to fill with options after the series number is selected and update with each subsequent selection of sub-series number to only display file unit numbers from the file unit table that match the selected values and have null values for sub-series comboboxes that have nothing selected.
I've written a sub procedure that calls after the user exits each combobox when certain conditions are met. In the sub procedure, if then statements check each combobox, if there's a value it's stored in a corresponding string variable and if not the variable is set to IS NULL.
This is the code I'm using to fill the file unit number combobox:
File_Unit_Number.RowSourceType = "Table/Query"
File_Unit_Number.RowSource = "SELECT File_Unit.File_Unit_Number,
File_Unit.File_Unit_Name FROM File_Unit WHERE
(((File_Unit.Collection_Number) " & cmbCollection & " AND
(File_Unit.Series_Number) " & cmbSeries & "
AND(File_Unit.Subseries_1_Number) " & cmbSubseries_1 & "
AND(File_Unit.Subseries_2_Number) " & cmbSubseries_2 & "
AND(File_Unit.Subseries_3_Number) " & cmbSubseries_3 & "
AND(File_Unit.Subseries_4_Number) " & Subseries_4 & "
AND(File_Unit.Subseries_5_Number) " & cmbSubseries_5 & "));"
I've run tests to make sure that my if-then statements are triggering correctly and that the SQL string is printing correctly. When I remove the fields that have null values (ie. delete everything except series_number) from the WHERE part of the statement and test then items populate my file unit combobox. Problem is these numbers are all of the file unit numbers regardless of value in all of the sub-series columns.
What am I doing wrong? And is there an easier way to get the results I need?
In your code there are some weak points (and do what sstan suggested: Check the generated code before the execution!)
What is in your variables, if the value is not null? If I understand this correctly, you store the text "IS NULL" in your variables. If there is a value, your variables should contain something like "='SomeValue'" (Attention with strings, which must be in quotes, and formats like . or , in decimals or with datetime literals)
The paranthesis should include the full expression AND (FileUnit.Subseries_4_Number=123). You might let them away entirely as there are only ANDs.
And check for the blanks:
AND(File_Unit.Subseries_1_Number) " & cmbSubseries_1 & "
AND(File_Unit.Subseries_2_Number) " & cmbSubseries_2 & "
...might result in
AND(File_Unit.Subseries_1_Number) ISNULLAND(File_Unit.Subseries_2_Number) 123
(Look at the paranthesis, the missing blank before the AND and the missing = if your variable carries the value only.
UPDATE Beware of sql injection!
Anyway SQL statements with values should not be created by concatenating strings but should use parameters...
Sorry to waste anyone's time! I sorted out the answer on my own. It turned out that some of the fields were not NULL but empty strings. I changed the string that was being saved to the variable when there was no value to:
[table].field IS NULL or [table].field = ""

VB - simply link a Data Grid View to display data that was selected from a ComboBox

I just want to display data in a DataGridView (from SQL - already made the connection) based on what is selected in a ComboBox (data that is also coming from SQL). The 2 are separete on the form. I am using VB 2010.
This doesn't work for me:
objCommand2.CommandText = "SELECT ProductID, Name, Color, Size, ListPrice FROM SalesLT.Product WHERE ProductCategoryID = " & cbCategory.SelectedValue
It gives me an error "invalid syntax around '=' "
Thank you!!!
Catalin
Try this
cbCategory.SelectedText
and look into passing command parameters to a stored procedure in the future.
Have you tried to assign the long string to a variable first to see if it really looks like a SQL statement. As Saif suggested, it may be something related to the value of the combobox. What I usually do is hardcode a SQL statement in a string to make the function work and then replace it with the dynamic string.
Dim s As String = "SELECT ProductID FROM SalesLT.Product WHERE ProductCategoryID=1"
One simple step at a time
Check that cbCategory.SelectedValue is indeed a numeric value. If it is, say "XXY", then you would need to code
WHERE ProductCategoryID = '" & cbCategory.SelectedValue & "'"
(watch out for the difference between ' and " !