| Author | Topic | 
                            
                                    | jeffbondStarting Member
 
 
                                        16 Posts | 
                                            
                                            |  Posted - 2008-12-01 : 10:52:51 
 |  
                                            | Hi,How do I retrieve the date of the first day of a given week given a week number and a year number, with week starting on Sundays?ex: week 47, year 2008 first date is 11/16/2208last date is 11/22/2008input(47,2008)output '11/16/2008' 0r '2008-11-16' |  | 
       
                            
                       
                          
                            
                                    | spirit1Cybernetic Yak Master
 
 
                                    11752 Posts |  | 
                            
                       
                          
                            
                                    | jeffbondStarting Member
 
 
                                    16 Posts | 
                                        
                                          |  Posted - 2008-12-01 : 11:18:25 
 |  
                                          | Is there anything I could use inline using DatePart, DateDiff, and Convert ? |  
                                          |  |  | 
                            
                       
                          
                            
                                    | visakh16Very Important crosS Applying yaK Herder
 
 
                                    52326 Posts | 
                                        
                                          |  Posted - 2008-12-01 : 11:25:54 
 |  
                                          | quote:DECLARE @Year varchar(4),@weeknumber int,@yeardate datetimeSET @yeardate=dateadd(yy,datediff(yy,0,@year),0)SET @yearday=DATEPART(dw,@yeardate)Originally posted by jeffbond
 Hi,How do I retrieve the date of the first day of a given week given a week number and a year number, with week starting on Sundays?ex: week 47, year 2008 first date is 11/16/2208last date is 11/22/2008input(47,2008)output '11/16/2008' 0r '2008-11-16'
 
 select dateadd(dd,-1* (@yearday-1),dateadd(wk,@week-1,@yeardate)) |  
                                          |  |  | 
                            
                       
                          
                            
                                    | SwePesoPatron Saint of Lost Yaks
 
 
                                    30421 Posts | 
                                        
                                          |  Posted - 2008-12-01 : 11:33:36 
 |  
                                          | How do you define a week? E 12°55'05.63"N 56°04'39.26"
 |  
                                          |  |  | 
                            
                       
                          
                            
                                    | jeffbondStarting Member
 
 
                                    16 Posts | 
                                        
                                          |  Posted - 2008-12-01 : 11:49:34 
 |  
                                          | first day of the first week of a year is the first Sunday before Jan  1st of that Yearsunday is first day and saturday last dayI tried your code Visak, it didn't work for me. |  
                                          |  |  | 
                            
                       
                          
                            
                                    | jeffbondStarting Member
 
 
                                    16 Posts | 
                                        
                                          |  Posted - 2008-12-01 : 12:02:23 
 |  
                                          | OK, I stopped being lazy and coded it :  --Set Sunday as the first Day of the weekset datefirst 7--Inputdeclare @year intset @year = 2008declare @N intset @N = 47-- Declare the datesdeclare @FirstDayOfYear datetimedeclare @FirstDayOfFirstWeek datetimedeclare @FirstDayOfWeekN datetime-- set the datesset @FirstDayOfYear =  CONVERT(datetime,'01/01/'+CONVERT(char(4),@Year)) set @FirstDayOfFirstWeek =    dateadd("d",-datepart("dw",@FirstDayOfYear)+1,@FirstDayOfYear) set @FirstDayOfWeekN = dateadd("ww",@N-1,@FirstDayOfFirstWeek)--show offselect @FirstDayOfWeekN |  
                                          |  |  | 
                            
                       
                          
                            
                                    | Michael Valentine JonesYak DBA Kernel (pronounced Colonel)
 
 
                                    7020 Posts | 
                                        
                                          |  Posted - 2008-12-01 : 12:28:20 
 |  
                                          | This does what you want no matter what the setting of datefirst is, so it is more suitable for a function: declare @year	intdeclare @N	intset @year	= 2008set @N		= 47select FirstDayOfWeekN =  	dateadd(dd,(((datediff(dd,-53684,dateadd(yy,@year-1900,0)+7)/7)*7)-7)+((@N-1)*7),-53684)Results:FirstDayOfWeekN------------------------ 2008-11-16 00:00:00.000(1 row(s) affected)CODO ERGO SUM |  
                                          |  |  | 
                            
                       
                          
                            
                                    | jeffbondStarting Member
 
 
                                    16 Posts | 
                                        
                                          |  Posted - 2008-12-01 : 13:08:50 
 |  
                                          | Thanks.Can I ask you why your solution is better?It doesn't seem to speed up my queries. |  
                                          |  |  | 
                            
                       
                          
                            
                                    | Michael Valentine JonesYak DBA Kernel (pronounced Colonel)
 
 
                                    7020 Posts | 
                                        
                                          |  Posted - 2008-12-01 : 14:05:20 
 |  
                                          | quote:It is hard to tell about speed without careful testing.  This activity tends to be a small part of the overall query, so if you want to compare them, you have to prepare a test that isolates the two methods enough to detect the difference.  However, it is my experience that date manipulations involving casting to a datetime from a string the way you are doing is slower that using the nested DATEADD/DATEDIFF in my solution. Also, I just modified my original code to make it slightly shorter and eliminate one call to the DATEADD function, so this should improve the performance slightly.As for why it is better, I stated before that it eliminates the dependency on setting of datefirst, making it more suitable for use inside a function.CODO ERGO SUMOriginally posted by jeffbond
 Thanks.Can I ask you why your solution is better?It doesn't seem to speed up my queries.
 
 |  
                                          |  |  | 
                            
                       
                          
                            
                                    | LampreyMaster Smack Fu Yak Hacker
 
 
                                    4614 Posts | 
                                        
                                          |  Posted - 2008-12-02 : 15:40:23 
 |  
                                          | I was just messing around and ripped off part of Michael's code and came up with something that seems to work: declare @year intset @year = 2008declare @N intset @N = 47SELECT DATEADD(DAY, (@N - 1) * 7, DATEADD(DAY,(((DATEDIFF(DAY, 0, DATEADD(YEAR, @Year - 1900, 0)) + 7) / 7) * 7)- 7, 0)) - 1Michael, what is the purpose of the -53684? |  
                                          |  |  | 
                            
                       
                          
                            
                                    | hanbinglAged Yak Warrior
 
 
                                    652 Posts | 
                                        
                                          |  Posted - 2008-12-02 : 17:06:14 
 |  
                                          | Almost right, 1st day of 1st week in 2008 is 2008-1-1 not 2007-12-30 |  
                                          |  |  | 
                            
                       
                          
                            
                                    | hanbinglAged Yak Warrior
 
 
                                    652 Posts | 
                                        
                                          |  Posted - 2008-12-02 : 17:14:14 
 |  
                                          | [code]CREATE FUNCTION DBO.FN_GETFIRSTDAYOFWEEK(@year int, @week int)returns datetimebegindeclare @approxdate datetimeset @approxdate = dateadd(week,@week-1,dateadd(year, @year-1900,0))if @week > 1beginreturn dateadd(dd,1-datepart(dw,@approxdate ),@approxdate )endreturn @approxdate end[/code] |  
                                          |  |  | 
                            
                            
                                |  |