name matching with escape sequence in commons dbutils - sql

I am using dbutils QueryRunner on a product table to get (ID, Name) from the table where the query is as follows:
select ID, NAME
from PRODUCT
where NAME LIKE ?
LIMIT 5;
I am getting
ERROR 203 (22005): Type mismatch. BOOLEAN for null Query
while if i run the same query with NAME = ? , it works fine.
Can you please let me know what is wrong with the syntax and also how can i use the escape sequence for e.g
WHERE NAME LIKE 'XYZ%'

Related

Fuzzy matching a List in SQL

Have a set of data that is riddled with duplicates. The company names are either written as their Workplace name, e.g. Amazon, or the legal name, e.g Amazon.com Inc. Both entries have information I need.
Issue with the name is I am running a subquery to generate the correct list of companies to search for, however the LIKE function only seems to work for a set list.
FROM CRM.organizations
WHERE name LIKE (SELECT org_name FROM CRM.deals WHERE UUID IS NOT NULL AND status = 'won')```
The code above returns the following error: 'Error: Scalar subquery produced more than one element'
Trying to understand if there is a function that can help, or I will need to create a list manually with: 'companyAinc';'companyBllc';....
Well, the LIKE operator doesn't support directly passing a list a values to match with, you can use the CROSS APPLY to map each value to fuzzy match in your statement.
You can refer to this example for the same to use multiple clauses with LIKE operator.
On the other hand you can also try using User-defined functions/routines, in which you can map all your the returned values with the LIKE and OR operators and return your required query as a string.
FROM CRM.organizations
WHERE name in (SELECT org_name FROM CRM.deals WHERE UUID IS NOT NULL AND status = 'won');
FROM CRM.organizations
WHERE exists (SELECT 1 FROM CRM.deals WHERE UUID IS NOT NULL AND status = 'won' and organizations.name like deals.org_name );

SQL Parameter to Include All on ID Column

I'm just taking a look at the following query
select * from tablename
where id like '%%';
So that it can handle parameters to include all of the data or filtered data like bellow
select * from tablename
where id like '%1%';
Which is fine for most parameters I use but this seems wrong for an ID because it will return all data that has IDs containing 1 which I don't want
To get around this I can only append the where clause if the ID is given but that seems like a pain in the butt
Is it possible to use a different type of where clause so that a wildcard can be used in a where equals clause instead of a where like clause, example
select * from tablename
where id = '*';
So that the same query can be used to return all or filtered data? Pass parameter '*' for all or parameter '1' for ID 1 specifically
(I'm not sure if it matters for this case but I'm using PostgreSQL 9.6.12 in this example)
This would often be expressed as:
where (id = :id or :id is null)
null is the "magic" value that represents all rows.

using % in IN operator of sql

I have a query like this
select * from table_employee where name in ('Jack','Jon','Jade');
and it gives me three results.
but I want to use % with the names I mentioned in query like:
select * from table_employee where name in ('%Jack%','%Jon%','%Jade%');
The query execution is completed but now I get no results. Whats wrong here and how can i use % in IN operator?
You should use LIKE combined with OR, IN does not support wildcards
select * from table_employee where name LIKE '%Jack%'
OR name LIKE '%Jon%';
The in keyword does not work like that. You can use a like operand and bind the three values with or
example
SELECT *
FROM table_employee
WHERE NAME LIKE '%Jack%'
OR NAME LIKE '%Jon%'
OR NAME LIKE '%Jade%';
Convert it to an OR:
SELECT *
FROM table_employee
WHERE NAME LIKE '%Jack%'
OR NAME LIKE '%Jon%'
OR NAME LIKE '%Jade%';
If your fields data are exact what you want get in the result is no needed to use like . You should use like only when you don't know the data in a field but you know that it could contains a string Jack for example .
I'm sayng this because if you have an index on the field where you use LIKE it will be not used , so you will get performance problem .

select column in hibernate does not work but * does

I have this:
Query q = em.createNativeQuery("select * from TRANSAKTION",Transaktion.class);
List<Transaktion> trans = q.getResultList();
While this works, using a column name like id doesn't although it exists in database and generated entity class.
Other tables work fine it's just that one and with all columns. When I use * and .getId() it returns the correct id.
What could possibly be the problem?
The error message is:
SQL Exception: invalid column name

Basic SQL Select in Postgresql fails

I am doing a select * on a postgresql table, and everything looks good. But if I do:
SELECT Name from People
It says:
ERROR: column People.Name does not exist
SQL state: 42703
Character: 8
But the name column shows up during select *. I've tried:
SELECT People.Name from People
as well, with the same result. Am I missing something? It should be pretty easy to do this in any other database.
Try putting quotation marks around the column name, i.e. "Name"
PostgreSQL converts everything to lowercase.
If you asks for this:
CREATE TABLE People
{
id SERIAL,
Att1 varchar(100)
-- constraints
};
SELECT Name FROM People;
You should get all the information, because pgsql converts this to
CREATE TABLE people
{
id SERIAL,
att1 varchar(100),
-- constraints
};
SELECT name FROM people;
If you built your table with pgAdmin and created field with mixed casing, you will need to quote them to be sucessfull, like this:
SELECT "Att1" FROM people
Name is a keyword, so it might not be handled well in this case. The best thing to do would be to alias the table like this:
SELECT Name from Peoplep
and then use the p to select the column:
SELECTp.Namefrom People p