tags:

views:

216

answers:

5

I need to check that result of expression in where clause is in range of Integers.

something lke this: select * from table where (col1 / col2 ) in (1..8). (1..8) - means range of integers.

I mean that it must be integer, not float. So that I cant use "between 1 and 8", because 1.2 will be correct.

Are there any thought?

A: 

You could cast it from float to int and use between. (You might want to make a virutal/computed column depending on the query's performance.)

Nelson
If you cast it from float to integer, it will always pass an integer test...
Martin Bøgelund
Ah, good point! I need my coffee. =)
Nelson
A: 

To test col1/col2 is an integer, you could Mod them together...

Where (col1/col2) = (col1\col2) and (col1/col2) between 1 and 8

Bill
Or just as easily, test the modulocol1%col2 = 0
Bill
\ does not seem to be an Oracle operator
Patrick McDonald
@patrick you are right. Oracle wants you to use Floor(x,y) and Mod(x,y). Mine is the useless M$SQL answer.
Bill
+1  A: 

How about

select * 
from table
where (col1 / col2 ) BETWEEN 1 AND 8
  and (col1 / col2 ) = FLOOR(col1 / col2 )

This simply checks if the fraction is in the interval, and integer.

Martin Bøgelund
This does not work on oracle...
FerranB
Which part of it?
Martin Bøgelund
The 1..8 isn't valid SQL syntax for a range.
Gary
That was copied from the question - my contribution is correct. Downvote the question instead.I've updated the where clause to use Oracles BETWEEN function.
Martin Bøgelund
+3  A: 

You can of course do this:

select * from table where (col1 / col2 ) in (1,2,3,4,5,6,7,8);

or

select * from table where (col1 / col2 ) between 1 and 8
and mod (col1 , col2 ) = 0;
Tony Andrews
As far as I see 2nd variant is the best option. Of course in (1,..,n) is the most intuitive, but less handy in case of wide range.
zmische
A: 

You can use pipeline function to generate integer range.

create or replace type my_number_collection is table of number;

create or replace function
  gen_range (p_from in number, p_to in number)
  return my_number_collection
  PIPELINED
  as
  begin
    for i in p_from..p_to loop
      pipe row(i);
    end loop;
    return;
  end;
/

select *
from my_table 
where col1/col2 in (select column_value from table(gen_range(1,8)));
jva