2011-08-08 2 views

Répondre

3

Ceci est une fonction qui sera de retour le samedi suivant si vous l'appelez comme ça :

SELECT dbo.fn_Get_NextWeekDay('2011-08-08', 6) 

Le "6" vient de the list of possible values vous pouvez définir pour DATEFIRST.

Vous pouvez obtenir n'importe quel autre jour de la semaine en modifiant le second paramètre en conséquence.

C'est la fonction:

IF OBJECT_ID('dbo.fn_Get_NextWeekDay') IS NOT NULL 
    DROP FUNCTION dbo.fn_Get_NextWeekDay 
GO 
CREATE FUNCTION dbo.fn_Get_NextWeekDay(
    @aDate DATETIME 
    , @dayofweek  INT 
    /* 
     @dw - day of the week 
     1 - Monday 
     2 - Tuesday 
     3 - Wednesday 
     4 - Thursday 
     5 - Friday 
     6 - Saturday 
     7 - Sunday 
    */ 
) 
RETURNS DATETIME 
AS 
/* 
    SELECT dbo.fn_Get_NextWeekDay('2011-08-08', 6) 
    SELECT dbo.fn_Get_NextWeekDay('2011-08-08', 1) 
*/ 
BEGIN 
    RETURN 
     DATEADD(day 
     , (@dayofweek + 8 - DATEPART(dw, @aDate) - @@DATEFIRST) % 7 
     , @aDate 
    ) 
END 
GO 

[EDIT] Cela pourrait être une autre solution. Cela devrait fonctionner dans toutes les langues:

IF OBJECT_ID('dbo.fn_NextWeekDay') IS NOT NULL 
    DROP FUNCTION dbo.fn_NextWeekDay 
GO 
CREATE FUNCTION dbo.fn_NextWeekDay(
    @aDate  DATE 
    , @dayofweek NVARCHAR(30) 
) 
RETURNS DATE 
AS 
/* 
    SELECT dbo.fn_NextWeekDay('2016-12-14', 'fri') 
    SELECT dbo.fn_NextWeekDay('2016-03-15', 'mon') 
*/ 
BEGIN 
    DECLARE @dx INT = 6 
    WHILE UPPER(DATENAME(weekday,@aDate)) NOT LIKE UPPER(@dayofweek) + '%' 
    BEGIN 

    SET @aDate = DATEADD(day,1,@aDate) 

    SET @[email protected] 
    if @dx < 0 
    BEGIN 
     SET @aDate = NULL 
     BREAK 
    END 
    END 

    RETURN @aDate 

END 
GO 
+0

Ceci est une excellente solution mais est non déterministe et ne fonctionnera dans aucune langue. –

+0

Pour la valeur dbo.fn_Get_NextWeekDay ('2016-12-14', 2) la fonction renvoie 2016-12-13 00: 00: 00.000 dans certaines configurations –

2

Utilisez DATEPART pour obtenir le jour de la semaine d'aujourd'hui et d'ajouter la différence au jour de la semaine souhaitée à la date d'aujourd'hui.

3
DECLARE @Today date = 'TODAYS-DATE'; 
DECLARE @TodayNumber int = DATEPART(dw, @Today) -- Get the day number 
DECLARE @Saturday date = DATEADD(DAY, ([email protected])%7, @Today) 
-- Add the number of days between today and saturday (the 6th day), modulus 7 to stop you adding negative days 

Hope that helps!

1

Essayez ceci:

SET DATEFIRST 7 
DECLARE @d DATETIME 
SET @d = '2011-08-08' --GETDATE() 

SELECT NEXT_SAT = DATEADD(day, (7 + @@DATEFIRST - DATEPART(dw, @d)) % 7, @d) 
+0

mais 'SET DATEFIRST 7' cela ne peut pas être utilisé dans une fonction SQL .. que faire? – Sreekumar

+0

Quelle est votre valeur actuelle de SET DATEFIRST? (sélectionnez @@ DATEFIRST) – leoinfo

+0

la valeur actuelle est 7. mais il pourrait changer de SYSTEM à SYSTEM ..right ?? – Sreekumar

2

Utilisez une table de calendrier (tableau avec une ligne par jour):

SELECT MIN(DateValue) DateValue 
FROM Calendar 
WHERE DateValue >= CURRENT_TIMESTAMP 
AND DayOfWeek = 'Saturday'; 
Questions connexes