检查输入日期是否在开始日期和结束日期表之内

时间:2019-02-18 10:28:31

标签: sql sql-server date

我想运行查询以检查输入日期是否在表格中任何给定的期限内。

学期日期表示例:

<table border="1">
<tr><th>termID</th><th>txtStartDate</th><th>txtFinishDate</th></tr>
<tr><td>37</td><td>2017-09-05 00:00:00</td><td>2017-12-15 23:59:00</td></tr>
<tr><td>38</td><td>2018-01-09 00:00:00</td><td>2018-03-29 23:59:00</td></tr>
<tr><td>39</td><td>2018-04-24 00:00:00</td><td>2018-07-06 23:59:00</td></tr>
<tr><td>40</td><td>2018-09-04 00:00:00</td><td>2018-12-14 23:59:00</td></tr>
<tr><td>41</td><td>2019-01-08 00:00:00</td><td>2019-03-29 23:59:00</td></tr>
<tr><td>42</td><td>2019-04-24 00:00:00</td><td>2019-07-05 23:59:00</td></tr></table>

给出一个日期,例如今天的日期,它是否介于行的开始日期和结束日期之间。

示例代码方法

date = now()
for (row in rows):
    if date between row.txtStartDate and row.txtEndDate:
        return "yes"

我可以在具有给定日期的逻辑之间使用,但是不确定如何将其应用于整个表。

select case when getdate() between '2019-04-24 00:00:00' and '2019-07-05 23:59:00' then 'yes' else 'no' END 

谢谢

3 个答案:

答案 0 :(得分:1)

尝试使用private func createPlayerForVideoIndex(index: Int){ guard let videoComposition = videoFilterComposition else { return } self.player?.pause() self.audioPlayer?.pause() playerLayer?.removeFromSuperlayer() let video = videoClipsArray.object(at: index) as! VideoFileModel playerItem = AVPlayerItem(url:NSURL(fileURLWithPath: video.localFilePath) as URL) playerItem?.videoComposition = videoComposition self.player = AVPlayer(playerItem: playerItem) playerLayer = AVPlayerLayer(player: player) playerLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill playerLayer?.frame = CGRect(x:0, y:67, width:self.view.frame.size.width, height:250) self.view.layer.addSublayer(playerLayer!) if !myScrollView.isDescendant(of: self.view) { myScrollView.frame = (playerLayer?.frame)! myScrollView.delegate = self myScrollView.isPagingEnabled = true myScrollView.showsHorizontalScrollIndicator = false myScrollView.showsVerticalScrollIndicator = false myScrollView.backgroundColor = UIColor.clear self.view.addSubview(myScrollView) } self.view.bringSubview(toFront: self.pageControl) self.myScrollView.contentSize = CGSize(width:self.myScrollView.frame.size.width * 4,height: self.myScrollView.frame.size.height) let startSeconds : Int64 = Int64(video.leftRangeValue) let targetTime:CMTime = CMTimeMake(startSeconds, 1) self.player?.seek(to: targetTime) let endSeconds : Int64 = Int64(video.rightRangeValue) self.player?.currentItem?.forwardPlaybackEndTime = CMTimeMake(endSeconds, 1) self.player?.volume = videoVolumeSlider.value self.player?.play() self.audioPlayer?.play() self.player?.rate = video.slowFastMotionRate } 运算符:

Video

答案 1 :(得分:1)

您需要引用表的列,而不是对值进行硬编码。您可以使用表的别名和正确的列名来完成此操作。

DECLARE @InputDate DATE = '2019-01-05'

SELECT
    T.*
FROM
    YourTable AS T
WHERE
    @InputDate BETWEEN T.txtStartDate AND T.txtEndDate

使用WHERE子句将过滤表中的行,并仅显示条件为true的行。如果您将条件作为新列的表达式(在SELECT列列表中)移动,则它将使用该新表达式显示所有行:

DECLARE @InputDate DATE = '2019-01-05'

SELECT
    T.*,
    IsInputDateBetweenDates = CASE
        WHEN @InputDate BETWEEN T.StartDate AND T.EndDate THEN 'Yes'
        ELSE 'No' END
FROM
    YourTable AS T

答案 2 :(得分:0)

您也可以尝试使用IF EXISTS,如下所示。

If Exists (Select 1 from YourTable where GETDATE() between txtStartDate AND txtEndDate)
Begin
    --Your if logic
end
else
begin
    --Your else logic
end