Skip to content

Instantly share code, notes, and snippets.

@danielleevandenbosch
Created August 20, 2018 19:47
Show Gist options
  • Save danielleevandenbosch/1fbe392fd9e2d91952a1b029351e4fcb to your computer and use it in GitHub Desktop.
Save danielleevandenbosch/1fbe392fd9e2d91952a1b029351e4fcb to your computer and use it in GitHub Desktop.
WARNING! slight syntax difference. See how to use. SQL server has dateadd and so should postgres. Run the following DDL and have date add in your custom user defined functions.
CREATE OR REPLACE FUNCTION dateadd(_timelabel VARCHAR(25),_timevalue NUMERIC,_timegiven TIMESTAMP)
RETURNS TIMESTAMP AS
$BODY$
DECLARE returnVal TIMESTAMP;
/*
-- =============================================
-- Author : Daniel Van Den Bosch
-- Create date: 8/20/2018
-- Description: Use dateadd (a ms sql server function) in postgres
-- How To use : Your are going to have to add quotes to the _timelabel AND it is a good idea to cast :: TO TIMESTAMP _timegiven::TIMESTAMP
Take the following example: MS TSQL : SELECT DATEADD(year, 1, current_timestamp) AS DateAdd;
-- POSTGRES : SELECT DATEADD('year', 1, current_timestamp::TIMESTAMP) AS DateAdd;
-- =============================================
___________________________________________
|datepart | Abbreviations | supported |
-------------------------------------------|
|year | yy, yyyy | x |
-------------------------------------------|
|quarter | qq, q | x |
-------------------------------------------|
|month | mm, m | x |
-------------------------------------------|
|dayofyear | dy, y | no |
-------------------------------------------|
|day | dd, d | x |
-------------------------------------------|
|week | wk, ww | x |
-------------------------------------------|
|weekday | dw, w | no |
-------------------------------------------|
|hour | hh | x |
-------------------------------------------|
|minute | mi, n | x |
-------------------------------------------|
|second | ss, s | x |
-------------------------------------------|
|millisecond | ms | x |
-------------------------------------------|
|microsecond | mcs | no |
-------------------------------------------|
|nanosecond | ns | no |
-------------------------------------------|
*/
DECLARE
BEGIN
IF _timelabel='yy' OR _timelabel='yyyy' OR _timelabel = 'year' OR _timelabel='years' THEN
returnVal=_timegiven + (_timevalue::TEXT || ' years')::INTERVAL;
ELSIF _timelabel='qq' OR _timelabel='q' OR _timelabel = 'quarter' OR _timelabel='quarters' THEN
returnVal=_timegiven + ((_timevalue*3)::TEXT || ' months')::INTERVAL;
ELSIF _timelabel='mm' OR _timelabel='m' OR _timelabel = 'month' OR _timelabel='months' THEN
returnVal=_timegiven + ((_timevalue)::TEXT || ' months')::INTERVAL;
ELSIF _timelabel='dd' OR _timelabel='d' OR _timelabel = 'days' OR _timelabel='day' THEN
returnVal=_timegiven + ((_timevalue)::TEXT || ' days')::INTERVAL;
ELSIF _timelabel='wk' OR _timelabel='ww' OR _timelabel = 'week' OR _timelabel='weeks' THEN
returnVal=_timegiven + ((_timevalue)::TEXT || ' week')::INTERVAL;
ELSIF _timelabel='hh' OR _timelabel='h' OR _timelabel = 'hour' OR _timelabel='hours' THEN
returnVal=_timegiven + ((_timevalue)::TEXT || ' hours')::INTERVAL;
ELSIF _timelabel='mi' OR _timelabel='n' OR _timelabel = 'minute' OR _timelabel='minutes' THEN
returnVal=_timegiven + ((_timevalue)::TEXT || ' minutes')::INTERVAL;
ELSIF _timelabel='ss' OR _timelabel='s' OR _timelabel = 'second' OR _timelabel='seconds' THEN
returnVal=_timegiven + ((_timevalue)::TEXT || ' seconds')::INTERVAL;
ELSIF _timelabel='ms' OR _timelabel='ms' OR _timelabel = 'millisecond' OR _timelabel='milliseconds' THEN
returnVal=_timegiven + ((_timevalue)::TEXT || ' milliseconds')::INTERVAL;
ELSE
returnVal='1/1/1900'::TIMESTAMP;
END IF;
--raise exception 'dateadd interval parameter not supported';
return returnVal;
END;
$BODY$
LANGUAGE 'plpgsql';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment