-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Cast Hexadecimal string to Integer
PostPosted: Thu Nov 26, 2009 10:31 am 
Newbie

Joined: Thu Nov 26, 2009 10:08 am
Posts: 4
Hello,

I am trying to convert a hexadecimal string to a number for a where clause in HQL. I tried:

"from Table tb where cast(tb.hexstringcolumn as int) between 100 and 1000" .......

This works if tb.hexstringcolumn is a numeric string (e.g. 10). It will not work if tb.hexstringcolumn = FF. Any thoughts?

First post, so please be gentle (I read the before you post and searched the forum)


Top
 Profile  
 
 Post subject: Re: Cast Hexadecimal string to Integer
PostPosted: Mon Nov 30, 2009 10:05 am 
Pro
Pro

Joined: Tue Aug 26, 2003 8:07 pm
Posts: 229
Location: Brisbane, Australia
Easy way: convert your hex string into a standard Java int or long before binding it into your query.

Hard way: write a custom UserType object to bind it in to the queries yourself.

_________________
Cheers,
Shorn.


Top
 Profile  
 
 Post subject: Re: Cast Hexadecimal string to Integer
PostPosted: Mon Nov 30, 2009 10:18 am 
Newbie

Joined: Thu Nov 26, 2009 10:08 am
Posts: 4
I don't want to bind it at all. I am using the where clause to include a range of hex strings. Once this is done, it doesn't matter to me whether the hex string is a string or int. I found that I can use hex() to convert the string to a numeric representation. This is not the best method, but seems to be working. What would be better is if 'cast (column as int)' would recognize integer and hexadecimal strings.


Top
 Profile  
 
 Post subject: Re: Cast Hexadecimal string to Integer
PostPosted: Mon Nov 30, 2009 10:22 am 
Pro
Pro

Joined: Tue Aug 26, 2003 8:07 pm
Posts: 229
Location: Brisbane, Australia
What your are doing is what I meant by "easy way". cast() is a database operation, you'll have to talk to your DB vendor about that (although you might find if you dig through your DB vendor docs that it might do what you want if you prefix "0x" or somesuch).

_________________
Cheers,
Shorn.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.