Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all
i want to subtract current month from previous month on the last date of current month.
please give me ideas and solutions.
i am using
between DATEADD(month,-2,getdate()) and DATEADD(month,-1,getdate()

but it returns difference of previous two months and when i use between DATEADD(month,-1,getdate()) and DATEADD(month,-1,getdate())
it shows no data
so please help me.
Posted
Comments
ssd_coolguy 20-Feb-13 5:48am    
sorry but not understood your question.. :(
What i can understand is you need difference between current and last month then you can use..

between DATEADD(month,-1,getdate()) and getdate()

dear jackson please use

C#
between DATEADD(month,-1,getdate()) and getdate()
 
Share this answer
 
Comments
[no name] 20-Feb-13 6:26am    
azziet it does not working
Azziet 20-Feb-13 6:28am    
what is the error...please write complete query with some example records
[no name] 20-Feb-13 8:25am    
sir
when i use your code like
select DATEADD(month,-1,getdate()) - getdate()
it show
1899-12-01 but i want 2013-01-01
Azziet 21-Feb-13 0:53am    
why are you subtracting dates...i think you want some records between 1 months...
check this..

select DATEADD(month,-1,getdate())

output - 2013-01-21 11:22:43.813

select getdate()

output - 2013-02-21 11:22:43.813
[no name] 21-Feb-13 2:24am    
thnxx Azziet sir
Hi, Go through this...
Previous months last date

SQL
SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0))


Current months last date

SQL
SELECT DATEADD(S,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+1,0))


Difference for current month and previous month in month

SQL
Select DATEDIFF(M,DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0)),DATEADD(S,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+1,0)))

Or try this

SQL
Select MONTH(DATEADD(S,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+1,0)))- MONTH(DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0)))

Or use

SQL
Select datediff(Month,DATEADD(month,-1,getdate()) ,GETDATE() )
 
Share this answer
 
v3
Comments
[no name] 20-Feb-13 6:35am    
thanks a lot shobhana.n

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