I created a view for report. I imported data to the report. In columns null values are also present. If i filter one row using filter in vb.net the null values of row cannot be displayed.
For example, column names are ID, Name, Number, Place. In this some of the places number has null values I include filter ID, Name ,Number,Place. If I filter using ID the null values of number cannot be displayed.
This is the code I tried but not filter
IN FORM TextBox1.Text=""
TABLE1BindingSource.Filter = "YOUR FIELDNAME LIKE '" + TextBox1.Text.Equals (String.Empty) + "'")
Expected Result
Table
YOUR FIELDNAME
value1
value2
NULL
value3
NULL
NULL
value4
That filter doesn't make sense. This part:
TextBox1.Text.Equals(String.Empty)
is going to evaluate to Boolean, i.e. True if the TextBox is empty and False if it's not. That means that your filter ends up being:
YOUR FIELDNAME LIKE 'False'
or the like. What you should actually be doing is something like this:
Dim columnName As String
Dim fieldValue As String
'...
TABLE1BindingSource.Filter = String.Format("{0} LIKE '{1}%'", columnName, fieldValue)
Note a number of things there. Firstly, the use of String.Format to make this sort of code more readable. Secondly, the use of the actual value entered by the user and not a Boolean indicating whether that value was empty or not. Thirdly, the use of a wildcard because using LIKE without a wildcard makes no sense.
It should also be noted that that code is only going to work with text fields, because they are the only ones that get delimited by single quotes and the only ones that you can use LIKE with. If you want to filter on a numeric field or some other data type then you'll have to write your code to create the filter differently.
Related
I'm trying to pass a parameter to my query in the criteria field. The parameter is contained on a form in the combobox cboReportSender. cboReportSender contains a list of departments that I run reports for. Also contained in the list is " ALL". When this is selected, I wish the report to display all records. I'm sure that the query is looking for a field literally containing "Like *"
Am I going at this from a wrong angle?
IIf([Forms]![frmRunReport]![cboReportSender]=Trim(" ALL"),"Like *",[Forms]![frmRunReport]![cboReportSender])
Firstly, I wondered whether your combobox selected a numerical value or an alphanumerical value. But when I created a database for testing, neither worked for me in combination with the operator Like * . I needed to typ Like '\*' , with a single inverted comma before and after the asterisk. Even better, adding this inverted comma's works with both numerical and alphanumerical values.
The second thing I needed to change was, to add a second like-operator. In the end, your criterion would become :
IIf([Forms]![frmRunReport]![cboReportSender]=Trim(" ALL"),"Like '*'","Like '" & [Forms]![frmRunReport]![cboReportSender] & "'")
Consider returning the field itself when combobox selects ALL.
SELECT ...
FROM ...
WHERE myDepartmentField = IIf(
TRIM([Forms]![frmRunReport]![cboReportSender]) = 'ALL',
myDepartmentField,
TRIM([Forms]![frmRunReport]![cboReportSender])
)
And if combobox is empty, use NZ to default to field itself.
WHERE myDepartmentField = IIf(
TRIM([Forms]![frmRunReport]![cboReportSender]) = 'ALL',
myDepartmentField,
TRIM(
NZ(
[Forms]![frmRunReport]![cboReportSender],
myDepartmentField
)
)
I want to fetch data from a table Item that has the two fields name and nullable_name, the second one can be empty.
Now I want to add a filter string, so that only results will get fetched that contain that string (in the name field if nullable_name is null, and in the nullable_name field if it is present). So I want one of two WHERE expressions to take place, depending on the situation.
What I tried but didn't work:
SELECT * FROM Item
WHERE
CASE
WHEN Item.nullable_name IS NULL THEN (Item.name LIKE '%filter%')
ELSE (Item.nullable_name LIKE '%filter%')
END
You seem to want coalesce():
where coalesce(item.nullable_name, item.name) like '%filter%'
coalesce() is a function that returns the first non-NULL value.
Try
Where isnull(item.nullable_name, item.name) like '%filter%'
I am attempting to update several values in a table based on several conditions. If a column (let's call it "name") contains some set of strings, I would like to change the name to something else. For example, if name contains two consecutive a's, like "aa", I would like to change it to just "A".
Here is my query that is currently not working:
UPDATE table
SET name = CASE name
WHEN name like "%aa%" then "A"
WHEN name like "%er%" then "E"
ELSE "Z"
END
WHERE name is not null
I know it is a non-sensical example, but I'm not great with coming up with those on the spot. The error I am currently getting is:
"No matching signature for operator CASE for argument types: STRING, BOOL, STRING, BOOL, STRING, STRING at [2:18].
Any thoughts?
Remove name from CASE name and replace double quotes " with single quotes ':
UPDATE table
SET name = CASE
WHEN name like '%aa%' then 'A'
WHEN name like '%er%' then 'E'
ELSE 'Z'
END
WHERE name is not null
P.S. in this example and your question query the word table is the name of the table not the keyword table. That means that in some databases you will have problems and you will have to put this word in quotes.
In this DEMO you can see that the query from my answer will work in MySQL database when you put word table in quotes.
In this DEMO you can see that the query from my answer will work in Oracle database when you put word table in double quotes. Also you will see that the strings(values of columns) can not be between double quotes in Oracle.
Note: Some of the databases will not cause errors if the string(column value) is between double quotes but it is better to use single quotes for this and because you have not tagged a database that you use this are some guidelines that will help you.
Remove the first name in the CASE clause:
UPDATE table
SET name = CASE WHEN name like "%aa%" then "A"
WHEN name like "%er%" then "E"
ELSE "Z"
END
WHERE name is not null
Exactly what The Impaler said,remove the first name in the CASE clause. SQL syntax does not call for the column name directly after case. For example (From W3 schools):
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
WHEN conditionN THEN resultN
ELSE result
END;
https://www.w3schools.com/sql/sql_case.asp
Good luck!
Remove the first instance of the word name
UPDATE table
SET name = CASE
WHEN name like "%aa%" then "A"
WHEN name like "%er%" then "E"
ELSE "Z"
END
WHERE name is not null
There are two ways to use case:
1) CASE WHEN name=1 THEN WHEN name=2 THEN...
2) CASE name WHEN 1 THEN WHEN 2 THEN...
In the first case it looks for the first TRUE expression, in the second for the first expression that is equal to name.
You made a mix of the two, but for the interpreter it is the second form.
Therefore, it was looking for the first expression equal to name. When it reads the first WHEN, it checks whether name is equal to the argument (name like "%aa%")
But (name like "%aa%") is a boolean, name is a string, hence the error.
I have a table like this
col
-----
A,B
The col could be string with comma or array. I have flexibility on the storage.
How to check of col is a subset of either another string or array variable? For example:
B,A --> TRUE (order doesn't matter)
A,D,B --> TRUE (other item in between)
A,D,C --> FALSE (missing B)
I have flexibility on the type. The variable is something I cannot store in a table.
Please let me know if you have any suggestion for Impala only (no Hive).
Thanks
A not pretty method, but perhaps a starting point...
Assuming a table with a unique identifier column id and an array<string> column col, and a string variable with ',' as a separator (and no occurrences of escaped '\,')...
SELECT
yourTable.id
FROM
yourTable,
yourTable.col
GROUP BY
yourTable.id
HAVING
COUNT(DISTINCT CASE WHEN find_in_set(col.item, ${VAR:yourString}) > 0 THEN col.item END)
=
LENGTH(regexp_replace(${VAR:yourString},'[^,]',''))+1
Basically...
Expand the arrays in your table, to one row per array item.
Check if each item exists in your string.
Aggregate back up to count how many of the items were found in the string.
Check that the number of items found is the same as the number of items in the string
The COUNT(DISTINCT <CASE>) copes with arrays like {'a', 'a', 'b', 'b'}.
Without expanding the string to an array or table (which I don't know how to do) you're dependent on the items in the string being unique. (Because I'm just counting commas in the string to find out how many items there are...)
I am working with a table that contains two versions of stored information. To simplify it, one column contains the old description of a file run while another column contains the updated standard for displaying ran files. It gets more complicated in that the older column can have multiple standards within itself. The table:
Old Column New Column
Desc: LGX/101/rpt null
null Home
Print: LGX/234/rpt null
null Print
null Page
I need to combine the two columns into one, but I also need to delete the "Print: " and "Desc: " string from the beginning of the old column values. Any suggestions? Let me know if/when I'm forgetting something you need to know!
(I am writing in Cache SQL, but I'd just like a general approach to my problem, I can figure out the specifics past that.)
EDIT: the condition is that if substr(oldcol,1,5) = 'desc: ' then substr(oldcol,6)
else if substr(oldcol,1,6) = 'print: ' then substr(oldcol,7) etc. So as to take out the "desc: " and the "print: " to sanitize the data somewhat.
EDIT2: I want to make the table look like this:
Col
LGX/101/rpt
Home
LGX/234/rpt
Print
Page
It's difficult to understand what you are looking for exactly. Does the above represent before/after, or both columns that need combining/merging.
My guess is that COALESCE might be able to help you. It takes a bunch of parameters and returns the first non NULL.
It looks like you're wanting to grab values from new if old is NULL and old if new is null. To do that you can use a case statement in your SQL. I know CASE statements are supported by MySQL, I'm not sure if they'll help you here.
SELECT (CASE WHEN old_col IS NULL THEN new_col ELSE old_col END) as val FROM table_name
This will grab new_col if old_col is NULL, otherwise it will grab old_col.
You can remove the Print: and Desc: by using a combination of CharIndex and Substring functions. Here it goes
SELECT CASE WHEN CHARINDEX(':',COALESCE(OldCol,NewCol)) > 0 THEN
SUBSTRING(COALESCE(OldCol,NewCol),CHARINDEX(':',COALESCE(OldCol,NewCol))+1,8000)
ELSE
COALESCE(OldCol,NewCol)
END AS Newcolvalue
FROM [SchemaName].[TableName]
The Charindex gives the position of the character/string you are searching for.
So you get the position of ":" in the computed column(Coalesce part) and pass that value to the substring function. Then add +1 to the position which indicates the substring function to get the part after the ":". Now you have a string without "Desc:" and "Print:".
Hope this helps.