Subject RE: [firebird-support] Re: select birthdays form the next several days
Author Simon Carter
How about an sp, something like:

SET TERM ## ;

CREATE OR ALTER PROCEDURE BIRTHDAYS (RANGE SMALLINT, BASEDATE TIMESTAMP)
RETURNS
(
ID BIGINT,
USERNAME VARCHAR(80),
BIRTHDATE TIMESTAMP
)
AS
DECLARE VARIABLE vDays INTEGER;
DECLARE VARIABLE vYear INTEGER;
DECLARE VARIABLE vMonth INTEGER;
DECLARE VARIABLE vDay INTEGER;
BEGIN
-- if range not specified, default to 10 days
IF (RANGE IS NULL) THEN
RANGE = 10;

-- if date not specified, default to today
IF (BASEDATE IS NULL) THEN
BASEDATE = CURRENT_DATE;

EXECUTE PROCEDURE DECODEDATE(BASEDATE) RETURNING_VALUES :vYear, :vMonth,
:vDay;

FOR SELECT USERID, NAME, BDAY
FROM USERS
INTO :ID, :USERNAME, :BIRTHDATE
DO
BEGIN
EXECUTE PROCEDURE ENCODEDATE(EXTRACT(YEAR FROM BIRTHDATE), vMonth, vDay)
RETURNING_VALUES :BASEDATE;
vDays = BirthDate - BaseDate;

IF (:vDays BETWEEN 0 AND RANGE) THEN
SUSPEND;
END
END ##

SET TERM ; ##

FYI , encodedate and decodedate can be downloaded from www.fbtalk.net,
however, if you want to rewrite them encode date takes a Y, M and D and
returns a TIMESTAMP, decode date takes a Timestamp and returns a Y, M and D
values.

I have tested for leap year and it seems to work ok, the parameters are:

Range, number of days your looking for and base date, the date which your
comparing to, both of these can be null as defaults are used.

To call you can use something like:

-- 10 days over a leap year
SELECT *
FROM BIRTHDAYS(null, '2004.02.25')

-- 10 days from today
SELECT *
FROM BIRTHDAYS(null, null)

Hih

Si Carter
http://www.fbtalk.net/
http://www.tectsoft.net/