By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,712 Members | 1,958 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,712 IT Pros & Developers. It's quick & easy.

Integer ranges in PostgreSQL

P: n/a
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
---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)

Nov 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 22 '05 #2

P: n/a

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);

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)

Nov 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.