Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HQL function to extract rows with field contains only number

I've have a little issue to expose:

I want to extract from an entity, suppose its name "CustomerDetail" all rows where a specific field (code) has only numeric characters.

In HQL doesn't exist ISNUMERIC() function like Sql Server, and the possibility to apply regex function.

A possible solution is the following:

SELECT C
FROM CustomerDetail C
WHERE C.code NOT LIKE '%A%'
AND C.code NOT LIKE '%B%'

and so on repeating this condition overall alfabetical letter and special characters.

I think this is a poor solution, with a low level of performance (enormous number of LIKE)

Please, can you advice me a more smart solution?

Thank you in advance

P.S. My application is multi DBMS so I can't use SQL query

like image 662
Joe Taras Avatar asked Nov 12 '22 20:11

Joe Taras


1 Answers

Major edit: My bad, I mistook the just working function isNumeric() for a HQL function which is wrong. According to the doc HQL supports database scalar functions - and SQLServer (on which I tested) has an isNumeric() function.

I see two options now:

Option 1: You can write different HQLs for different databases which utilizes the build in functions like isNumeric() on SQLServer (but that's a huge step back in terms of "write once run everywhere")

Option 2: Write for every database you want to be compliant with a custom function in SQL and register it with the same name in your dialect.

I know, both options aren't terrific but I can't think of any other way to get it working.

like image 106
Marius K. Avatar answered Nov 15 '22 05:11

Marius K.