chinnusaccount
chinnusaccount

Reputation: 229

String split function with hql

I have following hql query,

from Channe where ip='1.11.6.0';

But in the db the IP is saving as 1.11.6.0:8080 .

So I need to modify the query in a way that, split the ip with a delimiter ':' and take the firstcome value. I do not wish to modify the search with value 1.11.6.0:8080.

Upvotes: 0

Views: 7082

Answers (3)

carbontax
carbontax

Reputation: 2184

See this page in the Hibernate docs. On the page below there is a section called 14.10. Expressions

http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html

It says, among other things:

string concatenation ...||... or concat(...,...) current_date(),

...

Any function or operator defined by EJB-QL 3.0: substring(), trim(), lower(), upper(), length(), locate(), abs(), sqrt(), bit_length(), mod()

But you are actually better off doing as @Hansraj suggests in the comments and appending a wildcard to your search term

String query = "from Channe where ip like :term";

entityManager.createQuery(query).setParameter("term",ipString + "%");

This assumes that your data type is string, of course.

Upvotes: 2

Henry
Henry

Reputation: 1008

Try this:

SPLIT(".", FIELDNAME)

Upvotes: 0

Ashok
Ashok

Reputation: 93

Try the following:

Say variable ip had the address

ip = "10.131.56.40:8080";
var ipSplit = ip.Split(':');
var ipStart = ipSplit[0];

ipStart will store only 10.131.56.40

This could solve your problem

Upvotes: 0

Related Questions