×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

How to create a function which will retrieve year, based on a certain logic

How to create a function which will retrieve year, based on a certain logic

How to create a function which will retrieve year, based on a certain logic

(OP)
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

RE: How to create a function which will retrieve year, based on a certain logic

For example you can try something like this:

CODE

create or replace function getReferenceYear()
  returns decimal (4, 0)
  returns null on null input
begin  
  declare ref_year decimal (4, 0);

  set ref_year = year(current_date);
  if(month(current_date) <= 6) then
    set ref_year = ref_year - 1;
  end if;
  return ref_year;
end 

then this works

CODE

select current_date, getreferenceyear() from sysibm.sysdummy1 
and gives this output

CODE

....+....1....+....2....+....3 
 CURRENT DATE  GETREFERENCEYEAR 
  2019-04-12         2018      
 ********  End of data  ******** 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close