Disclaimer: I am new to PostgreSQL, and even newer to functions in plpgSQL. I've read the documentation but I am stumped.
Anyway, I have this function, to pick a charge band out of a table for asterisk CDR rating:
CREATE FUNCTION getBand(
callDateTime varchar
) RETURNS varchar AS $$
/* Select correct charging band */
DECLARE
callTime varchar;
callDay varchar;
theBand varchar;
BEGIN
/* Find the time and the day of the call */
callTime := to_char(to_timestamp(callDateTime,'YYYY-MM-DD HH24:MI:SS'),'HH24:MI:SS');
callDay := to_char(to_timestamp(callDateTime,'YYYY-MM-DD HH24:MI:SS'),'Day');
theBand := band
FROM bands
WHERE day = callDay
AND start < callTime
AND finish > callTime
LIMIT 1;
RETURN theBand;
END;
$$ LANGUAGE plpgsql
The input string is lifted from the asterisk CDR table, and is in the format 'YYYY-MM-DD HH24:MI:SS'.
The bands table is in the format
day | band | start | finish
============================
Where 'day' is the name of a weekday, band is the charge band for a call ("Peak", "Off Peak", "Weekend") and start and finish are the start and finish times for that charge band.
When I run
SELECT getBand('2013-05-03 11:30:00');
I get a NULL result.
When I run
SELECT band FROM bands
WHERE day = 'Friday' AND start < '11:30:00' and finish > '11:30:00' LIMIT 1;
I get the correct answer (for my data): 'Peak'
I'm confused. Anyone want to tell me where I'm going wrong?