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

时间:2016-07-27 15:30:12

标签: javascript c# tsql

我的表包含3列:某些事件的开始和结束日期以及事件ID。

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

例如,我有2个时间跨度用于事件:从2016年8月1日到2016年8月8日以及2016年8月10日到2016年8月17日。这样我就无法插入任何开始和结束日期在1.08.2016和17.08.2016之间的新行

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

3 个答案:

答案 0 :(得分:0)

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)。

答案 1 :(得分:0)

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

  1. x1“DataGridView”(用于查看包含数据的列的表)

  2. x1“NumericUpDown”(选择活动ID)

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

  4. x1“按钮”(将事件添加到表格中)

  5. 这里有代码:

    // 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);
    }
    

    希望这有帮助。

答案 2 :(得分: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