Re: Boolean SQL function ? - Mailing list pgsql-novice

From Tom Lane
Subject Re: Boolean SQL function ?
Date
Msg-id 3583.998490669@sss.pgh.pa.us
Whole thread Raw
In response to Boolean SQL function ?  (Bo Lorentsen <bl@netgroup.dk>)
List pgsql-novice
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

pgsql-novice by date:

Previous
From: Bo Lorentsen
Date:
Subject: Boolean SQL function ?
Next
From: Hanbiao Wang
Date:
Subject: installation problem