Thread: Integer ranges in PostgreSQL

Integer ranges in PostgreSQL

From
Marco Lazzeri
Date:
Does exist a syntax in PostgreSQL for integer (INT) number ranges?

It could be useful for queries like
"SELECT * FROM table WHERE id IN (range from 1 to 20)"

Thank you!
Marco


Re: Integer ranges in PostgreSQL

From
Gregory Wood
Date:
Marco Lazzeri wrote:
> Does exist a syntax in PostgreSQL for integer (INT) number ranges?
>
> It could be useful for queries like
> "SELECT * FROM table WHERE id IN (range from 1 to 20)"

I believe you're looking for BETWEEN:
SELECT * FROM table WHERE id BETWEEN 1 AND 20;

That is standard SQL and not specific to PostgreSQL

Greg

Re: Integer ranges in PostgreSQL

From
Stephan Szabo
Date:
On Fri, 16 Jan 2004, Marco Lazzeri wrote:

> Does exist a syntax in PostgreSQL for integer (INT) number ranges?
>
> It could be useful for queries like
> "SELECT * FROM table WHERE id IN (range from 1 to 20)"

As already sent, BETWEEN will do this for you already for this kind
of use.  However, if you need it for a join or something, you could
also write a set returning function that returns you the set of numbers.

Maybe something like the not really tested:

create or replace function range(int, int, int) returns setof int as '
declare
 i int;
begin
 i := $1;
 while (i <= $2) loop
  return next i;
  i := i + $3;
 end loop;
 return;
end;' language 'plpgsql';
select * from range(1,20,1);