declare @T table (dt datetime) insert into @T values ('20...

0

Please login or register to vote for this query.

(click on this box to dismiss)

Stack Overflow

Q&A for programmers

declare @T table (dt datetime) 
--set datefirst 2
insert into @T values 
('2010-12-25'), 
('2010-12-26'), 
('2010-12-27'), 
('2010-12-28'), 
('2010-12-29'), 
('2010-12-30'), 
('2010-12-31'), 
('2011-01-01'), 
('2011-01-02'), 
('2011-01-03'), 
('2011-01-04'), 
('2011-01-05'), 
('2011-01-06'), 
('2011-01-07'), 
('2011-01-08'), 
('2011-12-31'),
('2012-01-01')

select dt, 
week = case when dt + 6 - datediff(day, -1, dt) % 7 = dateadd(year, datediff(year,-1, dt), 0)
then 1 else datepart(week, dt) end from @t

Enter Parameters

Options:
Switch sites:
loading Hold tight while we fetch your results
:records returned in :time ms:cached