1

I have a table where numbers are represented as Strings.

Is there a way to make an SQL/JPQL between query function as though they are Integers?

Currently between 100 and 110 returns 1001, 1002, 1015 etc. It should return only the numbers between 100 and 110.

The column type cannot be changed.

2
  • Why are you storing numbers as strings? Commented Mar 18, 2014 at 6:59
  • Not me, I'm just taking a look at it. It is a part of a pretty old product and nobody's sure why it was done. Commented Mar 18, 2014 at 7:06

1 Answer 1

1

You could try casting the value column as a numeric datatype before doing the comparison, like so:

SELECT *
FROM YOURTABLE
WHERE CAST(VALUE AS INT) BETWEEN 100 AND 110

This will ensure that comparison is done on numeric values, not text.

If you are passing parameters of VARCHAR data type as parameters, you will also need to explicitly cast them like so:

SELECT *
FROM YOURTABLE
WHERE CAST(VALUE AS INT) BETWEEN (CAST @Lower AS INT) AND (CAST @Upper AS INT)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.