Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JPQL: "LIKE" comparison between numeric column and a String input parameter

843830Mar 31 2009 — edited Apr 1 2009
Good afternoon:

I'm still a rookie with EJB and JPQL and have a doubt about the use of the LIKE operator. On SQL, I used this operator to compare not only text strings but also a numeric value against a text pattern. I've checked and proved that I can do exactly the same in JPQL. Something like this:
SELECT s FROM Student s WHERE s.age LIKE '%5'
Being "s.age" a column of type Integer, I would get every student whose age ends in a 5. For example: 5, 15, 25, 35...

I need to do exactly the same but in a case in which the string to compare with is not specified literally but with an input parameter which is of type String:
SELECT s FROM Student s WHERE s.age LIKE :age
However, this returns a failure because it expects the named parameter "age" to be of the same type that the column, thus an Integer. Is there any way to avoid that? By now I've found a workaround which I didn't expect to work, sincerely... I wrapped the "a.age" column with the function CONCAT in order to force the left term to be a String. I didn't think that it could work, because the documentation states that the parameters of CONCAT must be 2 strings. However, this works:
SELECT s FROM Student s WHERE CONCAT (s.age, '') LIKE :age
Is there a "cleaner" way to achieve the same target? Thank you very much for your help.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 29 2009
Added on Mar 31 2009
5 comments
1,542 views