From 222c201d78d1d284d5aef09f0e1aebb739fd2ab2 Mon Sep 17 00:00:00 2001 From: hengsin Date: Mon, 21 Nov 2022 20:56:25 +0800 Subject: [PATCH] IDEMPIERE-5481 PostgreSQL: trunc(timestamp,format) missing version for TIMESTAMP WITHOUT TIME ZONE (#1569) --- db/postgresql/functions/trunc.sql | 9 ++++++- .../i9/oracle/202211180700_IDEMPIERE-5481.sql | 6 +++++ .../202211180700_IDEMPIERE-5481.sql | 27 +++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 migration/i9/oracle/202211180700_IDEMPIERE-5481.sql create mode 100644 migration/i9/postgresql/202211180700_IDEMPIERE-5481.sql diff --git a/db/postgresql/functions/trunc.sql b/db/postgresql/functions/trunc.sql index 891e8d0319..ae5c689d2a 100644 --- a/db/postgresql/functions/trunc.sql +++ b/db/postgresql/functions/trunc.sql @@ -33,7 +33,7 @@ BEGIN END; $$ LANGUAGE plpgsql IMMUTABLE; -CREATE OR REPLACE FUNCTION trunc(datetime TIMESTAMP WITH TIME ZONE, format varchar) +CREATE OR REPLACE FUNCTION trunc(datetime TIMESTAMP WITHOUT TIME ZONE, format varchar) RETURNS DATE AS $$ BEGIN IF format = 'Q' THEN @@ -52,6 +52,13 @@ BEGIN END; $$ LANGUAGE plpgsql IMMUTABLE; +CREATE OR REPLACE FUNCTION trunc(datetime TIMESTAMP WITH TIME ZONE, format varchar) +RETURNS DATE AS $$ +BEGIN + RETURN trunc(cast(datetime as timestamp without time zone), format); +END; +$$ LANGUAGE plpgsql IMMUTABLE; + CREATE OR REPLACE FUNCTION trunc(i INTERVAL) RETURNS INTEGER AS $$ BEGIN diff --git a/migration/i9/oracle/202211180700_IDEMPIERE-5481.sql b/migration/i9/oracle/202211180700_IDEMPIERE-5481.sql new file mode 100644 index 0000000000..d5927aa657 --- /dev/null +++ b/migration/i9/oracle/202211180700_IDEMPIERE-5481.sql @@ -0,0 +1,6 @@ +SET SQLBLANKLINES ON +SET DEFINE OFF + +SELECT register_migration_script('202211180700_IDEMPIERE-5481.sql') FROM dual; + +-- PostgreSQL Only diff --git a/migration/i9/postgresql/202211180700_IDEMPIERE-5481.sql b/migration/i9/postgresql/202211180700_IDEMPIERE-5481.sql new file mode 100644 index 0000000000..153e8077c1 --- /dev/null +++ b/migration/i9/postgresql/202211180700_IDEMPIERE-5481.sql @@ -0,0 +1,27 @@ +SELECT register_migration_script('202211180700_IDEMPIERE-5481.sql') FROM dual; + +CREATE OR REPLACE FUNCTION trunc(datetime TIMESTAMP WITHOUT TIME ZONE, format varchar) +RETURNS DATE AS $$ +BEGIN + IF format = 'Q' THEN + RETURN CAST(DATE_Trunc('quarter',datetime) as DATE); + ELSIF format = 'Y' or format = 'YEAR' THEN + RETURN CAST(DATE_Trunc('year',datetime) as DATE); + ELSIF format = 'MM' or format = 'MONTH' THEN + RETURN CAST(DATE_Trunc('month',datetime) as DATE); + ELSIF format = 'DD' THEN + RETURN CAST(DATE_Trunc('day',datetime) as DATE); + ELSIF format = 'DY' THEN + RETURN CAST(DATE_Trunc('day',datetime) as DATE); + ELSE + RETURN CAST(datetime AS DATE); + END IF; +END; +$$ LANGUAGE plpgsql IMMUTABLE; + +CREATE OR REPLACE FUNCTION trunc(datetime TIMESTAMP WITH TIME ZONE, format varchar) +RETURNS DATE AS $$ +BEGIN + RETURN trunc(cast(datetime as timestamp without time zone), format); +END; +$$ LANGUAGE plpgsql IMMUTABLE;