I need to create a function in DB2 which will retrieve year from the current date, based on a certain logic
If the number of the month of the current date is smaller and equal than 6 (=any month prior to June) then the previous year is the ‘reference year’
Else if the number of the month of the current date is larger than 6 (=any month after June!) then the current year is the ‘reference year’.
Examples:
The reference year for date ‘4/9/2019’ is 2018 , since 4 <= 6
The reference year for date ‘9/3/2019’ is 2019, since 9 > 6
Below is an example for the implementation for SQL Server:
CREATE FUNCTION dbo.getReferenceYear()
RETURNS int
AS
BEGIN
DECLARE @ret int;
SELECT @ret = MONTH(GETDATE())
IF (@ret <= 6)
SET @ret = (YEAR(GETDATE()) -1);
Else
SET @ret = (YEAR(GETDATE()) );
RETURN @ret;
END;
I need the same in db2.
Below is what I have tried
CREATE FUNCTION dbo.getReferenceYear()
RETURNS INT
BEGIN ATOMIC
DECLARE _month INT;
DECLARE _year INT;
SET _month = SELECT MONTH (current timestamp) FROM sysibm.sysdummy1
if(_month<=6)
SET _year = (SELECT YEAR (current timestamp) FROM sysibm.sysdummy1) -1
ELSE
SET _year = (SELECT YEAR (current timestamp) FROM sysibm.sysdummy1)
RETURN _year
END
If the number of the month of the current date is smaller and equal than 6 (=any month prior to June) then the previous year is the ‘reference year’
Else if the number of the month of the current date is larger than 6 (=any month after June!) then the current year is the ‘reference year’.
Examples:
The reference year for date ‘4/9/2019’ is 2018 , since 4 <= 6
The reference year for date ‘9/3/2019’ is 2019, since 9 > 6
Below is an example for the implementation for SQL Server:
CREATE FUNCTION dbo.getReferenceYear()
RETURNS int
AS
BEGIN
DECLARE @ret int;
SELECT @ret = MONTH(GETDATE())
IF (@ret <= 6)
SET @ret = (YEAR(GETDATE()) -1);
Else
SET @ret = (YEAR(GETDATE()) );
RETURN @ret;
END;
I need the same in db2.
Below is what I have tried
CREATE FUNCTION dbo.getReferenceYear()
RETURNS INT
BEGIN ATOMIC
DECLARE _month INT;
DECLARE _year INT;
SET _month = SELECT MONTH (current timestamp) FROM sysibm.sysdummy1
if(_month<=6)
SET _year = (SELECT YEAR (current timestamp) FROM sysibm.sysdummy1) -1
ELSE
SET _year = (SELECT YEAR (current timestamp) FROM sysibm.sysdummy1)
RETURN _year
END