Updating day portion od date field

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • breimer
    New Member
    • Feb 2008
    • 2

    #1

    Updating day portion od date field

    I have created a simple database with a single date field & inserted the current date. What I need to do is alter the day portion of this date field to be a value of 01 if the day of the current month is less than 21, otherwise the value should be 20. Thanks..bill

    Here's a sample of the code I am attempting to use:
    Code:
    code=DB2
    -- drop date table
    drop table REIMEB2.DATE_TABLE;
    -- create date table
    CREATE TABLE REIMEB2.DATE_TABLE (SELECTION_DATE DATE);
    INSERT INTO REIMEB2.DATE_TABLE (SELECTION_DATE)
    values (CURRENT DATE);
    update REIMEB2.DATE_TABLE set day(SELECTION_DATE) = 
    (SELECT  CASE DAY(CURRENT DATE)
        WHEN  < 21
            then  01
        else 
            then 21
    end case FROM REIMEB2.DATE_TABLE);
    SELECT * FROM REIMEB2.DATE_TABLE;
    
    end code
    Last edited by docdiesel; Feb 23 '08, 09:38 PM. Reason: Added code tags
  • docdiesel
    Recognized Expert Contributor
    • Aug 2007
    • 297

    #2
    Hi,

    after adding some code tags to your posting, here's a trick for you which should fit to your needs:

    Results in the first of the current month:
    Code:
    select
     current_date + 1 day
     - (  day(current_date) ) days
    from sysibm.sysdummy1 ;
    Results in 0 or 19, due to integer division :
    Code:
    select
     ( (day(current_date) / 20 ) * 19 ) days
    from sysibm.sysdummy1 ;
    Together they'll give you the first or 20th of the month:
    Code:
    select
              current_date            + 1 day
     - (  day(current_date)      )        days
    +  ( (day(current_date) / 20 ) * 19 ) days
    from sysibm.sysdummy1 ;
    Based on this, you could even add a GENERATED ALWAYS column to your table with this calculation, leading to this result automatically getting written into column DATE_CALC every time there's an INSERT or UPDATE on column DATE_ORIGINAL.

    Regards,

    Bernd

    Comment

    • breimer
      New Member
      • Feb 2008
      • 2

      #3
      Results are great! I'll play with the calculation to see if I can get either 1 or 21 as the final day values. Greatly appreciate your assistance..... bill

      Originally posted by docdiesel
      Hi,

      after adding some code tags to your posting, here's a trick for you which should fit to your needs:

      Results in the first of the current month:
      Code:
      select
       current_date + 1 day
       - (  day(current_date) ) days
      from sysibm.sysdummy1 ;
      Results in 0 or 19, due to integer division :
      Code:
      select
       ( (day(current_date) / 20 ) * 19 ) days
      from sysibm.sysdummy1 ;
      Together they'll give you the first or 20th of the month:
      Code:
      select
                current_date            + 1 day
       - (  day(current_date)      )        days
      +  ( (day(current_date) / 20 ) * 19 ) days
      from sysibm.sysdummy1 ;
      Based on this, you could even add a GENERATED ALWAYS column to your table with this calculation, leading to this result automatically getting written into column DATE_CALC every time there's an INSERT or UPDATE on column DATE_ORIGINAL.

      Regards,

      Bernd

      Comment

      Working...