Room Database, Android Studio, Query problem with finding a name the ends with given string - android-sqlite

I am working with Android Studio room database. I have successfully used these queries to get names satisfying the query criteria.
#Query("SELECT * FROM malenamesdb WHERE name LIKE '%' || :search || '%' AND selected ")
fun findByName(search: String?): List<MaleNamesDB>
#Query("SELECT * FROM malenamesdb WHERE name LIKE :search || '%' AND selected ")
fun maleNameBeginsWith(search: String?): List<MaleNamesDB>
But when I tried to create a query that is supposed to find all name that ends with a given string like so.
#Query("SELECT * FROM malenamesdb where name LIKE '%' || :search")
fun maleNameEndsWith(search: String?): List<MaleNamesDB>
The result of maleNameEndsWith() function is an empty list, it dosent find anything. No matter what search string is prowidet.
It does not work, why is this not working and what am I doing wrong?

Related

Springboot JPA Cast numeric substring to string

I have a JPA/Springboot application backed by a Postgres database. I need to get a records that is equal to a substring passed back to the server.
For example:
Select * from dp1_attachments where TRIM(RIGHT(dp1_submit_date_dp1_number::text, 5)) ='00007'
This query works in PgAdmin, but not in the JPA #Query statement.
#Query("SELECT a.attachmentsFolder as attachmentsFolder, a.attachmentNumber as attachmentNumber, a.attachmentName as attachmentName, a.dp1SubmitDateDp1Number as dp1SubmitDateDp1Number,a.attachmentType as attachmentType, a.attachmentDate as attachmentDate, a.attachmentBy as attachmentBy "
+ "FROM DP1Attachments a WHERE TRIM(SUBSTRING(a.dp1SubmitDateDp1Number::text, 5 )) = :dp1Number")
I've also tried CASTing the parameter like this:
#Query("SELECT a.attachmentsFolder as attachmentsFolder, a.attachmentNumber as attachmentNumber, a.attachmentName as attachmentName, a.dp1SubmitDateDp1Number as dp1SubmitDateDp1Number,a.attachmentType as attachmentType, a.attachmentDate as attachmentDate, a.attachmentBy as attachmentBy "
+ "FROM DP1Attachments a WHERE TRIM(SUBSTRING(CAST(a.dp1SubmitDateDp1Number as string, 5 ))) = :dp1Number")
but the application won't even run, and returns an error that the query isn't valid.
If I make no attempt to cast it, I get an error that function pg_catalog.substring(numeric, integer) does not exist
UPDATE
I've also tried creating a native query instead but that also doesn't seem to work.
List<DP1AttachmentsProjection> results = em.createNativeQuery("Select * FROM dp1_attachments WHERE TRIM(RIGHT(CAST(dp1_submit_date_dp1_number as varchar),5)) =" + dp1Number).getResultList();
In place of varchar I have also tried string and text.
Errors come back similar to ERROR: operator does not exist: text = integer. Its like the CAST is being ignored and I'm not sure why.
I also tried the following as a native query:
em.createNativeQuery("Select * FROM dp1_attachments WHERE TRIM(RIGHT(dp1_submit_date_dp1_number::varchar),5)) =" + dp1Number).getResultList();
and get ERROR: syntax error at or near ":"
FINAL SOLUTION
Thanks to #Nenad J I altered the query to get the final working solution:
#Query(value = "SELECT a.attachments_Folder as attachmentsFolder, a.attachment_Number as attachmentNumber, a.attachment_Name as attachmentName, a.dp1_Submit_Date_Dp1_Number as dp1SubmitDateDp1Number,a.attachment_Type as attachmentType, a.attachment_Date as attachmentDate, a.attachment_By as attachmentBy FROM DP1_Attachments a WHERE TRIM(RIGHT(CAST(a.dp1_Submit_Date_Dp1_Number as varchar ), 5 )) = :dp1Number", nativeQuery = true)"
Default substring returns a string, so substring(integer data,5) returns a string. Thus no need for the cast.
#Query("SELECT * FROM DP1Attachments a WHERE TRIM(SUBSTRING(a.dp1SubmitDateDp1Number, 5)) = :dp1Number")
But I recommend in this case use native query like this:
Put this code in your attachment repository.
#Query(value="SELECT * FROM DP1Attachments AS a WHERE TRIM(SUBSTRING(a.dp1SubmitDateDp1Number, 5 )) = :dp1Number", nativeQuery=true)
Be careful with the column's name.

Postgresql - How to make NOT LIKE + SELECT statement, comparing two columns between tables

Hit a bit of a brickwall here, I haven't found anything that seems to work.
SELECT *
FROM glsltransaction gls
INNER JOIN glhistory h ON gls.sltrxid = h.schedxrefid
INNER JOIN apvendor ap ON ap.vendorid = gls.acctid
INNER JOIN glchartofaccounts coa USING (acctdeptid)
WHERE h.description <> gls.description
AND h.description not like || ap.name || '%'
AND gls.description <> ''
This line here AND h.description not like || ap.name || '%' is what i'm having issues with.
I get the following error when trying to run that statement above:
'Error occurred in running query from editor : ERROR: operator does not exist: || text Hint: No operator matches the given name and argument type. You might need to add an explicit type cast. Position: 563'
I'm effectively wanting it to function like a.column not like b.column%
Any help will be greatly appreciated, thanks!

How to search in SQL for cloumn with asterisk?

I try to create advanced search to my database.
I want to do something like that: if the user type for search = overf**w
and I have in my database an cloumn that his value = overflow - show him.
this my code:
$name = str_replace('*', '_', $name);
SELECT name FROM table WHERE name LIKE CONCAT('%', ?, '%')
its not working, I dont know what the problem.
You can't use LIKE in this situation, you need to use REGEXP() to do a wildcard search. Replace * or ** with .*. To only return names that starts with the given value use ^ at the beginning of the regular expression
SELECT name
FROM actors
WHERE name REGEXP('^overf.*w')
I don't know php but your $name parameter should be set like this (in pseudo code)
$name = '^' + replace($name, '**', '.*')

return one string from colums data

I've result set from query select * from personal."phoneNumbers" like this
prefix
pref |number
-----|--------
"12 "|"4589524"
"077"|"7090701"
"050"|"2561024"
But I want to return data like
(12) 4589524;(077) 7090701; (050) 2561024
How to do this with postgresql ?
You can use the string operators and functions to construct a single phone number in the format that you want:
'(' || btrim(pref) || ') ' || number
This, obviously, yields a string for each record that you process. You can then use the aggregation function string_agg() to string (no pun intended) the extended phone numbers from all the records together into one, with the appropriate separator between phone numbers:
SELECT string_agg('(' || btrim(pref) || ') ' || number, '; ') AS pref_number
FROM personal."phoneNumbers"

LIKE with % on column names

Here is my query that results in a syntax error:
SELECT *
FROM account_invoice,sale_order
WHERE sale_order.name LIKE %account_invoice.origin%
The account_invoice.origin field contains the text of sale_order.name, plus other text as well, so I need to match sale_order.name string anywhere in the account_invoice.origin string.
I'm using PostgreSQL 8.4.
Try this
SELECT *
FROM account_invoice,sale_order
WHERE sale_order.name LIKE '%' || account_invoice.origin || '%'
% needs single quote because the pattern is a string.
|| is the operator for concatenation.