1select EXTRACT(
2 EPOCH FROM now() - (now() - INTERVAL '5 HOUR')
3)/3600
4
5-- it will return 5
1select age('2010-04-01', '2012-03-05'),
2 date_part('year',age('2010-04-01', '2012-03-05')),
3 date_part('month',age('2010-04-01', '2012-03-05')),
4 date_part('day',age('2010-04-01', '2012-03-05'));
1 CREATE OR REPLACE FUNCTION DateDiff (units VARCHAR(30), start_t TIME, end_t TIME)
2 RETURNS INT AS $$
3 DECLARE
4 diff_interval INTERVAL;
5 diff INT = 0;
6 BEGIN
7 -- Minus operator for TIME returns interval 'HH:MI:SS'
8 diff_interval = end_t - start_t;
9
10 diff = DATE_PART('hour', diff_interval);
11
12 IF units IN ('hh', 'hour') THEN
13 RETURN diff;
14 END IF;
15
16 diff = diff * 60 + DATE_PART('minute', diff_interval);
17
18 IF units IN ('mi', 'n', 'minute') THEN
19 RETURN diff;
20 END IF;
21
22 diff = diff * 60 + DATE_PART('second', diff_interval);
23
24 RETURN diff;
25 END;
26 $$ LANGUAGE plpgsql;