获取介于ID日期和正负日期之间的日期

时间:2019-12-13 07:12:53

标签: sql-server tsql sql-server-2008-r2

我有以下示例数据:

CREATE TABLE tblDates
(
    ID int,
    Dates DATE
);

SELECT * FROM tblDates

INSERT INTO tblDates VALUES(1,'2019-12-01');
INSERT INTO tblDates VALUES(2,'2019-12-05');
INSERT INTO tblDates VALUES(3,'2019-12-02');
INSERT INTO tblDates VALUES(4,'2019-12-09');
INSERT INTO tblDates VALUES(5,'2019-12-11');

在这里,我正在寻找date的{​​{1}}到1,2,.. n天的正负之间的日期。

尝试1:我尝试使用UNION ALL。

ID = 4

当我要查找50天或更多天数差异时,这种方法不好。

尝试2:

SELECT Dates FROM tblDates WHERE ID = 4 
UNION ALL
SELECT DATEADD(day,1,Dates) FROM tblDates WHERE ID = 4;

有单个日期。

尝试3:

创建的函数:获取日期的函数

SELECT Dates FROM tblDates WHERE ID = 4 AND Dates between Dates AND DATEADD(day,1,Dates);

查询:

CREATE FUNCTION udf_GetDates(@MinDate DATE,@MaxDate DATE)
RETURNS TABLE
AS
RETURN
SELECT  TOP (DATEDIFF(DAY, @MinDate, @MaxDate) + 1)
        Date = DATEADD(DAY, ROW_NUMBER() OVER(ORDER BY a.object_id) - 1, @MinDate)
FROM    sys.all_objects a
        CROSS JOIN sys.all_objects b;

出现错误:

  

消息4104,级别16,状态1,第2行多部分标识符   无法绑定“ t.Dates”。讯息4104,第16级,状态1,第2行   多部分标识符“ t.Dates”无法绑定。

预期输出:

给出:ID = 4,天= + 1

SELECT f.* 
FROM udf_GetDates(t.Dates,DATEADD(day,1,t.Dates)) f        
INNER JOIN tblDates t ON f.[Date] = t.[Dates]
WHERE t.ID = 4

给出:ID = 4,天= + 10

Dates
----------- 
2019-12-09
2019-12-10

给出:ID = 4,天= -5

Dates
----------- 
2019-12-09
2019-12-10
2019-12-11
2019-12-12
2019-12-13
2019-12-14
2019-12-15
2019-12-16
2019-12-17
2019-12-18

4 个答案:

答案 0 :(得分:2)

尝试此查询

FIDDLE DEMO

功能

CREATE FUNCTION udf_GetDates (@StartDate DATE, @Range INT)
RETURNS TABLE
AS
RETURN
    SELECT  
        DATEADD(DAY, nbr - 1, @StartDate) myDate
    FROM    
        (SELECT    
             ROW_NUMBER() OVER (ORDER BY c.object_id) AS Nbr
         FROM      
             sys.columns c) nbrs
    WHERE   
        nbr - 1 <= @Range

查询用法1:

SELECT f.myDate 
FROM udf_GetDates((SELECT dates FROM tblDates WHERE ID = 4), 2) f      

查询用法2:

SELECT t.*, P.*
FROM tblDates t 
OUTER APPLY udf_GetDates(t.Dates, 5) p
WHERE t.ID = 4

更新的答案:

下次约会

CREATE FUNCTION udf_GetDates (@StartDate DATE, @Range INT)
RETURNS TABLE
AS
RETURN
    SELECT  
        DATEADD(DAY, nbr - 1, @StartDate) myDate
    FROM    
        (SELECT    
             ROW_NUMBER() OVER (ORDER BY c.object_id) AS Nbr
         FROM      
             sys.columns c) nbrs
    WHERE   
        nbr - 1 <= @Range

以前的日期

CREATE FUNCTION [udf_GetDates_Minuus] (@StartDate DATE, @Range INT)
RETURNS TABLE
AS
RETURN
    SELECT  
        DATEADD(DAY, -(nbr - 1), @StartDate) myDate
    FROM    
        (SELECT    
             ROW_NUMBER() OVER (ORDER BY c.object_id) AS Nbr
         FROM      
             sys.columns c) nbrs
    WHERE   
        nbr - 1 <= @Range

单个功能中的下一个和上一个日期

CREATE FUNCTION udf_GetDatesNextandPrevious(@StartDate DATE, @Range INT)
RETURNS TABLE
AS
RETURN
    SELECT  
        DATEADD(DAY, nbr - 1, @StartDate) myDate
    FROM    
        (SELECT    
             ROW_NUMBER() OVER (ORDER BY c.object_id) AS Nbr
         FROM      
             sys.columns c) nbrs
    WHERE   
        nbr - 1 <= @Range

    UNION

    SELECT  
        DATEADD(DAY, -(nbr - 1), @StartDate) myDate
    FROM    
        (SELECT    
             ROW_NUMBER() OVER (ORDER BY c.object_id) AS Nbr
         FROM      
             sys.columns c) nbrs
    WHERE   
        nbr - 1 <= @Range

Updated Fiddle

答案 1 :(得分:1)

在sql server中尝试此代码

DECLARE @selecteddate DATE 
DECLARE @day INT = 10 
DECLARE @id INT = 4; 
DECLARE @count INT = 0; 
DECLARE @table1 TABLE 
  ( 
     date_ DATETIME 
  ) 

SELECT @selecteddate = dates 
FROM   tbldates 
WHERE  id = @id; 

IF( @count <= @day ) 
  BEGIN 
        if(@day > 1)
        begin 
            set @day = @day - 1
        end
      WHILE @count <= @day 
        BEGIN 

            INSERT INTO @table1 
            VALUES      (Dateadd(day, @count, @selecteddate)) 

            SET @count = @count + 1 
        END 
  END 
ELSE 
  BEGIN 
      WHILE @count > @day 
        BEGIN 
            INSERT INTO @table1 
            VALUES      (Dateadd(day, @count, @selecteddate)) 

            SET @count = @count - 1 
        END 
  END 

SELECT * 
FROM   @table1 
ORDER  BY 1 

答案 2 :(得分:0)

我尝试使用其他格式创建函数

create FUNCTION udf_GetDates(@MinDate DATE,@MaxDate DATE)
RETURNS @_result table (dt date)
AS
begin
    insert into @_result
    SELECT  TOP (DATEDIFF(DAY, @MinDate, @MaxDate) + 1)
            Date = DATEADD(DAY, ROW_NUMBER() OVER(ORDER BY a.object_id) - 1, @MinDate)
    FROM    sys.all_objects a
            CROSS JOIN sys.all_objects b;
    return
end

并选择这样的结果

declare @_date date=(select Dates from tblDates where ID=4)
select *
from udf_GetDates(@_date,DATEADD(day,1,@_date))

我得到了您想要的结果

dt
2019-12-09
2019-12-10

答案 3 :(得分:0)

此SQL将返回两个范围之间的正确日期。只需根据您的需求进行调整即可。

    DECLARE @From               DATETIME,    
            @To                 DATETIME

    SELECT  @From           =   '2019-11-13',
            @To             =   '2019-11-19'

    ;WITH   Numbers         AS
(
    SELECT  0               AS  Number
UNION ALL
    SELECT  Number + 1      AS  Number
    FROM    Numbers
    WHERE   Number          <   DATEDIFF(d, @From, @To)
)
    SELECT  DATEADD(d, 
            Number, @From)  AS Date
    FROM    Numbers

-结果

2019-11-13 00:00:00.000
2019-11-14 00:00:00.000
2019-11-15 00:00:00.000
2019-11-16 00:00:00.000
2019-11-17 00:00:00.000
2019-11-18 00:00:00.000
2019-11-19 00:00:00.000