Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In sql how i can find previous year dates
I have previous year weekday,month,week,year, I have calculated these figures from the
current from and to date, The Question is how to find the previous year similar same day,year and month


Example

2011-01-01 -> 2012-01-01

suggest me how to achive this previous year dates
Posted
Comments
Bala Selvanayagam 1-Nov-11 16:06pm    
I am not very clear about your requirement, i know you have pur some example for the first solution but, can you define what you want to do with examples likw what are the input values and what do you expect as an output please ?
Rajesh Duraisamy 1-Nov-11 16:15pm    
if feb2010 month 1st sunday is what date?
feb2011 1st sunday is what date? its something like this
Bala Selvanayagam 2-Nov-11 8:56am    
I have answered your question and please check

Use the DATEADD[^] function - it works with a negative or positive value.
 
Share this answer
 
Comments
Rajesh Duraisamy 1-Nov-11 15:50pm    
Problem by doing this is 20110102 is fall on sunday but select DATEadd(YYYY,-1,'20110102') is fall on Saturday. i dont want the same date of previous year, I need to find the 1st or 2nd or 3rd or 4th sundays of month sholud match with current month 1st or 2nd or 3rd

Example
if feb2010 month 1st sunday is what date?
feb2011 1st sunday is what date? its something like this



member60 2-Nov-11 2:03am    
agree with OriginalGriff
P.Salini 2-Nov-11 5:54am    
I go with this
I just created a function which does what you need

alter FUNCTION fnBala (
	@Year varchar(20),
	@Month varchar(20),
	@Day2Find varchar(20),
	@nth int
)
RETURNS date
AS
BEGIN
	
	-- select dbo.fnBala ('2012','JAN','MON',1)
	Declare @StartDate date =convert(date,'1/'+@Month+'/'+@Year)
	Declare @FirstDateOfMonth date
	
	Declare @Factor int=-1

	
	if @Day2Find='SUN' 		set @Factor=6
	else if @Day2Find='SAT'		set @Factor=5
	else if @Day2Find='MON'		set @Factor=0
	else if @Day2Find='TUE'		set @Factor=1
	else if @Day2Find='WED'		set @Factor=2
	else if @Day2Find='THU'		set @Factor=3
	else if @Day2Find='FRI'		set @Factor=4
	
		
	if  ( @Factor - DATEDIFF(DD,0,@StartDate)%7) <0
		set @FirstDateOfMonth=dateadd("d",( @Factor - DATEDIFF(DD,0,@StartDate)%7)+7,@StartDate)	
	else		
		set @FirstDateOfMonth=dateadd("d",( @Factor - DATEDIFF(DD,0,@StartDate)%7),@StartDate)
	
	return dateadd("d",7*(@nth-1),@FirstDateOfMonth)
	
END



Fist time when you are creating the function
alter FUNCTION fnBala
needs to be repalced by
create FUNCTION fnBala 



1 If you want to find if feb2010 month 1st sunday is what date?

select dbo.fnBala ('2010','FEB','SUN',1)



2. If you want to find feb2011 3rd sunday is what date?

select dbo.fnBala ('2011','FEB','SUN',3)



I just tested this on SQL server 2008 and it works for me and not suree what is your SQL server version and please let me know if you get any error message along with the SQL server version


Hope this helps
 
Share this answer
 
Comments
Rajesh Duraisamy 2-Nov-11 13:34pm    
Thanks bala,,

its working
Bala Selvanayagam 2-Nov-11 13:40pm    
Nice to hear :)
As I understood your question you want for example to know in Feb 2010 what is the date of first Sunday.
So here is one solution :

SQL
declare @y int
set @y = 2010  --in which year we are interested ?
declare @m int
set @m = 2 --February 
declare @d int
set @d = 1  -- start from first day of month
declare @dp int
set @dp = 0  --this is an invalid day of the week ! untill we reach to desired one

--finding first sunday in Feb 2010
set @dp = datepart(dw,	CAST(CAST(@y AS varchar) + '-' + CAST(@m AS varchar) + '-' + CAST(@d AS varchar) AS DATETIME) )
while @dp!=1 --search for sunday
begin
	set @d = @d+1	
	set @dp = datepart(dw,	CAST(CAST(@y AS varchar) + '-' + CAST(@m AS varchar) + '-' + CAST(@d AS varchar) AS DATETIME) )
end
print CAST(CAST(@y AS varchar) + '-' + CAST(@m AS varchar) + '-' + CAST(@d AS varchar) AS DATETIME)


It need some customizations to meet your needs but I'm sure you can handle that.

Please mark it as an answer if it solved your problem or vote it up :)
 
Share this answer
 
use below script to get 1 year back exact date of getdate()

SQL
Declare @NoOfYearBefore/AfterDate
Set @NoOfYearBefore/AfterDate=-1


Select DateAdd(year, @NoOfYearBefore/AfterDate, getdate())
 
Share this answer
 
v2
Comments
fjdiewornncalwe 2-Nov-11 9:51am    
Same answer as Griff's. Please don't post duplicates.

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