سؤال

INFO: MS SQL Server 2008 R2

Hello. I am trying to create a function that has two parameter inputs and one output. The two inputs are a day of the week as INT(@dowin) starting on Monday as 1, and a DATETIME(@datein). The purpose of the function would be to determine a new DATETIME output (@newdate) by looking at the week range created from (@datein) and picking a new DATETIME from that range using the day of the week INT (@dowin).

The reason: An appointment scheduler is using a start date and if the appointment is reoccuring it creates a day of the week that it reoccurs from the start date. I am creating an SSRS report from a Table View that I need an instance of each appointment occurance.

Here is the function script I have so far:

    CREATE FUNCTION UFsurgopsched(@datein DATETIME,@dowin int)
    RETURNS datetime
    AS
    BEGIN
        DECLARE @newdate datetime
        DECLARE @startOfWeek date
        DECLARE @endOfWeek date
    SELECT @newdate = datepart(dw,@dowin) as date in
        (
        SELECT
        convert(date, dateadd(dd, -1*(datepart(dw, @datein)-2), @datein)) AS @startOfWeek,
        convert(date, dateadd(dd, 7-(datepart(dw, @datein)-1), @datein)) AS @endOfWeek
        )
    RETURN @newdate
    END

What I am trying to get: - @dowin = 3 - @datein = 2014-02-11 07:30:00.000 - @datein should find the Range of: 2014-02-10 to 2014-02-16 Within that range the @newdate according to @dowin would be 2014-02-12 07:30:00.000

**EDIT*** I was able to solve after the assitance of everyone with a much simpler function:


    CREATE FUNCTION [dbo].[UFsurgopsched](@datein DATETIME,@dowin int)
    RETURNS DATETIME
    AS
    BEGIN
    DECLARE @newdate DATETIME
        DECLARE @dateweek DATETIME
        SET
            @dateweek =
                convert(datetime, dateadd(dd, -1*(datepart(dw, @datein)-2), @datein))
        SELECT
            @newdate =
                dateadd(DD ,@dowin - 1, @dateweek)
        RETURN
            @newdate
    END
هل كانت مفيدة؟

المحلول

Try This;

    CREATE FUNCTION UFsurgopsched(@datein DATETIME,@dowin int)
    RETURNS datetime
    as begin

    declare @dowinf int 
    DECLARE @newdate datetime
    if (@dowin)= 7 begin set @dowinf =1 end else set @dowinf =@dowin+1
            DECLARE @startOfWeek int = Datepart(DW,@datein)
    if @startOfWeek<@dowinf set @newdate = dateadd(day,@dowinf-@startOfWeek,@datein) else if @startOfWeek>@dowinf set @newdate = dateadd(day,7-(@startOfWeek-@dowinf),@datein) else set @newdate = dateadd(day,7,@datein) 
    return @newdate
    end

--------------Will return the next date from @datein where week day number @dowin matches

---------------For Example if insert 20140213 and weekday as 1

---------------will return the date of next Monday from the entered date

Hope this will work

نصائح أخرى

Not entirely sure I understand your criteria, but this is returning the desired result, based on your example:

        DECLARE @newdate datetime
        DECLARE @startOfWeek datetime
        DECLARE @endOfWeek datetime
        declare @datein DATETIME
        declare @dowin int


  set @dowin = 3
  set @datein = '2014-02-11 07:30:00.000'
/* -- Don't think you need any of this
SELECT @newdate = datepart(dw,@dowin) as date in
        (
        SELECT
        convert(date, dateadd(dd, -1*(datepart(dw, @datein)-2), @datein)) AS @startOfWeek,
        convert(date, dateadd(dd, 7-(datepart(dw, @datein)-1), @datein)) AS @endOfWeek
        )
*/
select
dateadd(dd,@dowin, convert(datetime, dateadd(dd, -1*(datepart(dw, @datein)-2), @datein)))

SQL Fiddle

I'm just calculating your "start of week" using your logic (just made it datetime), and adding your @dowin parm to it.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top