获取满足两个日期范围并在SQL Server中的特定时间内的数据库记录

时间:2016-05-16 06:46:30

标签: sql sql-server sql-server-2008 sql-server-2005 sql-server-2012

我有以下 [RestaurantOffer] 表。 优惠,仅在两个日期范围内(即FromDate和ToDate)和特定时间(FromTime,ToTime)有效。

enter image description here

所以我想写一个查询,它会在当前日期(今天)给我所有优惠,时间将超过当前时间(今天的当前时间) 。因为我不想获得过期的优惠。

数据库:

FromDate(date),
ToDate(date),
FromTime(time),
ToTime(time)

更新: **注意:**此优惠在24小时内无效。它仅在FromTime-ToTime范围内有效。 我想要的是什么:

1st:如果今天满足FromDate和Todate的话,请给我今天的所有记录  范围。

第二名:在获取今天的所有记录后,我想获得所有记录,如果超过或等于今天的当前时间。

3 个答案:

答案 0 :(得分:2)

尝试以下查询:

SELECT * FROM RestaurantOffer WHERE (GETDATE() >= FromDate AND GETDATE() <= ToDate) AND (cast(GETDATE() as datetime) >= FromTime AND cast(GETDATE() as datetime) <= ToTime);

答案 1 :(得分:1)

如果您将日期和时间字段首次投放到日期时间,则可以将日期和时间字段添加到一起以获取日期时间 所以

var files;

$('input[type=file]').on('change', function(event) {
  files = event.target.files;
});

$('form').on('submit', function(event) {
    event.stopPropagation();
    event.preventDefault();

    // Notify the user that this may take a while...

    var data = new FormData();
    $.each(files, function(key, value)
    {
        data.append(key, value);
    });

    $.ajax({
             contentType: false,
             processData: false
             type: 'POST',
             url:"some url i put here",
             data: data,
             cache: false,
             dataType: 'json',
             success: function(data, textStatus, jqXHR) {
                 if(typeof data.error === 'undefined') {
                     submitForm(event, data);
                 } else {
                    console.log('ERRORS: ' + data.error);
                 }
             },
             error: function(jqXHR, textStatus, errorThrown)
             {
                 console.log('ERRORS: ' + textStatus);
             }
             complete: function(jqXHR, textStatus, errorThrown)
             {
                 // Notify the user that the upload process is complete!
             }
    });
})

答案 2 :(得分:1)

试试这个:

--DROP TABLE #temp

CREATE TABLE #temp
    (
      FROMDate DATE
    , ToDate DATE
    , FromTime TIME
    , ToTime TIME
    );

INSERT  INTO #temp
        ( FROMDate, ToDate, FromTime, ToTime )
VALUES  ( '2016-05-01'  -- FROMDate - date
          , '2016-06-01'  -- ToDate - date
          , '11:30:00:000'  -- FromTime - time
          , '17:30:00:000'  -- ToTime - time
          ),
        ( '2016-05-01'  -- FROMDate - date
          , '2016-06-01'  -- ToDate - date
          , '11:30:00:000'  -- FromTime - time
          , '17:30:00:000'  -- ToTime - time
          ),
        ( '2016-05-01'  -- FROMDate - date
          , '2016-06-01'  -- ToDate - date
          , '11:30:00:000'  -- FromTime - time
          , '17:30:00:000'  -- ToTime - time
          ),
        ( '2016-05-01'  -- FROMDate - date
          , '2016-06-01'  -- ToDate - date
          , '11:30:00:000'  -- FromTime - time
          , '19:30:00:000'  -- ToTime - time
          );

SELECT  FROMDate
      , ToDate
      , FromTime
      , ToTime
FROM    #temp
WHERE   ( CAST(GETDATE() AS DATE) >= FROMDate
          AND CAST(GETDATE() AS DATE) <= ToDate
        )
        AND ( CAST(GETDATE() AS TIME) >= FromTime
              AND CAST(GETDATE() AS TIME) <= ToTime
            );