How many equal days are between two date ranges, SQL

前端 未结 2 1803
陌清茗
陌清茗 2021-01-17 04:03

I have table with date ranges, like this:

DATE          DATE2
14.03.2013    17.03.2013
13.04.2013    02.05.2013

I have to create a procedur

相关标签:
2条回答
  • 2021-01-17 04:36

    suppose your table is called daterange and you have parameters defined @param1 and param2 in your procedure then something on these lines should work:

    set @param1 := cast('2013-03-14' as date);
    set @param2 := cast('2013-03-16' as date);
    
    select 
    datediff(least(date2,@param2),@param1)+1
    from daterange where @param1 between date1 and date2
    

    See example in sqlfiddle

    0 讨论(0)
  • 2021-01-17 04:42

    You should be able to adapt this T-SQL function to MySQL

    IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[fu_RPT_OverlappingDateRangesDays]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
    DROP FUNCTION [dbo].[fu_RPT_OverlappingDateRangesDays]
    GO
    
    
    
    
    
    -- ======================================================================================================================
    -- Author:      Stefan Steiger
    -- ALTER date:  11.06.2015
    -- Alter date:  11.06.2015
    -- Description: Calculate the number of overlapping days in two date-ranges 
    -- http://stackoverflow.com/questions/20836429/how-to-gets-the-number-of-overlapping-days-between-2-ranges
    -- ======================================================================================================================
    
    
    -- DECLARE @firstStart datetime 
    -- DECLARE @firstEnd datetime 
    -- DECLARE @secondStart datetime 
    -- DECLARE @secondEnd datetime 
    
    -- SET @firstStart = '01.01.2015' 
    -- SET @firstEnd = '31.01.2015' 
    -- SET @secondStart = '15.01.2014' 
    -- SET @secondEnd = '15.02.2015' 
    
    -- SELECT dbo.fu_RPT_OverlappingDateRangesDays( @firstStart, @firstEnd, @secondStart, @secondEnd ) 
    CREATE FUNCTION [dbo].[fu_RPT_OverlappingDateRangesDays] 
    (
         @firstStart datetime 
        ,@firstEnd datetime 
        ,@secondStart datetime 
        ,@secondEnd datetime 
    )
        RETURNS integer 
    AS
    BEGIN 
        DECLARE @maxStart datetime 
        DECLARE @minEnd datetime 
        DECLARE @interval int 
    
        IF @firstStart IS NULL OR @firstEnd IS NULL OR @secondStart IS NULL OR @secondEnd IS NULL 
            -- RETURN 0
            RETURN NULL 
    
        IF @firstEnd < @firstStart 
            RETURN 0 
    
        IF @secondEnd < @secondStart 
            RETURN 0 
    
    
        SET @maxStart = @secondStart 
        SET @minEnd = @secondEnd 
    
        IF @firstStart > @secondStart 
            SET @maxStart = @firstStart 
    
        IF @firstEnd < @secondEnd 
            SET @minEnd =  @firstEnd 
    
        -- PRINT @maxStart 
        -- PRINT @minEnd 
    
        --SET @interval = DATEDIFF(DAY, @maxStart, @minEnd)  + 1 
        SET @interval = {fn timestampdiff(SQL_TSI_DAY, @maxStart, @minEnd)} + 1 
    
        IF @interval < 0
            SET @interval = 0 
    
        -- PRINT @interval 
        RETURN @interval 
    END 
    
    
    
    GO
    
    0 讨论(0)
提交回复
热议问题