在表中插入唯一的时间跨度

本文关键字:时间跨度 唯一 插入 | 更新日期: 2023-09-27 17:58:49

我有一个包含3列的表:一些事件的开始和结束日期以及事件id。

我需要插入新行,并以某种方式验证开始日期和结束日期之间的时间跨度是否与此事件的其他时间跨度相交。

例如,我有两个事件的时间跨度:从2016年8月1日到2016年9月8日,以及从2016年10月8日到2017年8月17日。因此,我无法插入任何起始日期和结束日期在2016年8月1日至2016年7月17日之间的新行

如何使用JavaScript、C#或T-SQL进行验证?

在表中插入唯一的时间跨度

这是我的C#解决方案。要做到这一点,你需要一个带有以下控件的窗体(窗口):

  1. x1"DataGridView"(查看带有数据的列的表)

  2. x1"NumericUpDown"(选择事件ID)

  3. x2"DateTimePicker"(选择开始和结束日期)

  4. x1"按钮"(将事件添加到表中)

这里有代码:

// This is your table with data in three columns
DataTable events = new DataTable();
// When the program starts
public Form1()
{
    InitializeComponent();
    // Add the columns to the table
    events.Columns.Add("ID", typeof(int));
    events.Columns.Add("Start Date", typeof(DateTime));
    events.Columns.Add("End Date", typeof(DateTime));
    // Set data and formats
    dataGridView1.DataSource = events;
    dataGridView1.Columns[1].DefaultCellStyle.Format = "dd/MM/yyyy"; // The way the date is shown
    dataGridView1.Columns[2].DefaultCellStyle.Format = "dd/MM/yyyy"; // Ex. 27-07-2016
}
// Add The row containing information about the event
private void btnAdd_Click(object sender, EventArgs e)
{
    // Weather or not the row should be added
    bool addRow = true;
    // Skip this if no rows are added yet
    foreach (DataRow row in events.Rows)
    {
        // Row id and dates
        int eventId = (int)row[0];
        DateTime rowStartDate = (DateTime)row[1];
        DateTime rowEndDate = (DateTime)row[2];
        // If new start date lies inside the timespan of an existing event
        bool newStartDateBetween = rowStartDate.Date <= dateTimePicker1.Value.Date && rowEndDate.Date >= dateTimePicker1.Value.Date;
        // If new end date lies inside the timespan of an existing event
        bool newEndDateBetween = rowStartDate.Date <= dateTimePicker2.Value.Date && rowEndDate.Date >= dateTimePicker2.Value.Date;
        // If any of these statements are true, show the error and dont add the new row
        if (eventId == numericUpDown1.Value || newStartDateBetween || newEndDateBetween || dateTimePicker2.Value.Date < dateTimePicker1.Value.Date)
        {
            if (eventId == numericUpDown1.Value)
                MessageBox.Show("Event ID already taken!");
            if (newStartDateBetween)
                MessageBox.Show("The starting date is within the timespan of event ID: " + row[0] + "!");
            if (newEndDateBetween)
                MessageBox.Show("The ending date is within the timespan of event ID: " + row[0] + "!");
            if (dateTimePicker2.Value.Date < dateTimePicker1.Value.Date)
                MessageBox.Show("The ending date is before the starting date!");
            // Dont add the row
            addRow = false;
            break;
        }
    }
    // Add the row if no errors
    if (addRow)
        events.Rows.Add((int)numericUpDown1.Value, dateTimePicker1.Value, dateTimePicker2.Value);
}

希望这能有所帮助。

DECLARE @DataX TABLE (
     EventID            INT IDENTITY
    ,StartDate      DATE
    ,EndDate        DATE
    )
INSERT INTO @DataX VALUES
     ('2016-08-01','2016-08-09')
    ,('2016-08-10','2016-08-17')
DECLARE  @NewStart          DATE = '2016-08-05'
        ,@NewEnd            DATE = '2016-08-18'
        ,@NewStartValid     TINYINT
        ,@NewEndValid       TINYINT
;WITH Validate
    AS (
        SELECT SUM(CASE WHEN @NewStart BETWEEN StartDate AND EndDate THEN 1 ELSE 0 END) AS VStart,
               SUM(CASE WHEN @NewEnd   BETWEEN StartDate AND EndDate THEN 1 ELSE 0 END) AS VEnd
            FROM @DataX
        )
SELECT @NewStartValid = VStart, @NewEndValid = VEnd
    FROM Validate
SELECT @NewStartValid, @NewEndValid

有效值为0,无效值大于0。此示例显示开始日期在其中一个日期范围内(结果:1),结束日期不在(结果:0)。

以下将返回任何确认

Declare @Table table (EventID int,StartDate Date,EndDate Date)
Insert into @Table values 
(1,'2016-08-01','2016-08-09'),
(2,'2016-08-10','2016-08-17')
Declare @StartDate Date = '2016-08-05'
Declare @EndDate   Date = '2016-08-18'
Select * 
      ,ValidStart = IIF(@StartDate Between StartDate and EndDate,0,1)
      ,ValidEnd   = IIF(@EndDate   Between StartDate and EndDate,0,1)
 From @Table 
 Where @StartDate Between StartDate and EndDate
    or @EndDate   Between StartDate and EndDate

返回

EventID StartDate   EndDate     ValidStart  ValidEnd
1       2016-08-01  2016-08-09  0           1