Thread: Boolean SQL function ?

Boolean SQL function ?

From
Bo Lorentsen
Date:
Hi ...

This time I'm really lost :-)

How does one write a SQL FUNCTION that works in a WHERE clause ?

If fx. I like to make a function called "is_today" that take two dates
and test if they are within the same day, how can I express this in SQL
?

I cant make a "SELECT 1 WHERE EXTRACT( day ...", so how does one return
a boolean ?

Hopefull regards ..

/BL


Re: Boolean SQL function ?

From
Tom Lane
Date:
Bo Lorentsen <bl@netgroup.dk> writes:
> How does one write a SQL FUNCTION that works in a WHERE clause ?
> If fx. I like to make a function called "is_today" that take two dates
> and test if they are within the same day, how can I express this in SQL
> ?

Something like this:

test71=# create function same_day(timestamp, timestamp) returns bool as
test71-# 'select $1::date = $2::date' language 'sql';
CREATE
test71=# select same_day(now(), 'today'::timestamp);
 same_day
----------
 t
(1 row)

test71=# select same_day(now(), 'yesterday'::timestamp);
 same_day
----------
 f
(1 row)

test71=#

This particular example doesn't need to use any boolean constants,
but when you do, you can write them like so:
    true
    false
    't'::bool
    'f'::bool

            regards, tom lane