sql - How can I generate Week ending dates (Saturdays) within a date range -
i need generate either column in query or temp table (not sure 1 required)
so can have list of dates on saturday fall within given date range.
this list used in join associate records weeks.
what options?
sample input:
from: 03/01/2013
to: 04/30/2013
results:
week ending - 03/02/2013 - 03/09/2013 - 03/16/2013 - 03/23/2013 - 03/30/2013 - 04/06/2013 - 04/13/2013 - 04/20/2013 - 04/27/2013 - 05/04/2013
current code:
create table #tbl7(year int, weeknumber int, startdate datetime, enddate datetime) begin declare @startdate datetime , @enddate datetime , @ctr int set @startdate = cast(2013 varchar)+ '/01/01' set @enddate = cast(2013 varchar) + '/12/31' set @ctr = 0 while @enddate >= @startdate begin set @ctr = @ctr + 1 insert #tbl7 values(year(@startdate), @ctr, @startdate, @startdate + 6) set @startdate = @startdate + 7 end end select * #tbl7
this oracle code. sorry not know how convert sql server. should not hard. need use proper functions in place of to_date() , to_char(), , calc difference between start , end date, e.g. (end_date-start_date)+1:
with data(r, some_date) ( select 1 r, to_date('03/01/2013', 'mm/dd/yyyy') some_date dual union select r+1, to_date('03/01/2013', 'mm/dd/yyyy')+r data r < 61 -- (end_date-start_date)+1 ) select some_date , to_char(some_date, 'dy') wk_day data to_char(some_date, 'dy') = 'sat' / some_date wk_day -------------------- 3/2/2013 sat 3/9/2013 sat 3/16/2013 sat 3/23/2013 sat 3/30/2013 sat 4/6/2013 sat 4/13/2013 sat 4/20/2013 sat 4/27/2013 sat
Comments
Post a Comment