Pregunta

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.

¿Fue útil?

Solución

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)
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top