locked
Select statement where a range of unique Years are selected RRS feed

  • Question

  • User-507786106 posted
    How do I create a Select statement where a range of unique rows are
    selected where year = 1989 between year = 2019?

    Goals:
    Select statements from 1 table
    Between the lowest year for CreateDate AND the current year 2019
    Thursday, August 22, 2019 9:10 PM

Answers

  • User-719153870 posted

    Hi slimbunny,

    Goals:
    Select statements from 1 table
    Between the lowest year for CreateDate AND the current year 2019

    Also confused about your needs, are you trying to secect data from your table where the date of these data is between the lowest year and current year?

    If so, please refer to below code:

    create table DateTable
    (
    id int identity(1,1),
    DDate date,
    Mark varchar(50)
    )
    
    insert into DateTable values('1989','aaa')
    insert into DateTable values('2009','bbb')
    insert into DateTable values('1996','ccc')
    insert into DateTable values('2000','ddd')
    insert into DateTable values('2020','eee')
    
    --select * from DateTable
    
    select * from DateTable where YEAR(DDate) between (select MIN(YEAR(DDate)) from DateTable) and (select YEAR(GETDATE()))

    This demo will get you:

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 23, 2019 3:03 AM

All replies

  • User-1716253493 posted

    Please give some sample of data

    Friday, August 23, 2019 1:10 AM
  • User-719153870 posted

    Hi slimbunny,

    Goals:
    Select statements from 1 table
    Between the lowest year for CreateDate AND the current year 2019

    Also confused about your needs, are you trying to secect data from your table where the date of these data is between the lowest year and current year?

    If so, please refer to below code:

    create table DateTable
    (
    id int identity(1,1),
    DDate date,
    Mark varchar(50)
    )
    
    insert into DateTable values('1989','aaa')
    insert into DateTable values('2009','bbb')
    insert into DateTable values('1996','ccc')
    insert into DateTable values('2000','ddd')
    insert into DateTable values('2020','eee')
    
    --select * from DateTable
    
    select * from DateTable where YEAR(DDate) between (select MIN(YEAR(DDate)) from DateTable) and (select YEAR(GETDATE()))

    This demo will get you:

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 23, 2019 3:03 AM