Expand or shrink the bounds by a value or an interval
expand(numspan,base) → numspan
expand(tstzspan,interval) → tstzspan
The function returns NULL if the value or interval given as second argument is negative and the span resulting from shifting the bounds with the argument is empty.
SELECT expand(floatspan '[1, 3]', 1); -- [0, 4] SELECT expand(floatspan '[1, 3]', -1); -- [2, 2] SELECT expand(floatspan '[1, 3)', -1); -- NULL SELECT expand(tstzspan '[2001-01-01, 2001-01-03]', interval '1 day'); -- [2000-12-31, 2001-01-04] SELECT expand(tstzspan '[2001-01-01, 2001-01-03]', interval '-1 day'); -- [2001-01-02, 2001-01-02] SELECT expand(tstzspan '[2001-01-01, 2001-01-03]', interval '-2 day'); -- NULL
Shift by a value or interval
shift(numbers,base) → numbers
shift(dates,integer) → dates
shift(times,interval) → times
SELECT shift(dateset '{2001-01-01, 2001-01-03, 2001-01-05}', 1); -- {2001-01-02, 2001-01-04, 2001-01-06} SELECT shift(intspan '[1, 4)', -1); -- [0, 3) SELECT shift(tstzspan '[2001-01-01, 2001-01-03]', interval '1 day'); -- [2001-01-02, 2001-01-04] SELECT shift(floatspanset '{[1, 2], [3, 4]}', -1); -- {[0, 1], [2, 3]} SELECT shift(tstzspanset '{[2001-01-01, 2001-01-03], [2001-01-04, 2001-01-05]}', interval '1 day'); -- {[2001-01-02, 2001-01-04], [2001-01-05, 2001-01-06]}
Scale by a value or interval
scale(numbers,base) → numbers
scale(dates,integer) → dates
scale(times,interval) → times
If the width or time span of the input value is zero (for example, for a singleton timestamp set), the result is the input value. The given value or interval must be strictly greater than zero.
SELECT scale(tstzset '{2001-01-01}', '1 day'); -- {2001-01-01} SELECT scale(tstzset '{2001-01-01, 2001-01-03, 2001-01-05}', '2 days'); -- {2001-01-01, 2001-01-02, 2001-01-03} SELECT scale(intspan '[1, 4)', 4); -- [1, 6) SELECT scale(datespan '[2001-01-01, 2001-01-04)', 4); -- [2001-01-01, 2001-01-06) SELECT scale(tstzspan '[2001-01-01, 2001-01-03]', '1 day'); -- [2001-01-01, 2001-01-02] SELECT scale(floatspanset '{[1, 2], [3, 4]}', 6); -- {[1, 3], [5, 7]} SELECT scale(tstzspanset '{[2001-01-01, 2001-01-03], [2001-01-04, 2001-01-05]}', '1 day'); /* {[2001-01-01 00:00:00, 2001-01-01 12:00:00], [2001-01-01 18:00:00, 2001-01-02 00:00:00]} */ SELECT scale(tstzset '{2001-01-01}', '-1 day'); -- ERROR: The duration must be a positive interval: -1 days
Shift and scale by the values or intervals
shiftScale(numbers,base,base) → numbers
shiftScale(dates,integer,integer) → dates
shiftScale(times,interval,interval) → times
This function combines the functions shift
and scale
.
SELECT shiftScale(tstzset '{2001-01-01}', '1 day', '1 day'); -- {2001-01-02} SELECT shiftScale(tstzset '{2001-01-01, 2001-01-03, 2001-01-05}', '1 day','2 days'); -- {2001-01-02, 2001-01-03, 2001-01-04} SELECT shiftScale(intspan '[1, 4)', -1, 4); -- [0, 5) SELECT shiftScale(datespan '[2001-01-01, 2001-01-04)', -1, 4); -- [2001-12-31, 2001-01-05) SELECT shiftScale(tstzspan '[2001-01-01, 2001-01-03]', '1 day', '1 day'); -- [2001-01-02, 2001-01-03] SELECT shiftScale(floatspanset '{[1, 2], [3, 4]}', -1, 6); -- {[0, 2], [4, 6]} SELECT shiftScale(tstzspanset '{[2001-01-01, 2001-01-03], [2001-01-04, 2001-01-05]}', '1 day', '1 day'); /* {[2001-01-02 00:00:00, 2001-01-02 12:00:00], [2001-01-02 18:00:00, 2001-01-03 00:00:00]} */
Round down or up to the nearest integer
floor({floatset,floatspans}) → {floatset,floatspans}
ceil({floatset,floatspans}) → {floatset,floatspans}
SELECT floor(floatset '{1.5,2.5}'); -- {1, 2} SELECT ceil(floatspan '[1.5,2.5)'); -- [2, 3) SELECT floor(floatspan '(1.5, 1.6)'); -- [1, 1] SELECT ceil(floatspanset '{[1.5, 2.5],[3.5,4.5]}'); -- {[2, 3], [4, 5]}
Round to a number of decimal places
round({floatset,floatspans},integer=0) → {floatset,floatspans}
SELECT round(floatset '{1.123456789,2.123456789}', 3); -- {1.123, 2.123} SELECT round(floatspan '[1.123456789,2.123456789)', 3); -- [1.123,2.123) SELECT round(floatspan '[1.123456789, inf)', 3); -- [1.123,Infinity) SELECT round(floatspanset '{[1.123456789, 2.123456789],[3.123456789,4.123456789]}', 3); -- {[1.123, 2.123], [3.123, 4.123]}
Convert to degrees or radians
degrees(floatset, normalize=false) → floatset
radians(floatset) → floatset
The additional parameter in the degrees
function can be used to normalize the values between 0 and 360 degrees.
SELECT round(degrees(floatset '{0, 0.5, 0.7, 1.0}', true), 3); -- {0, 28.648, 40.107, 57.296} SELECT round(radians(floatset '{0, 45, 90}'), 3); -- {0, 0.785, 1.571}
Transform to lowercase, uppercase, or initcap
lower(textset) → textset
upper(textset) → textset
initcap(textset) → textset
SELECT lower(textset '{"AAA", "BBB", "CCC"}'); -- {"aaa", "bbb", "ccc"} SELECT upper(textset '{"aaa", "bbb", "ccc"}'); -- {"AAA", "BBB", "CCC"} SELECT initcap(textset '{"aaa", "bbb", "ccc"}'); -- {"Aaa", "Bbb", "Ccc"}
Text concatenation
{text,textset} || {text,textset} → textset
SELECT textset '{aaa, bbb}' || text 'XX'; -- {"aaaXX", "bbbXX"} SELECT text 'XX' || textset '{aaa, bbb}'; -- {"XXaaa", "XXbbb"}
Set the temporal precision of the time value to the interval with respect to the origin
tprecision(times,interval,origin timestamptz=’2000-01-03’) → times
If the origin is not specified, it is set by default to Monday, January 3, 2000
SELECT tprecision(timestamptz '2001-12-03', '30 days'); -- 2001-11-23 SELECT tprecision(timestamptz '2001-12-03', '30 days', '2001-12-01'); -- 2001-12-01 SELECT tprecision(tstzset '{2001-01-01 08:00, 2001-01-01 08:10, 2001-01-01 09:00, 2001-01-01 09:10}', '1 hour'); -- {"2001-01-01 08:00:00+01", "2001-01-01 09:00:00+01"} SELECT tprecision(tstzspan '[2001-12-01 08:00, 2001-12-01 09:00]', '1 day'); -- [2001-12-01, 2001-12-02) SELECT tprecision(tstzspan '[2001-12-01 08:00, 2001-12-15 09:00]', '1 day'); -- [2001-12-01, 2001-12-16) SELECT tprecision(tstzspanset '{[2001-12-01 08:00, 2001-12-01 09:00], [2001-12-01 10:00, 2001-12-01 11:00]}', '1 day'); -- {[2001-12-01, 2001-12-02)} SELECT tprecision(tstzspanset '{[2001-12-01 08:00, 2001-12-01 09:00], [2001-12-01 10:00, 2001-12-01 11:00]}', '1 day'); -- {[2001-12-01, 2001-12-02)}