#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2011
    Posts
    9
    Rep Power
    0

    Return records from table based on current_date


    Hello,

    Consider this table


    agent_id | mu_id | agent_data_id | c_sdate | c_edate | agent_data_value
    ----------+-------+---------------+------------+------------+------------------
    1 | 1 | 1 | 2011-11-20 | 2012-01-05 | Doc Martin
    1 | 1 | 1 | 2012-01-06 | 2012-09-02 | Neil Morrisey
    1 | 1 | 1 | 2012-09-03 | | David Evans
    1 | 1 | 5 | 2012-03-22 | | 1234567890


    This table contains a start and end date so I need a query that says return rows if the current date is between those dates, for example if the current date is the 2012-07-13 then it should return the row with value Neil Morrisey. Also it is important to note that this table could only contain one row with no end date so the query should return that if the above is not true.

    Hopefully that make some sense!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2003
    Location
    Paris Uppland
    Posts
    2,385
    Rep Power
    391
    Code:
    select <columnsOfInterest>
      from t
     where current_date between c_sdate and coalesce(c_edate,current_date)

    Comments on this post

    • gilescardew agrees : Works perfectly
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2011
    Posts
    9
    Rep Power
    0
    Originally Posted by swampBoogie
    Code:
    select <columnsOfInterest>
      from t
     where current_date between c_sdate and coalesce(c_edate,current_date)
    Cheers, works a treat

IMN logo majestic logo threadwatch logo seochat tools logo