how to use ignore case in spring JPA #query? - sql

I have query as below:
#Query("SELECT b FROM Brand b WHERE b.name1 LIKE %:name1% or b.name2 like %:name2%")
List<Brand> findSome(#Param("name1") String name1, #Param("name2") String name2);
I want to ignore case so that I tried to modified it
#Query("SELECT b FROM Brand b WHERE lower(b.name1) LIKE lower(%:name1%) or lower(b.name2) like lower(%:name2%)")
But this looks like don't work, how can I do?

The problem is probably the syntax of your JP-Query. Try to use this:
lower(b.name1) LIKE concat('%', lower(:name1), '%')
Further reference: http://openjpa.apache.org/builds/1.2.3/apache-openjpa/docs/jpa_langref.html#jpa_langref_like

Related

SQL - Search multiple columns

I'm trying to search multiple columns for similar/matching values, while ensuring the "UID" is unique.
This seemed simple, but the following query ignores the "UID" and will return anything that matches under "SalesRef, "CustomerPO" or "Ref".
Any ideas why this would be?
SELECT * FROM OrderHeader
INNER JOIN DespatchDetails ON DespatchDetails.Ref = OrderHeader.Ref
INNER JOIN OrderStatus ON OrderStatus.Ref = OrderHeader.Ref
WHERE OrderHeader.UID = '$uid'
AND OrderStatus.SalesRef LIKE '%$search%'
OR OrderStatus.CustomerPO LIKE '%$search%'
OR OrderStatus.Ref LIKE '%$search%'
ORDER BY OrderHeader.OrderDate DESC";
There is a problem in the WHERE condition of yours. Consider to use brackets every time when you're using OR in WHERE part of the query
Let's have a closer look to your version
...
WHERE OrderHeader.UID = '$uid'
AND OrderStatus.SalesRef LIKE '%$search%'
OR OrderStatus.CustomerPO LIKE '%$search%'
...
To the database it says: get me all the lines where
WHERE OrderHeader.UID = '$uid'
AND OrderStatus.SalesRef LIKE '%$search%'
or
OrderStatus.CustomerPO LIKE '%$search%'
it means when this "OrderStatus.CustomerPO LIKE '%$search%'" condition is true, it will return a row without checking for the UID column and so on.
As far as I can understand the logic, all you need is to update the WHERE part of the query with the following
WHERE OrderHeader.UID = '$uid'
AND (OrderStatus.SalesRef LIKE '%$search%'
OR OrderStatus.CustomerPO LIKE '%$search%'
OR OrderStatus.Ref LIKE '%$search%')
ORDER BY OrderHeader.OrderDate DESC";

How to use SQL function CONCAT with LIKE

I confirmed that the columns in the SQL table contain the results I'm searching for (the results being NA), but when I use the query below, no results are returned.
Could I get some help with this please?
SELECT DISTINCT *
FROM [DB_NAME].[dbo].[TABLE_NAME]
WHERE BECDescription like '%Time Warner%' AND
(CONCAT(PhysicalAddress1, PhysicalCity, PhysicalStateProvince) like ('%NA% %NA% %NA%')
) AND
PhysicalCountry like '%NA%' AND
CarrierName like '%NA%' AND
CurrNetChargeAmt = 1326.00
I'm a little lost on why you are using CONCAT() here. Doesn't this do what you want?
WHERE BECDescription like '%Time Warner%' AND
PhysicalAddress1 like '%NA%' AND
PhysicalCity like '%NA%' AND
PhysicalStateProvince like '%NA%' AND
PhysicalCountry like '%NA%' AND
CarrierName like '%NA%' AND
CurrNetChargeAmt = 1326.00
It is certainly simpler to understand.
You concat PhysicalAddress1, PhysicalCity, PhysicalStateProvince but you add spaces between the '%NA'.
Try
SELECT DISTINCT *
FROM [db_name].[dbo].[table_name]
WHERE BECDescription like '%Time Warner%' AND (CONCAT(PhysicalAddress1, ' ', PhysicalCity, ' ', PhysicalStateProvince) like ('%NA% %NA% %NA%')) AND PhysicalCountry like '%NA%' AND CarrierName like '%NA%' AND CurrNetChargeAmt = 1326.00

slick how to execute LIKE query

I am playing with PlayScala and Slick. But stocked at how to execute Like query in Slick2. Any Documentation will be more helpful
SQL:
SELECT * FROM MyTable WHERE columnA LIKE "%s%";
Try This
val query = for {
coffee <- Coffees if coffee.name like "%expresso%"
} yield (coffee.name, coffee.price)
Will generate SQL like
SELECT name, price FROM coffees WHERE NAME like '%expresso%';
REF

SQL two or and one and in the where function

I have this search function that has two column. The search button is working except that it includes column that are hidden. How do I fix the SQL? This is my SQL:
SELECT app_keyword.id, app_keyword.visibility,app_keyword.keyword,application_table.app_name
FROM appwarehouse.APP_KEYWORD
INNER JOIN application_table
ON application_table.id = app_keyword.app_id
WHERE app_name like '%$search%' OR keyword like '%$search%'
AND app_keyword.VISIBILITY != 'hidden';"
It keeps on displaying columns that are hidden. How do I fix this?
This issue is due to priority of logic operators (AND has precedence over OR). Try the following :
WHERE (app_name LIKE '%$search%'
OR keyword LIKE '%$search%')
AND app_keyword.VISIBILITY != 'hidden'
In your query it is executed as
WHERE app_name LIKE '%$search%'
OR (keyword LIKE '%$search%'
AND app_keyword.VISIBILITY != 'hidden')

How to put this Query into Linq (C#)?

I'm able to do:
var codeStation = from Code in ent.Role
where Code.Code.StartsWith("S_")
select Code;
(ent: being my Entity for my Database)
That gives me :
S_ANC
S_ATL
S_BNA
S_BOS
S_BRU
S_CLT
.....
S_YXE
S_YXY
S_YYC
S_YYG
S_YYT
S_YYZ
How can I accomplish the equivalent of the following SQL query?
SELECT Substring(Codes,3,6)
FROM Role
WHERE Codes LIKE 'S%'
Thanks!
var codeStation = from Code in ent.Role
where Code.Code.StartsWith("S_")
select RoleName.Substring(3,6);
Your LINQ query can select any legal C# expression you want, including method calls on the field names. So, you can do something like this:
var codeStation = from Code in ent.Role
where Code.Code.StartsWith("S_")
select Code.RoleName.SubString(3,6);