Show post between today's date and 6 months before

I am having problems with my SQL statement SELECT

. Basically I want to return all records where the transaction date is between today's date and today's date minus 6 months.

What I've tried so far:

SELECT * FROM loan_ledger
WHERE trandate <= DATEADD(month,-6,GETDATE())

      

but he doesn't get the exact result. The result was dates like 2009 or 2007 being returned, but what I want is if the date today is April 9, 2012, the results should be transactions from that date (April 9, 2012) to April 9, 2012 minus 6 months ( October 9,2011). Only.

What the heck is that the results display transactions in the past and still display 2009, 2001 records which I don't want!

Can anyone help me?

+3


source to share


4 answers


Another answer is close, but if you are using BETWEEN you want to add it to the date if you want to enable today

select * from loan_ledger
where trandate BETWEEN dateadd(month,-6,getdate()) AND getdate()+1

      



When used between getdate, it will not be enabled today unless you reach 1.

+2


source


Just change the where clause

select * from loan_ledger
where trandate >= dateadd(month,-6,getdate()) AND trandate <= getdate()

      



Sincerely.

+1


source


all your statements that you are talking about are "grab information when the date is up to 6 months ago" instead of "grab material from 6 months to today"

try something like

select * from loan_ledger
where trandate BETWEEN dateadd(month,-6,getdate()) AND getdate()

      

+1


source


You are asking for lines where trandate is <= 6 months ago. Of course, every trandat in 2007 meets these criteria.

If you only want trandates between today and 6 months ago, try

trandate between dateadd(month, -6, getdate()) and getdate()  

      

0


source







All Articles