Home Forums SQL Server 2005 T-SQL (SS2K5) help-how to generate date backward from end to start RE: help-how to generate date backward from end to start

  • Matt Miller (1/16/2008)


    skip the d*** loops. Use a numbers table to do this:

    INSERT INTO @empShifts ([empID],[ShiftDate],[ShiftType],[StartDate] ,[EndDate])

    select @input_empID ,

    @current,

    shift.patternShiftValue ,

    @StartDate,

    dateadd(day,-1*spt.number,@EndDate)

    from @shifts_pattern as shift,

    master..spt_values spt

    where

    PatternId=@last_shift_id+1

    and spt.type='P'

    and dateadd(day,-1*spt.number,@EndDate) between @current and @enddate

    can you help me this is all my code

    DECLARE

    @shifts_pattern TABLE ([PatternId] [int] IDENTITY(1,1 ) NOT NULL, [patternShiftValue] [int]NOT NULL)

    declare

    @I int

    set

    @i=0

    while

    @i < 5

    BEGIN

    INSERT INTO @shifts_pattern ([patternShiftValue] )

    SELECT 1 UNION ALL

    SELECT 2 UNION ALL

    SELECT 3 UNION ALL

    SELECT 4 UNION ALL

    SELECT 5 UNION ALL

    SELECT 6 UNION ALL

    SELECT 7 UNION ALL

    SELECT 8

    set

    @i=@i+1

    end

    declare

    @empList

    TABLE

    ( [empID] [numeric](18, 0) NOT NULL,[ShiftType] [int]NULL,[StartDate][datetime]NOT NULL,[EndDate] [datetime] NOT NULL)

    INSERT INTO

    @empList ([empID], [ShiftType],[StartDate],[EndDate])

    SELECT 111111,1,CONVERT(DATETIME, '01/01/2008', 103), CONVERT(DATETIME, '17/01/2008', 103)

    -- create shifts table

    declare

    @empShifts

    TABLE ( [empID] [numeric](18, 0) NOT NULL,[ShiftDate] [datetime]NOT NULL,[ShiftType] [int]NULL ,[StartDate] [datetime]NOT NULL,[EndDate] [datetime]NOT NULL)

    DECLARE

    @StartDate datetime

    DECLARE

    @EndDate datetime

    Declare

    @current datetime

    DEclare

    @last_shift_id int

    Declare

    @input_empID int

    ----------------- open list table for emp with curser

    DECLARE

    List_of_emp CURSOR FOR

    SELECT

    emp.empId,emp.ShiftType,emp.StartDate,emp.EndDate FROM @empList emp

    OPEN

    List_of_emp

    FETCH

    List_of_emp INTO @input_empID , @last_shift_id ,@StartDate,@EndDate

    SET @current = @StartDate

    -----------------

    -- loop on all emp in the list

    while

    @@Fetch_Status = 0

    begin

    -- loop to insert info of emp shifts

    while

    @current<=@EndDate

    begin

    INSERT INTO @empShifts ([empID],[ShiftDate],[ShiftType],[StartDate] ,[EndDate])

    select @input_empID ,@current,shift .patternShiftValue ,@StartDate,@EndDate

    from @shifts_pattern as shift where PatternId=@last_shift_id+1

    -- if it is Friday and we are on one of the first shift we don't move to next shift type .

    if (DATENAME(dw ,@current) = 'Friday' ) and

    EXISTS(select ShiftType from @empShifts where ShiftDate=@current and empID= @input_empID and ShiftType in ( 1,2,3))

    -- do nothing

    --set @last_shift_id=@last_shift_id

    print ('friday first shift')

    ELSE

    set @last_shift_id=@last_shift_id+ 1

    set @current=DATEADD( d,1, @current)

    end

    FETCH

    List_of_emp INTO @input_empID ,@last_shift_id,@StartDate,@EndDate

    -- init of start date for the next emp

    set

    @current = @StartDate

    end

    CLOSE

    List_of_emp

    DEALLOCATE

    List_of_emp

    select

    empID,shiftDate,DATENAME (dw,shift.ShiftDate ), shiftType from @empShifts as shift

    RETURN