In my table of mariaDB database, I have a column name and It is going to store a person's name in any language.
There are 2 names änkür and ankur in table.
If I run below query, it is returning 2 rows
select * from table_name where name ='ankur';
As I want to return single result I changed my query like:
select * from table_name where name = BINARY 'ankur';
What is the JPA method signature or alternative solution for the above query?
Related
In Oracle, Datatype is vArchar and we have some ID's stored in format 0000123. From source we are getting ID as 123 or 1234 with preceding 0. In some cases data is stored simply as 123.
In SQL Query i can simply use
Select * from Table where ID = 123 (It wil fetch even if 000123 id is present)
Is there way to achieve it using Named Query through hibernate as currently in oracle, it is String against Varchar and when searching 123 does not return correct results.?
The query translates automatically to the following:
Select * from Table where TO_NUMBER(ID) = 123
so you can use that instead.
You can also use:
SELECT * from Table where LTRIM(ID,'0') = LTRIM(123,'0')
if there are non numerical values in the ID column.
If you are getting an error while applying Radagast81's answer then try to convert the number to varchar using to_char() as following:
SELECT
*
FROM
EMPS
WHERE
LTRIM(EMP_NAME, '0') = TO_CHAR(LTRIM(123, '0'));
You can also use LPAD to achieve the same:
SELECT
*
FROM
EMPS
WHERE
EMP_NAME = LPAD(123, LENGTH(EMP_NAME), '0'));
Cheers!!
If you can assign a default number which does not exists in table, you can use it. see the example below:
Select * from Table where TO_NUMBER(ID DEFAULT -999999999999 ON CONVERSION ERROR ) = 123
i want to get all tables names for the current schema ( i have many schemas)
so i use the user_tables which a system view that contain table_name column . but when im trying to query it inside java i get this this exception
java.sql.SQLSyntaxErrorException: ORA-00903: invalid table name
StringBuilder sb = new StringBuilder("SELECT * from FROM SYS.USER_TABLES ");
Query query = em.createNativeQuery(sb.toString());
List list = query.getResultList();
You have 'from' twice:
"SELECT * from FROM SYS.USER_TABLES "
This is being parsed as the from being the keyword, the FROM being the (invalid) table name, and the SYS.USER_TABLES being a table alias (also invalid, but has already failed by then).
It should be:
"SELECT * FROM USER_TABLES"
You don't need the SYS. prefix as mentioned in a comment. And if you only want the table names you should only select that column, not all columns via *.
I'm trying to use the same query to sort my table columns but when I pass as a parameter the column to ORDER BY, it adds quotes before and after my column name. If you are using ORDER BY parameter, the column name have to be written without being between quotes or MySQL is going to ignore it.
Example or query to execute:
select * from app_user ORDER BY mobile_token ASC LIMIT 0 , 20
This is what hibernate send to MySQL:
select * from app_user ORDER BY 'mobile_token' ASC LIMIT 0 , 20
Java query:
query = JPA.em().createNativeQuery("select * from app_user ORDER BY :column ASC LIMIT :init , :page",AppUser.class);
query.setParameter("column", column);
query.setParameter("init", pageNumber*pageSize);
query.setParameter("page", pageSize);
I could change the NativeQuery by:
"select * from app_user ORDER BY "+column+" ASC LIMIT :init , :page"
but this is going to become my app unsafety.
You can only pass values as parameters to a query. Not column or field names. That would make it impossible for the database to know which columns are actually used in the query, and thus make it impossible to prepare the execution plan.
So your solution using concatenation is the only one. Just make sure the column doesn't come from the user. Or if it comes from the user, that it's a valid column name and that the user is allowed to use it.
I'm trying the following query in cql.
SELECT * FROM LoadTest_Storage WHERE ItemName='NDTV_CRICKET145667657568'
AND key='NDTV_CRICKET1' LIMIT 1000
The key is existing but that itemname is not there in my table. But still it is returning the data of NDTV_CRICKET1 key.
But instead of key if i give some other column name for eg.
SELECT * FROM LoadTest_Storage WHERE ItemName='NDTV_CRICKET145667657568'
AND type='NDTV' LIMIT 1000
And such a type exists in my table but the itemname is not there then i don't get any results.
Why is it happening for key column alone?
Is it typical behavior of cassandra?
I am trying to implement an SQL code in order to scan a table of a database for a specific string of text that will alter with each query search.
For example, in the first search variable1 = 'cat'
The query Search for 'cat' will be performed.
If variable1 exists in the table proceed to the next variable value (changes dynamically in my Java stand alone app)
Now lets say that the variable1 = 'dog' The query result is negative (this string is NOT in the DB)
In this case make some processing and store this value in the table.
...and so on with all the values I need to search in the table!
The SQL code will be implemented in eclipse with the rest of my java code.
Any help appreciated.
Try this (Oracle pl/sql)
insert into pets_table (variable1_col) values ('cat')
where not exists (select * from pets_table where upper(variable1_col)='CAT')
insert into pets_table (variable1_col) values ('dog')
where not exists (select * from pets_table where upper(variable1_col)='DOG')