DB2 SELECT Statement with LIKE using "" - sql

I need to build a query to select records in a column that stores my app's payloads in DB2.
The column is set as varchar(2000).
The String that I need to search is:
userName": ""
So a LIKE statement on my select would be something like:
WHERE MyTable.column LIKE '%userName": ""%'
My problem is with the "" inside the LIKE ... it is not bringing the records

Related

SQLite, LIKE column name

What query could I use in sqlite to get the names of columns beginning with (for example) "thing" in a DB
Such as if they were formatted like this:
"thing_column1"
"thing_column2"
"thing_data"
etc.
You can use pragma_table_info() with the table's name:
SELECT name
FROM pragma_table_info('tablename')
WHERE name LIKE 'thing%'
You can use this query:
SELECT GROUP_CONCAT(name) AS columns
FROM pragma_table_info('tablename')
WHERE name LIKE 'thing%'
which returns only 1 column columns with a string value like 'thing_column1,thing_column2,thing_column3' and you can use it to construct a SELECT statement in your application.

Case insensitive LIKE clause in Pervasive SQL

I'm attempting to write a query in Pervasive SQL which matches on a "LIKE" clause, but without case sensitivity.
As an example, I want the following query to match both "john", "John", and "JOHN". Currently, this is case sensitive.
SELECT name FROM table WHERE name LIKE ?
In T-SQL, I would just wrap UPPER around both parts of the WHERE clause, like this:
SELECT name FROM table WHERE UPPER(name) LIKE UPPER(?)
However, placing any functions to the right of the WHERE clause fails with a syntax error.
How can I achieve a case-insensitive search?
The only way I can think of is to change the case of the value that's coming in before you create the SQL Statement. Something like this C#ish code:
string value = "world";
sql = "SELECT name FROM table WHERE name LIKE = '" + value.ToUpper();
Or, even better use Parameters and set your value before you set the parameter.
You're right, having a function on the right side the LIKE will cause a Syntax Error. You might want to report it as a bug to Actian.

PostgreSQL: return row where any column value like variable

I am trying to have the user search for a value in a SQL table, and the user is returned with any row that contains that value. At the moment, I can make it work such that the code is:
SELECT * FROM table WHERE lower('foo') in (lower('col1'),lower('col2'),etc)
However, I would like it to be able to search every column and return any row LIKE 'foo'. For instance,
SELECT * FROM table WHERE (lower('col1'), lower('col2'), etc) like lower('%foo%')
But that doesn't work.
Any suggestions?
I believe you need to use multiple WHERE clauses instead of grouping them all into one statement. Try this:
SELECT * FROM table
WHERE lower(col1) like lower('%foo%')
OR lower(col2) like lower('%foo%')
OR etc like lower('%foo%')
You can convert the whole row to a string and then use LIKE on the result of that:
select *
from the_table
where lower(the_table::text) like '%foo%';
the_table::text returns all columns of each row as a comma separated list enclosed with parentheses, e.g. (42,Arthur,Dent). So the above is not 100 identical to a LIKE condition applied on each column - but probably does what you want.

Derby SELECT statement failing to find a specific string

I have a table that contains a column called ATEST, in a schema called TESTSCHEMA, that is in a database called SESSION.
I am trying to find records whose ATEST column has a string called "MyTest".
I am using the following SQL:
final String query = "SELECT * FROM SESSIONDATA.SESSIONS WHERE SESSION.TESTSCHEMA.ATEST = 'MyTest'";
Now I have placed several records whose ATEST column contains "MyTest". I use JDBC in the usual way:
device = aConnect.prepareCall(query);
ResultSet result = device.executeQuery();
Note that, for brevity, try and catch code is being omitted since I am not getting any exceptions thrown.
For some reason, I keep getting what amounts to empty result sets. I cannot get this statement to find any records despite the fact that I have several records in the database where ATEST = 'MyTest'!
Have I found a bug in Derby? Searches without WHERE clauses or where WHERE clauses look for numbers seem to work without problems. Why doesn't a WHERE clause that looks for a string find the strings, despite the fct that the strings are actually in the database???
Someone please advise.
Try sending like
final String query = "SELECT * FROM SESSIONDATA.SESSIONS WHERE SESSION.TESTSCHEMA.ATEST = '''MyTest'''";
It should hit DB like
SELECT * FROM SESSIONDATA.SESSIONS WHERE SESSION.TESTSCHEMA.ATEST = 'MyTest'
instead of
SELECT * FROM SESSIONDATA.SESSIONS WHERE SESSION.TESTSCHEMA.ATEST =MyTest
I don't know Derby. But your query looks strange:
You say the database is called SESSION. So this is where you connect to. I wouldn't expect it's name in a query.
Your schema is called TESTSCHEMA. So this is where the tables reside in. ATEST is a column. So aren't you missing the table name between schema and column in TESTSCHEMA.ATEST?
You select from SESSIONDATA.SESSIONS. What is SESSIONDATA suddenly? It's neither of the names you mentioned. What is SESSIONS? Is that the table name?
I would expect a query about like this:
SELECT * FROM testschema.sessions WHERE atest = 'MyTest'
(Where the schema name might even be superfluous in case it's the default schema for your connection.)
Try using a numeric value for referencing in place of 'Mytest'
Replace
SELECT * FROM SESSIONDATA.SESSIONS WHERE SESSION.TESTSCHEMA.ATEST = 'MyTest'
with
SELECT * FROM SESSIONDATA.SESSIONS WHERE SESSION.TESTSCHEMA.ATEST = 123

assigning IS NULL to a string before querying DB

I have an excel spreadsheet with 15 or so fields. Wat I'm doing is, i open it, grab a row of data, then check each row for a value. Then using a few criteria I go look up to see if this Client value is already in the DB. As Example
Some of the fields mind be empty. Basically after checking some of the fields, I use them to check if that record exists already in DB. the problem arises when some fields are empty in which case when I query sql server it looks something like...
Select * from TblA where Company='Apple' and CompanyAdd ='Cupertino' and City=''
Because City = '' - it doesnt not find anything in SQL. The only thing that works is
and City is NULL
How am I able to programmatically assign that to a variable like CITY?
it is a string and the field in SQL is varchar
EDIT:
I want to be able to do something like this..... (as example)
if city = "" then
'I need this here to be so that....
city IS NULL
End if
So that when I query db it looks something like...
Select count(*) from TblA where City is Null
Is somethng like that possible?
You can use COALESCE for this purpose.
SELECT *
FROM TblA
WHERE COALESCE(Company, '')='Apple'
AND COALESCE(CompanyAdd, '') = 'Cupertino'
AND COALESCE(City, '') = ''
Keep in mind that the performance of this query will most likely not be stellar.