Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2011
    Posts
    7

    Question Unanswered: Dynamicaly generated where clause

    Hi,

    I would like to select records from a table which YEAR value equals current year or current year + 1 and I don't want to hard code year values.

    SELECT....
    FROM...
    WHERE YEAR in (current year, current year +1)

    I figured out how to get a current year:
    SELECT YEAR (current timestamp) FROM sysibm.sysdummy1

    but I'm having trouble incorporating that into where clause.
    Could you please show an example how to do that?

    Thanks,

  2. #2
    Join Date
    Mar 2003
    Posts
    280
    Quote Originally Posted by mt11 View Post
    Hi,

    I would like to select records from a table which YEAR value equals current year or current year + 1 and I don't want to hard code year values.

    SELECT....
    FROM...
    WHERE YEAR in (current year, current year +1)

    I figured out how to get a current year:
    SELECT YEAR (current timestamp) FROM sysibm.sysdummy1

    but I'm having trouble incorporating that into where clause.
    Could you please show an example how to do that?

    Thanks,
    Code:
     where year in (year(current_date), year(current_date + 1 year))
    between is perhaps a "better" predicate than in for this.
    Last edited by lelle12; 03-01-13 at 08:51.
    --
    Lennart

  3. #3
    Join Date
    Dec 2011
    Posts
    7
    Quote Originally Posted by lelle12 View Post
    Code:
     where year in (year(current_date), year(current_date + 1 year))
    between is perhaps a "better" predicate than in for this.
    Awesome!!
    Since my year values are char type I just modified it a little bit and it works perfectly:
    year in (char(year(current_date)), char(year(current_date + 1 year)))

    Thank you!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •