Click here to Skip to main content
15,900,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
I want to know the below date format in sql server
how to find month from current year to previous year which means total month count should 12 months.

Expected output:

input : 2016-04-26
output: 2015-03-26 // this is for output

please drop me a solution for this.

thanks in advance.

What I have tried:

how to find month from current year to previous year which means total month count should 12 months.
Posted
Updated 26-Apr-16 0:25am

SQL
declare @date date
set @date = getdate()
select  DATEADD(MONTH,-12,@date)
 
Share this answer
 
Comments
stellus 26-Apr-16 6:21am    
THANKS KARTHIK,
I tried this but its coming count 13 months but i need 12 months from current month,
but your code showing like 2015-04-26
Karthik_Mahalingam 26-Apr-16 6:25am    
then try adding -13
F-ES Sitecore 26-Apr-16 6:29am    
declare @date date
set @date = getdate()
select DATEADD(MONTH,-13,@date)
stellus 26-Apr-16 6:29am    
THANK YOU
Karthik_Mahalingam 26-Apr-16 6:47am    
welcome
Hello ,
Use DATEADD function to get required OP .
Link :DATEADD (Transact-SQL)

SQL
select  DATEADD(MONTH,-13,getdate()) 
will give
C#
dt
2015-03-26 15:55:43.560
 
Share this answer
 
v2

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