Convert to Postgres function - postgresql

I am converting my SQL stored procedure to Postgres function but it is resulting error in one of the statement as
ERROR: syntax error at or near ","
LINE 7: CAST(time(0), '00:00' + (h.hour * interval '1Hour...
^
Below is my Postgres function and SQL Store procedure which i am converting.Please tell me where i am getting wrong.
CREATE OR REPLACE FUNCTION shiftwisedata_sp(INOut shift_id bigint,InOut userdate date,OUT shift_name character varying (50),OUT from_time character varying(50),OUT to_time character varying(50),OUT cal bigint)
RETURNS SETOF record AS
$BODY$
BEGIN
return query
SELECT userdate, s.shift_name,
CAST(time(0), '00:00' + (h.hour * interval '1Hour')) AS from_time,
CAST(time(0), '00:00' + ((h.hour + 1) * interval '1Hour')) AS to_time,
COALESCE(r.Readings, 0) AS readings
FROM shift_wise s
CROSS JOIN (VALUES(0), (1), (2), (3), (4), (5), (6), (7), (8), (9),
(10), (11), (12), (13), (14), (15), (16), (17), (18), (19),
(20), (21), (22), (23)) AS h(hour)
OUTER APPLY (SELECT SUM(r.param_value) AS Readings
FROM table_1 r
WHERE r.timestamp_col >= CAST(userdate as timestamp without time zone) + (h.hour * interval '1Hour')
AND r.timestamp_col < CAST(userdate as timestamp without time zone) + ((h.hour + 1) * interval '1Hour')) AS r
WHERE s.shift_id = shift_id
AND (s.to_time > s.from_time AND
h.hour >= date_part(HOUR, s.from_time) AND
h.hour < date_part(HOUR, s.to_time)
OR
s.to_time < s.from_time AND
(h.hour >= date_part(HOUR, s.from_time) OR
h.hour < date_part(HOUR, s.to_time))
)
ORDER BY s.to_time;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
CREATE PROCEDURE Shiftdata #date date, #shiftid int AS
SELECT #date, s.Shift_Name,
convert(time(0), dateadd(HOUR, h.hour, '00:00')) AS from_time,
convert(time(0), dateadd(HOUR, h.hour + 1, '00:00')) AS to_time,
coalesce(r.Readings, 0) AS readings
FROM Shift_Wise s
CROSS JOIN (VALUES(0), (1), (2), (3), (4), (5), (6), (7), (8), (9),
(10), (11), (12), (13), (14), (15), (16), (17), (18), (19),
(20), (21), (22), (23)) AS h(hour)
OUTER APPLY (SELECT SUM(r.Reading_Col) AS Readings
FROM Reading r
WHERE r.Timestamp_Col >= dateadd(HOUR, h.hour, convert(datetime, #date))
AND r.Timestamp_Col < dateadd(HOUR, h.hour + 1, convert(datetime, #date))) AS r
WHERE s.Shift_ID = #shiftid
AND (s.to_time > s.from_time AND
h.hour >= datepart(HOUR, s.from_time) AND
h.hour < datepart(HOUR, s.to_time)
OR
s.to_time < s.from_time AND
(h.hour >= datepart(HOUR, s.from_time) OR
h.hour < datepart(HOUR, s.to_time))
)
ORDER BY s.to_time

That isn't how the CAST works in Postgresql. See http://www.postgresql.org/docs/9.2/static/sql-createcast.html
('00:00' + (h.hour * interval '1Hour'))::time
is easiest to read (IMHO) if you don't mind using the non-standard :: notation.

Finally,
Converted SQL SP into Postgres Function and got the desired result
CREATE OR REPLACE FUNCTION shiftwisedata_sp(IN shiftid bigint, INOUT userdate date, OUT shift_name character varying, OUT from_time time without time zone, OUT to_time time without time zone, OUT readings bigint)
RETURNS SETOF record AS
$BODY$
BEGIN
return query
SELECT userdate, s.shift_name,
('00:00' + (h.hour * interval '1Hour'):: time) AS from_time,
('00:00' + ((h.hour + 1) * interval '1Hour'):: time) AS to_time,
COALESCE(r.Readings, 0) AS readings
FROM shift_wise s
CROSS JOIN (VALUES(0), (1), (2), (3), (4), (5), (6), (7), (8), (9),
(10), (11), (12), (13), (14), (15), (16), (17), (18), (19),
(20), (21), (22), (23)) AS h(hour)
LEFT JOIN LATERAL (SELECT CAST(SUM(r.param_value) as bigint) AS Readings
FROM table_1 r
WHERE r.timestamp_col >= (CASt(userdate As timestamp without time zone) + h.hour * interval '1Hour')
AND r.timestamp_col < (CASt(userdate As timestamp without time zone) + (h.hour + 1) * interval '1Hour')
) AS r ON TRUE
WHERE s.shift_id = shiftid
AND (s.to_time > s.from_time AND
h.hour >= Extract(HOUR from CAST(s.from_time as time)) AND
h.hour < Extract(HOUR from CAST(s.to_time as time))
OR
s.to_time < s.from_time AND
(h.hour >= Extract(HOUR from CAST(s.from_time as time)) OR
h.hour < Extract(HOUR from CAST(s.to_time as time))
))
ORDER BY s.to_time;
END;
$BODY$
LANGUAGE plpgsql VOLATILE

Related

Recursive queries with two aggregate columns

I want to compute certain statistics montly in a postgres database
WITH RECURSIVE totals(start, t_end, null_count, not_null_count) AS (
VALUES (date_trunc('month', current_date + interval '1 month'),
date_trunc('month', current_date + interval '2 months'),
0::bigint)
UNION
SELECT start - interval '1 month' as start, start as t_end,
(SELECT count(*) filter (WHERE flag IS NULL) FROM tbl
WHERE created_at >= start
and created_at < t_end
and deleted_at < current_timestamp
) as null_count,
(SELECT count(*) filter (WHERE flag IS NOT NULL) FROM tbl
WHERE created_at >= start
and created_at < t_end
and deleted_at < current_timestamp
) as not_null_count
FROM totals
WHERE start > current_date - interval '1 year'
)
select * from totals
This would give me what I want, but would scan tbl twice.
Is there a way to to this scanning only once, like one would do in a plain query
SELECT count(*) filter (WHERE flag IS NULL) null_count,
count(*) filter (WHERE flag IS NOT NULL) not_null_count, FROM tbl
WHERE created_at >= start
AND created_at < t_end
AND deleted_at < current_timestamp
I know I could group by date_trunk('month', created_at) but doing that causes the query to sort the rows, and this is very costly in this case.
Yes. You can update your query to -
WITH RECURSIVE totals(start, t_end, null_count, not_null_count) AS (
VALUES (date_trunc('month', current_date + interval '1 month'),
date_trunc('month', current_date + interval '2 months'),
0::bigint)
UNION
SELECT start - interval '1 month' as start, start as t_end,
count(*) filter (WHERE flag IS NULL) as null_count,
count(*) filter (WHERE flag IS NOT NULL) as not_null_count
FROM tbl
JOIN totals ON created_at >= start
and created_at < t_end
WHERE start > current_date - interval '1 year'
AND deleted_at < current_timestamp
)
select * from totals

PostgreSQL Time Dimension (By Hours and Days) Error

I am am building a Time Dimension table in PostgreSQL with DATE_ID and DATE_DESC.
My T-SQL (works perfectly) script is:
set DATEFIRST 1
;WITH DATES AS (
SELECT CAST('2019-01-01 00:00:00.000' AS datetime) AS [DATE]
UNION ALL
SELECT DATEADD(HH,1,[DATE])
FROM DATES
WHERE DATEADD(HH,1,[DATE]) <= CAST('2019-12-31' AS datetime)
)
SELECT
DATE_ID, DATE_DESC
from
(
SELECT
CONVERT(int, CONVERT(char(8), DATE, 112)) AS DATE_ID,
DATE AS DATE_DESC
FROM
DATES)a
order by 1
OPTION (MAXRECURSION 0)
At the moment Im trying to convert this code to PostgreSQL readable one and it does not work..
Here is mine at the moment:
set EXTRACT(DOW FROM TIMESTAMP '2019-01-01 00:00:00.000')+1
;WITH DATES AS (
SELECT CAST('2019-01-01 00:00:00.000' AS timestamp) AS DATE
UNION ALL
SELECT CURRENT_DATE + INTERVAL '1 hour'
FROM DATES
WHERE CURRENT_DATE + INTERVAL '1 hour' <= CAST('2019-12-31' AS timestamp)
)
SELECT DATE_ID, DATE_DESC from
(SELECT cast(to_char((DATE)::TIMESTAMP,'yyyymmddhhmiss') as BIGInt) AS DATE_ID,
DATE AS DATE_DESC
FROM
DATES)a
order by 1
OPTION (MAXRECURSION 0)
I need all the hours (24h) between 2019-01-01 and 2019-12-31 . At the moment I think OPTION (MAXRECURSION 0) and set EXTRACT(DOW FROM TIMESTAMP '2019-01-01 00:00:00.000')+1 is not working properly.
Its a problem of Recursive CTE, In Postgresql, your desired query will be like below
WITH recursive DATES AS (
SELECT CAST('2019-01-01 00:00:00.000' AS timestamp) AS date_
UNION ALL
SELECT date_ + INTERVAL '1 hour'
FROM DATES
WHERE date_ + INTERVAL '1 hour' <= CAST('2019-12-31' AS timestamp)
)
SELECT DATE_ID, DATE_DESC from
(SELECT cast(to_char((date_)::TIMESTAMP,'yyyymmddhhmiss') as BIGInt) AS DATE_ID,
date_ AS DATE_DESC
FROM
DATES)a
order by 1
DEMO

TSQL get unique (not overlapping) datetime ranges

This is a question like this: TSQL get overlapping periods from datetime ranges but with a different result request.
This is the table:
create table period (
id int,
starttime datetime,
endtime datetime,
type varchar(64)
);
insert into period values (1,'2013-04-07 8:00','2013-04-07 13:00','Work');
insert into period values (2,'2013-04-07 14:00','2013-04-07 17:00','Work');
insert into period values (3,'2013-04-08 8:00','2013-04-08 13:00','Work');
insert into period values (4,'2013-04-08 14:00','2013-04-08 17:00','Work');
insert into period values (5,'2013-04-07 10:00','2013-04-07 11:00','Holyday'); /* 1h overlapping with 1*/
insert into period values (6,'2013-04-08 10:00','2013-04-08 20:00','Transfer'); /* 6h overlapping with 3 and 4*/
insert into period values (7,'2013-04-08 11:00','2013-04-08 12:00','Test'); /* 1h overlapping with 3 and 6*/
I need the unique not overlapping datetime ranges table.
In the before example the result would be:
'2013-04-07 08:00','2013-04-07 13:00'
'2013-04-07 14:00','2013-04-07 17:00'
'2013-04-08 08:00','2013-04-08 20:00'
It is not very important if could be time fragmentation such as:
'2013-04-08 08:00','2013-04-08 13:00'
'2013-04-08 12:00','2013-04-08 20:00'
--EDIT--
Another example:
create table period (
id int,
starttime datetime,
endtime datetime,
type varchar(64)
);
insert into period values (1,'2013-06-13 8:30','2013-06-13 12:30','');
insert into period values (2,'2013-06-13 8:38','2013-06-13 12:38','');
insert into period values (3,'2013-06-13 13:18','2013-06-13 17:45','');
insert into period values (4,'2013-06-13 13:30','2013-06-13 17:30','');
insert into period values (5,'2013-06-13 20:00','2013-06-13 23:59','');
this should return:
2013-06-13 08:30 - 2013-06-13 12:38
2013-06-13 13:18 - 2013-06-13 17:45
2013-06-13 20:00 - 2013-06-13 23:59
But you have only one non-overlapping period, or did I understand the question wrong?
select *
from period t
where id in (
select t1.id
from period t1
join period t2 on t1.id <> t2.id
where t2.endtime <= t1.starttime or t2.starttime >= t1.endtime
group by t1.id
having count(*) + 1 = (select count(*) from period)
)
Result:
'2013-04-07 14:00','2013-04-07 17:00'
Update: Ok, so you want to merge overlapping ranges. Try this:
select starttime, endtime
from period
where id in (
select t1.id
from period t1
join period t2 on t1.id <> t2.id
where t2.endtime < t1.starttime or t2.starttime > t1.endtime
group by t1.id
having count(*) + 1 = (select count(*) from period)
)
union all
select min(start), max(fin) from (
select
case when t2.starttime < t1.starttime then t2.starttime else t1.starttime end as start,
case when t2.endtime < t1.endtime then t1.endtime else t2.endtime end as fin
from period t1
join period t2 on t1.id < t2.id
where t2.endtime >= t1.starttime and t2.starttime <= t1.endtime) overlaps
group by datepart(dd, start), datepart(dd, fin)
I found this solution... I think this is not the best way, but seems to work.
DECLARE #union_unique TABLE (id INT IDENTITY(1, 1) primary key ,starttime datetime,endtime datetime)
DECLARE #idset TABLE (id int)
DECLARE #i int
SET #i = 1
IF (SELECT COUNT(*) FROM period) > 0
WHILE (#i <= (SELECT MAX(id) FROM period))
BEGIN
delete from #idset
insert into #idset
select distinct t2.id
from period t1
join #union_unique t2 on convert(date, t1.starttime)=convert(date, t2.starttime)
where t1.id=#i and
(
t1.starttime >= t2.starttime and t1.starttime <= t2.endtime
or
t1.endtime >= t2.starttime and t1.endtime <= t2.endtime
or
t1.starttime <= t2.starttime and t1.endtime >= t2.endtime
)
if(select count(*) from #idset)=0
insert into #union_unique (starttime, endtime) select starttime, endtime from period where id=#i
else
BEGIN
insert into #union_unique (starttime, endtime)
select
min(starttime),
max(endtime)
from (
select starttime, endtime from #union_unique where id in (select id from #idset)
union
select starttime, endtime from period where id=#i
) alll
delete from #union_unique where id in (select id from #idset)
END
SET #i = #i + 1
END
select * from #union_unique order by starttime

Split datetime SQL Server 2008

I have a table with 3 columns StartDate, EndDate, ElapsedTimeInSec.
I use an AFTER INSERT trigger to calculate the ElapsedTimeInSec.
I would like to do this:
If my start date is 2011-11-18 07:30:00 and my end date 2011-11-18 9:30:00 which give me a ElapsedtimeInSec of 7200 I would like to be able to split it this way.
Row 1 : 2011-11-18 07:30:00 / 2011-11-18 08:00:00 / 1800
Row 2 : 2011-11-18 08:00:00 / 2011-11-18 09:00:00 / 3600
Row 3 : 2011-11-18 09:00:00 / 2011-11-18 09:30:00 / 1800
How can I achieve this result ?
I dont think I made my explaination clear enough.
I have an actual table with data in it which as 2 field one with a StratDowntime and one with a EndDowntime and I would like to create a view of hours per hour base on a production shift of 12 hours (07:00:00 to 19:00:00) of the downtime.
So If I have a downtime from 2011-11-19 06:00:00 to 2011-11-19 08:00:00 I want in my report to see from 07:00:00 so the new rocrd should look like 2011-11-19 07:00:00 to 2011-11-19 08:00:00.
Another example if I do have downtime from 2011-11-19 10:30:00 to 2011-11-19 13:33:00 I should get in my report this
- 2011-11-19 10:30:00 to 2011-11-19 11:00:00
- 2011-11-19 11:00:00 to 2011-11-19 12:00:00
- 2011-11-19 12:00:00 to 2011-11-19 13:00:00
- 2011-11-19 13:00:00 to 2011-11-19 13:33:00
I hope this will clarify the question because none of the solution down there is actually doing this it is close but not on it.
thanks
You could try something like:
DECLARE #StartDate DATETIME = '11/18/2011 07:30:00',
#EndDate DATETIME = '11/18/2011 09:30:00',
#Runner DATETIME
IF DATEDIFF (mi, #StartDate, #EndDate) < 60
BEGIN
SELECT #StartDate,
#EndDate,
DATEDIFF (s, #StartDate, #EndDate)
RETURN
END
SET #Runner = CONVERT (VARCHAR (10), #StartDate, 101) + ' ' + CAST (DATEPART(hh, #StartDate) + 1 AS VARCHAR) + ':00:00'
WHILE #Runner <= #EndDate
BEGIN
SELECT #StartDate,
#Runner,
DATEDIFF (s, #StartDate, #Runner)
SET #StartDate = #Runner
SET #Runner = DATEADD(hh, 1, #Runner)
END
SET #Runner = CONVERT (VARCHAR (10), #EndDate, 101) + ' ' + CAST (DATEPART(hh, #EndDate) AS VARCHAR) + ':00:00'
SELECT #Runner,
#EndDate,
DATEDIFF (s, #Runner, #EndDate)
CTE:
DECLARE #beginDate DATETIME,
#endDate DATETIME
SELECT #beginDate = '2011-11-18 07:30:00',
#endDate = '2011-11-18 09:33:10'
DECLARE #mytable TABLE
(
StartDowntime DATETIME,
EndDowntime DATETIME,
ElapsedDowntimesec INT
)
-- Recursive CTE
;WITH Hours
(
BeginTime,
EndTime,
Seconds
)
AS
(
-- Base case
SELECT #beginDate,
DATEADD(MINUTE, ( DATEPART(MINUTE, #beginDate) * -1 ) + 60, #beginDate),
DATEDIFF
(
SECOND,
#beginDate,
CASE
WHEN #endDate < DATEADD(MINUTE, ( DATEPART(MINUTE, #beginDate) * -1 ) + 60, #beginDate) THEN #endDate
ELSE DATEADD(MINUTE, ( DATEPART(MINUTE, #beginDate) * -1 ) + 60, #beginDate)
END
)
UNION ALL
-- Recursive
SELECT Hours.EndTime,
CASE
WHEN #endDate < DATEADD(MINUTE, ( DATEPART(MINUTE, Hours.BeginTime) * -1 ) + 120, Hours.BeginTime) THEN #endDate
ELSE DATEADD(minute, ( DATEPART(MINUTE, Hours.BeginTime) * -1 ) + 120, Hours.BeginTime)
END,
DATEDIFF
(
SECOND,
Hours.EndTime,
CASE
WHEN #endDate < DATEADD(MINUTE, ( DATEPART(MINUTE, Hours.BeginTime) * -1 ) + 120, Hours.BeginTime) THEN #endDate
ELSE DATEADD(MINUTE, ( DATEPART(MINUTE, Hours.BeginTime) * -1 ) + 120, Hours.BeginTime)
END
)
FROM Hours
WHERE Hours.BeginTime < #endDate
)
INSERT INTO #myTable
SELECT *
FROM Hours
WHERE BeginTime < #endDate
SELECT * FROM #myTable
Results
BeginTime EndTime Seconds
2011-11-18 07:30:00.000 2011-11-18 08:00:00.000 1800
2011-11-18 08:00:00.000 2011-11-18 09:00:00.000 3600
2011-11-18 09:00:00.000 2011-11-18 09:33:10.000 1990
You can use a table valued function applied like SELECT * FROM [dbo].split('2011-11-02 12:55:00','2011-11-02 13:05:00')
Function defintion:
CREATE FUNCTION [dbo].[split] (#d1 DATETIME, #d2 DATETIME)
RETURNS #result TABLE (
StartDate DATETIME,
EndDate DATETIME,
ElapsedTimeSeconds INT
)
AS
BEGIN
-- Store intermediate values in #tmp, using ix as driver for start times.
DECLARE #tmp TABLE (ix INT NOT NULL IDENTITY(0,1) PRIMARY KEY
, d1 DATETIME, d2 DATETIME)
-- Insert first hole hour lower than start time
INSERT INTO #tmp (d1) SELECT DATEADD(HOUR, DATEDIFF(HOUR, -1, #d1), -1)
-- Calculate expected number of intervals
DECLARE #intervals INT = DATEDIFF(HOUR, #d1, #d2) - 1
-- insert all intervals
WHILE #intervals > 0
BEGIN
INSERT INTO #tmp (d1, d2) select top 1 d1, d2 FROM #tmp
SET #intervals = #intervals - 1
END
-- Set start and end time for all whole hour intervals
UPDATE #tmp SET d1 = DATEADD(hour, ix, d1)
, d2 = DATEADD(hour, ix + 1, d1)
-- Set correct start time for first interval
UPDATE #tmp SET d1 = #d1 WHERE d1 <= #d1
-- Insert end interval
INSERT INTO #tmp (d1, d2)
SELECT MAX(d2), #d2 FROM #tmp
-- Delete non-empty last interval
DELETE FROM #tmp WHERE d1 = d2
-- Insert #tmp to #result
INSERT INTO #result (StartDate, EndDate)
SELECT d1, d2 FROM #tmp
-- Set interval lengths
UPDATE #result SET ElapsedTimeSeconds = DATEDIFF(second, StartDate, EndDate)
return
END
GO
To get a result from an existing table, you can use CROSS APPLY. Assuming a table YourTable with StartTime and EndTime you can do something like
SELECT s.*, y.* FROM YourTable y
cross apply dbo.split(y.StartTime, y.EndTime) s
WHERE y.EndTime < '2011-09-11'
to get a result with a kind of join between input data and output table.

How to get the count of current month Sunday's in psql?

How to get total number of Sunday's for given date in postgresql
You need EXTRACT:
SELECT
EXTRACT(DOW FROM DATE '2011-02-16') = 0; -- 0 is Sunday
This can result in true or false, it's a sunday or it's not. I have no idea what you mean by "total number" because that will always be 0 (the date is not a sunday) or 1 (the given data is a sunday).
Edit: Something like this?
SELECT
COUNT(*)
FROM
generate_series(timestamp '2011-01-01', '2011-03-01', '1 day') AS g(mydate)
WHERE
EXTRACT(DOW FROM mydate) = 0;
The total number of Sundays for a given date can only be either 0 or 1.
But if you want the number of Sundays within a given date range, then your best bet is a calendar table. To find how many Sundays are in February this year, I'd just
select count(*)
from calendar
where cal_date between '2011-02-01' and '2011-02-28' and
day_of_week = 'Sun';
or
select count(*)
from calendar
where year_of_date = 2011 and
month_of_year = 2 and
day_of_week = 'Sun';
Here's a basic calendar table that you can start with. I also included a PostgreSQL function to populate the calendar table. I haven't tested this in 8.3, but I'm pretty sure I'm not using any features that 8.3 doesn't support.
Note that the "dow" parts assume your days are in English. But you can easily edit those parts to match any language. (I think. But I could be wrong about "easily".)
-- Table: calendar
-- DROP TABLE calendar;
CREATE TABLE calendar
(
cal_date date NOT NULL,
year_of_date integer NOT NULL,
month_of_year integer NOT NULL,
day_of_month integer NOT NULL,
day_of_week character(3) NOT NULL,
CONSTRAINT calendar_pkey PRIMARY KEY (cal_date),
CONSTRAINT calendar_check CHECK (year_of_date::double precision = date_part('year'::text, cal_date)),
CONSTRAINT calendar_check1 CHECK (month_of_year::double precision = date_part('month'::text, cal_date)),
CONSTRAINT calendar_check2 CHECK (day_of_month::double precision = date_part('day'::text, cal_date)),
CONSTRAINT calendar_check3 CHECK (day_of_week::text =
CASE
WHEN date_part('dow'::text, cal_date) = 0::double precision THEN 'Sun'::text
WHEN date_part('dow'::text, cal_date) = 1::double precision THEN 'Mon'::text
WHEN date_part('dow'::text, cal_date) = 2::double precision THEN 'Tue'::text
WHEN date_part('dow'::text, cal_date) = 3::double precision THEN 'Wed'::text
WHEN date_part('dow'::text, cal_date) = 4::double precision THEN 'Thu'::text
WHEN date_part('dow'::text, cal_date) = 5::double precision THEN 'Fri'::text
WHEN date_part('dow'::text, cal_date) = 6::double precision THEN 'Sat'::text
ELSE NULL::text
END)
)
WITH (
OIDS=FALSE
);
ALTER TABLE calendar OWNER TO postgres;
-- Index: calendar_day_of_month
-- DROP INDEX calendar_day_of_month;
CREATE INDEX calendar_day_of_month
ON calendar
USING btree
(day_of_month);
-- Index: calendar_day_of_week
-- DROP INDEX calendar_day_of_week;
CREATE INDEX calendar_day_of_week
ON calendar
USING btree
(day_of_week);
-- Index: calendar_month_of_year
-- DROP INDEX calendar_month_of_year;
CREATE INDEX calendar_month_of_year
ON calendar
USING btree
(month_of_year);
-- Index: calendar_year_of_date
-- DROP INDEX calendar_year_of_date;
CREATE INDEX calendar_year_of_date
ON calendar
USING btree
(year_of_date);
And a rudimentary function to populate the table. I haven't tested this in 8.3 either.
-- Function: insert_range_into_calendar(date, date)
-- DROP FUNCTION insert_range_into_calendar(date, date);
CREATE OR REPLACE FUNCTION insert_range_into_calendar(from_date date, to_date date)
RETURNS void AS
$BODY$
DECLARE
this_date date := from_date;
BEGIN
while (this_date <= to_date) LOOP
INSERT INTO calendar (cal_date, year_of_date, month_of_year, day_of_month, day_of_week)
VALUES (this_date, extract(year from this_date), extract(month from this_date), extract(day from this_date),
case when extract(dow from this_date) = 0 then 'Sun'
when extract(dow from this_date) = 1 then 'Mon'
when extract(dow from this_date) = 2 then 'Tue'
when extract(dow from this_date) = 3 then 'Wed'
when extract(dow from this_date) = 4 then 'Thu'
when extract(dow from this_date) = 5 then 'Fri'
when extract(dow from this_date) = 6 then 'Sat'
end);
this_date = this_date + interval '1 day';
end loop;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;