验证两个日期(日期开始和日期结束)的最佳方法

时间:2015-03-16 00:59:48

标签: sql-server validation date

你在SQ​​L SERVER中有一个表,它包含Date StartDate End 如何知道表中是否存在插入的日期。

示例:

01-Jan-1515-Jan-15已插入表格

01-Mar-1515-Mar-15已插入表格

01-Feb-1515-Feb-15有效,因为它不在范围内。

4 个答案:

答案 0 :(得分:0)

DECLARE @StartDate DATE = '2015-02-01';
DECLARE @EndDate DATE = '2015-02-15';

SELECT COUNT(1) c
FROM YourTable t
WHERE StartDate < @EndDate AND EndDate > @StartDate
如果没有重叠,

c应为0。这就是你需要做的。如果您计算重叠的同一天,请使用>=<=

答案 1 :(得分:0)

以下是我采取的方法:

  1. 创建用户定义的函数(UDF)以计算表中的重叠次数
  2. 在表格中添加此UDF作为检查约束。这可以防止重叠日期的插入
  3. 详情如下。下表是我使用的示例表。

    CREATE TABLE [dbo].[Dates](
        [Id] [int] IDENTITY(1,1) NOT NULL,
        [StartDate] [datetime] NULL,
        [EndDate] [datetime] NULL,
        CONSTRAINT [PK_Dates] PRIMARY KEY CLUSTERED ( [Id] ASC )
            WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,
            ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    

    然后,我创建了用户定义函数(UDF)。

    CREATE FUNCTION f_CountOverlaps (
        @StartDate DATE,
        @EndDate DATE
    ) RETURNS INT
    AS
    BEGIN
        DECLARE @Rows INT = (
            SELECT
                COUNT(*)
            FROM dbo.Dates D
            WHERE (D.EndDate IS NULL OR @StartDate IS NULL OR @StartDate <= D.EndDate)
                AND (D.StartDate IS NULL OR @EndDate IS NULL OR @EndDate >= D.StartDate)
        )
        RETURN COALESCE(@Rows, 1)
    END
    GO
    

    最后,我将用户定义的函数添加到表的检查约束中。请注意,您必须检查您的函数是否检查行计数为1(而不是0)。这是因为您的约束在插入完成之后但在将插入提交到表之前运行。

    ALTER TABLE dbo.Dates
        ADD CONSTRAINT chkOverlaps CHECK (dbo.f_CountOverlaps(StartDate, EndDate) = 1 );
    

    然后,我用插页测试了这个。

    INSERT dbo.Dates VALUES ('1/1/2015', '1/1/2015') -- Good
    INSERT dbo.Dates VALUES ('3/1/2015', '3/1/2015') -- Good
    INSERT dbo.Dates VALUES ('2/1/2015', '2/1/2015') -- Good
    INSERT dbo.Dates VALUES ('2/1/2015', '2/1/2015') -- Bad, dupe
    INSERT dbo.Dates VALUES ('2/28/2015', '3/2/2015') -- Bad, overlap
    

    &#34;坏&#34;由于检查限制,insert会抛出错误消息:

    Msg 547, Level 16, State 0, Line 42
    The INSERT statement conflicted with the CHECK constraint "chkOverlaps". The conflict occurred in database "SampleDB", table "dbo.Dates".
    

    最后,检查表的内容只显示我的&#34;好&#34;允许插入:

    Id          StartDate               EndDate
    ----------- ----------------------- -----------------------
    1           2015-01-01 00:00:00.000 2015-01-01 00:00:00.000
    2           2015-03-01 00:00:00.000 2015-03-01 00:00:00.000
    3           2015-02-01 00:00:00.000 2015-02-01 00:00:00.000
    

答案 2 :(得分:0)

两者之间的功能应该有效。试试Select 1 from yourTable where yourDate is between 'date start' and 'date end'

答案 3 :(得分:0)

使用某些WHERE NOT EXISTS(...BETWEEN...)逻辑可以防止重叠插入,如以下过程:

CREATE PROCEDURE DateIns(@DateStart DATE, @DateEnd DATE)
AS
INSERT Dates(DateStart, DateEnd) 
SELECT @DateStart, @DateEnd
WHERE NOT EXISTS(
  SELECT * FROM Dates
  WHERE @DateStart BETWEEN DateStart AND DateEnd
  OR @DateEnd BETWEEN DateStart AND DateEnd
);

以下小提琴演示了接受三个有效范围并拒绝一个无效范围的程序:http://sqlfiddle.com/#!6/5fc6e/5