Special Case : How to query from SQL Server where next month is next year?

Hi, I have a problem with SQL Query.

So I have to output data from SQL query, but there is a special case.

Normal Case :

select contract_number, end_date
from table_A
where (MONTH(END_DATE)=‘10’ or MONTH(END_DATE)=‘11’ or MONTH(END_DATE)=‘12’) AND (YEAR(END_DATE)>=‘2022’)
order by end_date

using query above, I will output data from October 2022 until December 2022…

Special Case :
select contract_number, end_date
from table_A
where (MONTH(END_DATE)=‘11’ or MONTH(END_DATE)=‘12’ or MONTH(END_DATE)=‘1’) AND (YEAR(END_DATE)>=‘2022’)
order by end_date

  1. Based on special case, I have to output data from November 2022 until January 2023… Any idea how to do this? I tried to split the month and year as shown below like this and still not working

(MONTH(END_DATE)=‘1’ AND (YEAR(END_DATE)>=‘2023’))

  1. My assumption after the query is done, I have to filter it again in uipath (the data from January 2022 probably will be queried too). How to remove the whole month unused date (example 1 January 2022 - 31 January 2022) using filter data table?

any help on the query for this special case? Thanks !!

Are you trying to select a quarter?
You need set only first day of month for any date

select contract_number, end_date
from table_A
where '2022-10-01' <= END_DATE AND END_DATE < DATEADD(m,3,'2022-10-01')
order by END_DATE
1 Like

And why you do not use BETWEEN operator?

Cheers

1 Like

Your query are more simplify than mine, but the result are the same. Thanks !!

Hi,

I tried to use between and it’s not working due to different year. But it’s all solved now, thanks for responding !

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.