I would like to write a like query in JpaRepository but it is not obeying the like query :
LIKE "abc" and "abc.com" are two string and when we do like :
#Query("Select * from table_name where column_name like ?0'%' ALLOW FILTERING ")
List<entity> findPlaceContainingKeywordAnywhere(String keyword)
it is returning both the values when we pass :
repository.findPlaceContainingKeywordAnywhere("abc.")
Your code will search only string starting with "abc." also I think there is some issue with query
Try this
#Query("Select * from table_name where column_name like %:keyword% ALLOW FILTERING ")
List<entity> findPlaceContainingKeywordAnywhere(#Param("keyword") String keyword)
Related
I have a column string like this
"test.123.test"
"something"
And I want run a query to find string like "test.*.test". In postgresql I use this query:
select * from table where string_column like 'test.%.test'
I run this query in presto but got nothing! It should be related to dot in my string because when I replace string like with something like this 'test.1%1.test' it work but it's not my result.
For a Presto query
string_column like 'test.%.test'
the predicate pushed down into the PostgreSQL connector is similar to:
string_column BETWEEN 'test.' AND 'test/'
however, string comparison are subject to collation and trailing punctuations hits an edge case of Presto/PostgreSQL incompatibility: https://github.com/trinodb/trino/issues/3645
You can workaround this by preventing predicate pushdown into the connector. You can achieve this by adding OR rand() = 42 to your query:
string_column like 'test.%.test' OR rand() = 42
I am trying to run a Athena query which will match multiple values for a column. So the query is like this. So here the event name will be 'n' number of events in string with comma seperated.
eventname = "ExecuteQuery, ErrorOccured, RunningStatus, AbortStatus"
SELECT * FROM "db_name"."table_name" where account='123456' and year='2010' and month='04' and day = '1'
and (eventname like '%Execute%' or eventname like'%Error%' or eventname like '%Running%' or eventname like '%...%')
So basically I want query to perform LIKE and IN operator
How can I write the sql query with operator which has n number of events.
I tried with 'IN' operator. But didnt get desired output.
SELECT * FROM "db_name"."table_name" where account='123456' and year='2010' and month='04' and day = '1'
and (eventname in ('Execute%', '%Error%', 'Running%')
Also tried with below query and it throws error as Function any not registered
SELECT * FROM "db_name"."table_name" where account='123456' and and year='2020' and month='04' and day = '1'
and eventname LIKE ANY (ARRAY['%Execute%', 'Error%']);
you could use regexp_like, for example
select * from table where regexp_like(eventname, 'Execute|Error|Running' )
I'm getting the order by clause as a String from the application configuration.
Example
String orderByString = "NAME DESC, NUMBER ASC";
Now I want to use this order by in jOOQ query:
Result<KampagneRecord> records = repository.dsl()
.selectFrom(KAMPAGNE)
.orderBy(orderByString)
.fetch();
Unfortunately orderBy does not accept a String.
Is there a way to add the order by clause to the query?
You could use the fact that jOOQ does not validate your plain SQL templating, and just wrap your string in a DSL.field(String):
Result<KampagneRecord> records = repository.dsl()
.selectFrom(KAMPAGNE)
.orderBy(field(orderByString))
.fetch();
Of course, you will have to make sure that syntactical correctness is guaranteed, and SQL injection is prevented.
Some edge cases that rely on jOOQ being able to transform your SQL's ORDER BY clause might stop working, but in your simple query example, this would not apply.
An alternative solution, in very simple cases, is to preprocess your string. It seems as though this would work:
String orderByString = "NAME DESC, NUMBER ASC";
List<SortField<?>> list =
Stream.of(orderByString.split(","))
.map(String::trim)
.map(s -> s.split(" +"))
.map(s -> {
Field<?> field = field(s[0]);
return s.length == 1
? field.sortDefault()
: field.sort("DESC".equalsIgnoreCase(s[1])
? SortOrder.DESC
: SortOrder.ASC
);
})
.collect(Collectors.toList());
System.out.println(list);
This list can now be passed to the orderBy() clause.
I would like to generate the query that results for the BeneficiaryID 'ABC123' along with some other inputs if they were also given. Suppose if the currency value is given, I would like to include the Currency condition as well in the JOIN query, so as well the Category. I have the following code snippet in the SOAP UI Groovy script.
query= " CORR.BeneficiaryID LIKE 'ABC123'"
if (currencyValue!=""){
query=query + " and CORR.Currency LIKE '${currencyValue}'"
}
if (CategoryValue!=""){
query=query + " and CORR.Category LIKE '${CategoryValue}'"
}
log.info("Query" + query)
Outputrows = sql.rows("select CORR.Preferred as preferred ,CORR.Category as category,CORR.Currency as currency\
from BENEFICIARY CORR \
JOIN LOCATION LOC on CORR.UID=LOC.UID and ${query}
log.info("Output rows size" + Outputrows.size())
When currency and category are not given, I would like to have the following query run and get me the results.
select CORR.Preferred as preferred ,CORR.Category as category,CORR.Currency as currency\
from BENEFICIARY CORR \
JOIN LOCATION LOC on CORR.UID=LOC.UID and CORR.BeneficiaryID LIKE 'ABC123'
and when the currency and category are given(say USD & Commercial), then the following query.
select CORR.Preferred as preferred ,CORR.Category as category,CORR.Currency as currency\
from BENEFICIARY CORR \
JOIN LOCATION LOC on CORR.UID=LOC.UID and CORR.BeneficiaryID LIKE 'ABC123' and CORR.Currency LIKE 'USD' and CORR.Category LIKE 'Commercial'
All I could see on the result for Outputrows.size() is zero(0).
Can you please correct me where am I doing wrong.
Thanks.
Here is changed script.
Since the issue to just build query, only putting that part remove sql execution part as that is not really the issue.
//Define the values or remove if you get those value from somewhere else
//Just put them here to demonstrate
//You may also try by empty value to make sure you are getting the right query
def currencyValue = 'USD'
def categoryValue = 'Commercial'
def query = 'select CORR.Preferred as preferred, CORR.Category as category,CORR.Currency as currency from BENEFICIARY CORR JOIN LOCATION LOC on CORR.UID = LOC.UID and CORR.BeneficiaryID LIKE \'ABC123\''
currencyValue ? (query += " and CORR.Currency LIKE '${currencyValue}'") : query
categoryValue ? (query += " and CORR.Category LIKE '${categoryValue}'") : query
log.info "Final query is \n ${query}"
You can just pass query to further where you need to run the sql, say sql.rows(query)
You may quickly try Demo
var searchValue = 'shahid';
var query = ("select * from students where name ilike '%"+searchValue+"%'");
This is my psql query, but it is not returning any values. So that I just console the query to know the execution.
The query is executing as:
select * from students where name ilike 'hahid%'
When I capitalize the first letter of search value (Shahid), it's executing perfectly.
If you want to pass in the upper case you should convert the variable searchValue
eg.
var newSearchValue = (select initcatp(searchValue)) ;
This will convert 'shahid' to 'Shahid' Then use this in your query variable.
This, lacking a '%' on the left hand side, will only match thing that start with hahid
select * from students where name ilike 'hahid%'
It's not the same as this
select * from students where name ilike 'Shahid%'
which will match only things that start with Shahid. Now if you want something that will match anything with hahid then you want
select * from students where name ilike '%hahid%'
BTW, your example is extremely insecure if searchValue comes from I/O (user, file, network etc).