Click here to Skip to main content
15,921,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please can someone help me out, I need to display records that falls in between two dates inclusive using the month. Assuming data below is my data;

SN NAME FIRSTPAY_DATE RECOVERY_DATE

1. DENIS 30/06/2010 22/02/2011
2. KAY 10/03/2010 05/07/2010
3. JANE 06/01/2010 03/02/2010
4. LOLA 03/08/2010 10/10/2011

If I need to display record that falls in between FIRSTPAY_DATE AND RECOVERY_DATE using july 2010 the following record should be displayed;


SN NAME FIRSTPAY_DATE RECOVERY_DATE

1. DENIS 30/06/2010 22/02/2011
2 KAY 10/03/2010 05/07/2010


Please help
Posted
Comments
fjdiewornncalwe 8-Jul-11 7:42am    
Kay. In the future, please add commments using the "Add Comment" link at the bottom right corner of the solution you are replying to so that the threads stay intact. Cheers.

Select *
From tablename
Where year(@date) between year(firstpay_day) and
 year(recovery_date) and month(@date) between month(firstpay_day)
and month(recovery_date)


Give that a try
 
Share this answer
 
Comments
fjdiewornncalwe 8-Jul-11 7:39am    
This is the correct solution. I missed the "inclusive to the month" part on my first read-through.
fjdiewornncalwe 8-Jul-11 7:41am    
OPs answer moved to comment:
Thanks Simon_Whale

This works perfectly for me.
Try:
SELECT * FROM myTable where @date >= FIRSTPAY_DATE AND @date <= RECOVERY_DATE
 
Share this answer
 
Comments
fjdiewornncalwe 8-Jul-11 7:40am    
OPs answer moved to comment:
Thanks OriginalGriff

But i want something like this

SELECT * FROM myTable where (YEAR AND MONTH (@date)) between (YEAR AND MONTH(FIRSTPAY_DATE)) AND (YEAR AND MONTH(RECOVERY_DATE))
This should be what you are after - not tested the performance though

declare @date datetime

set @date = '10 july 2010'

SELECT * FROM myTable where CAST(CAST(DATEPART(year, @date) as varchar(50)) + '-' + CAST(DATEPART(month, @date) AS varchar) + '-1'  AS DATETIME) between FIRSTPAY_DATE AND RECOVERY_DATE


Edit:Fixed minor spelling error
 
Share this answer
 
v2
Comments
fjdiewornncalwe 8-Jul-11 7:40am    
OPs answer moved to comment:
Thank Reiss

But this is not returning any record
fjdiewornncalwe 8-Jul-11 7:41am    
Reiss comment to above:
Reiss - 34 mins ago
I suspect due to us having different date formats, which Simons solution handles and his would be more effecient anyway due to not having to cast anything.
Select *
From tablename
Where year(@date) between year(firstpay_day) and
year(recovery_date) and month(@date) between month(firstpay_day)
and month(recovery_date)

This suggested solution above only work for a month with the same year eg
If @date = '03/04/2011'
Fistpay_day = '01/04/2011'
Recovery_date = '29/12/2011'

BUT when @date = '03/04/2011'
Fistpay_day = '01/04/2011'
Recovery_date = '29/02/2012'

it will not work because the month in second year(29/02/2012) is less than starting year (01/04/2011)

ie o4 between 04 and 02????

Please help out
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900