I need to apply a java regex on sql query string to calculate the count of it. I have to get what is between the "first select" and "from" of the principal query.
This is my example :
Query :
select name,(select age from subtable), adress from table where name in (select name from subtable1)
Result :
select count(*) from table where name in (select name from subtable1)
I was using replaceFirst("^(.*?)from", "select count(*) from") but it is not working because there is an sql query in the attribute.
Please anyone can help ?
You can solve your problem using this regex ^(.*?)from(?![^(]*\\)):
str = str.replaceFirst("^(.*?)from(?![^(]*\\))", "select count(*) from");
Output
select count(*) from table where name in (select name from subtable1)
The idea is match from that is inside () parenthesis.
Demo
Related
Postgres query:
select t
from TableName t
where t.isActive=true
order by t.extcol->'desc'
This work's fine.
Need help with HQL query which will do the same job.
String query = "select t from TableName t where t.isActive=true order by t.extcol->'desc'"
newList = TableNameModel.executeQuery(query);
Method threw 'java.lang.IllegalArgumentException' exception.
context: there is a table TableName where one column is extcol which contains object. Need to sort TableName based on a property desc which present in extcol json object.
For HQL you have the wrong syntax for sorting.
It should be order by t.extcol desc without the arrow and quotes.
see https://docs.jboss.org/hibernate/core/3.3/reference/en/html/queryhql.html#queryhql-ordering
Is there any way to convert the SQL query with REGEXP to HQL query fro this example.
SELECT * FROM product m WHERE id = 19 and m.name REGEXP ('DEf|abc');
SELECT * FROM product m WHERE id = 19 and m.name REGEXP ('DEf|abc');
where name is column having json values
I expect the output will display the results but i am getting an error
UNEXPECTED TOKEN REGEXP.
HQL does not have regular expressions.
but for this you could use a like or
SELECT * FROM product m
WHERE id = 19
and ( m.name like '%Def%' or m.name like '%abc%' );
I want to run the query: "DESCRIBE table_name;"
statement = this.connection.createStatement();
ResultSet rset = statement.executeQuery("DESCRIBE table_name");
and I got this error:
" java.sql.SQLSyntaxErrorException: ORA-00900: invalid SQL statement"
what is the problem?
DESC[RIBE] is a SQL*Plus command, not a SQL statement. The DESC command queries the Oracle data dictionary, something like:
select COLUMN_NAME, DATA_TYPE
from USER_TAB_COLUMNS
where TABLE_NAME = 'YOUR_TABLE'
DESC is a SQL*Plus command. SO, you cannot use it via JDBC/ODBC.
An alternative can be like this below.
select RPAD(COLUMN_NAME,30)||' '||DATA_TYPE||'('||DATA_LENGTH||')' as descr
FROM all_tab_cols
WHERE TABLE_NAME = UPPER('YOUR_TABLE') and owner=UPPER('SCHEMA_NAME');
all_tab_cols is a data dictionary table(view) which contains the table metadata
Oracle's Reference
describe user2.flights;
Here user2 is database name and flights is table name. Try this.
Or use next query
select *
from user_tab_columns
where table_name = 'MY_TABLE'
order by column_id;
Use this query.
column_id is the "order" of the column in the table.
You should ensure that 'MY_TABLE' is capitalised unless you've been adding tables with casing ( a bad idea ) in which case you need to use something like = "MyTable"
I have a query that worked for me when I was using Mysql.
Now I get this error on Oracle :
ORA-00979: not a GROUP BY expression
This is the query:
SELECT o.neNesId, COUNT(o)
FROM ParNe AS o
WHERE o.neBanId = :neBanId
GROUP BY o.neNesId
Any ideas why I have this error?
Your query is:
SELECT o.neNesId, COUNT(o)
FROM ParNe AS o
WHERE o.neBanId = :neBanId
GROUP BY o.neNesId
My guess is that o.o is not a valid field. So, you have a table name where a column name is expected.
Try this instead:
SELECT o.neNesId, COUNT(*)
FROM ParNe AS o
WHERE o.neBanId = :neBanId
GROUP BY o.neNesId
Or replace the * with a valid column name.
use count(*) . count(o) is used in hibernate not in sql query
You need to select count(*) or count(1) as in Gordon's example. If you insist on using your query syntax, which is not necessary at all then try this:
SELECT neNesId, count(o) AS o_cnt
FROM
( -- your query here --
SELECT 1 neNesId, 2 AS o FROM dual
)
GROUP BY neNesId
/
String sqlStm = "SELECT * FROM TableName WHERE TableName MATCH '?'";
pst.setString(1, keyword.getText());
I am doing a full-text search on my database. When I use the above it doesnt execute the sql statement. However, if I were to change the program and give a value to match like this...
String sqlStm = "SELECT * FROM TableName WHERE TableName MATCH 'technology'";
The programme execute. But i want the search to be based on entered text in keyword JTextfield. is the formatting correct in the first line. does MATCH support pst.setString(). What should be the correct statement.
remove the single quote around the parameter, it will convert the parameter into a value. Parameter placeolder ? shouldn't be enclosed with single quotes.
String sqlStm = "SELECT * FROM TableName WHERE TableName MATCH ?";
pst.setString(1, keyword.getText());
How about this?
SELECT * FROM TableName WHERE TableName LIKE '%^technology$%'
or
SELECT * FROM TableName WHERE TableName REGEXP '^\\technology$'
I couldn't really find any article that says SQlite Like or regexp behaves differently than MySQL so... you may give that a try. If there is anything that differs please share as well.