Filtering out based Start Date and End Date

  • koti.raavi

    SSCrazy

    Points: 2694

    Hello,

    I have query contains both start and end date, i would like to filter out data based on two dates. I need only 2019 and higher data either based on start or end date, if you can have a look examples. i need ID 1,2,3,6,7 and 4,5 is  not required. I have tried using Year function on both dates, but like to know any other easy solution for this.

    CREATE TABLE #TEMP

    (

    ID INT,

    SDate DATE,

    EDate DATE

    )

    INSERT INTO #TEMP

    SELECT 1,'01/01/2014', '01/01/2019'

    UNION ALL

    SELECT 2,'01/01/2015', '01/01/2020'

    UNION ALL

    SELECT 3,'01/01/2019', '12/31/2019'

    UNION ALL

    SELECT 4,'01/01/2012', '12/31/2018'

    UNION ALL

    SELECT 5,'01/01/2010', '10/01/2016'

    UNION ALL

    SELECT 6,'06/01/2020', '10/01/2020'

    UNION ALL

    SELECT 7,'01/01/2021', '03/01/2021'

    • This topic was modified 1 month, 1 week ago by  koti.raavi.
    • This topic was modified 1 month, 1 week ago by  koti.raavi. Reason: Spelling
  • John Mitchell-245523

    SSC Guru

    Points: 148761

    I imagine you want your WHERE clause to be something like this:

    WHERE SDate > '20190101'
    AND EDate > '20190101'
  • Phil Parkin

    SSC Guru

    Points: 244584

    Start date is not relevant ... at least based on the sample data provided.

    SELECT *
    FROM #TEMP t
    WHERE t.EDate >= '20190101';

    If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.
    See https://www.sqlservercentral.com/articles/forum-etiquette-how-to-post-datacode-on-a-forum-to-get-the-best-help/ for details of how to post T-SQL code-related questions.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply