0

現在の DateTime が 2 つの DateTime の間にあるかどうかを確認したい。

1回目2016-05-19 04:23:00.000と2回目があり2016-05-19 04:50:00.000ます。

現在の DateTime が 1 回目と 2 回目の間にある場合に true を返すクエリを作成する方法、それ以外の場合は false を返す方法は?

4

3 に答える 3

1

基本的なケース式では、これを非常に簡単に行うことができます。

case when FirstTime <= getdate() AND getdate() <= SecondDate 
    then 'True' 
    else 'False' 
end
于 2016-05-20T18:26:47.753 に答える
0

何をしているのかを完全に理解し、日時の概念を完全に理解している場合を除き、 datetimeでの between の使用を停止します。

create table #test(
    Id int not null identity(1,1) primary key clustered,
    ActionDate datetime not null
)

insert into #test values
( '2015-12-31 23:59:59.99' ),
( '2016-01-01' ),
( '2016-01-10' ),
( '2016-01-31 23:59:59.99' ),
( '2016-02-01' )

select * from #test
-- all the rows
1   2015-12-31 23:59:59.990
2   2016-01-01 00:00:00.000
3   2016-01-10 00:00:00.000
4   2016-01-31 23:59:59.990
5   2016-02-01 00:00:00.000


-- lets locate all of January

-- using between
select * from #test
where
    ( ActionDate between '2016-01-01' and '2016-01-31' )

2   2016-01-01 00:00:00.000
3   2016-01-10 00:00:00.000
-- missing row 4

select * from #test
where
    ( ActionDate between '2016-01-01' and '2016-02-01' )

2   2016-01-01 00:00:00.000
3   2016-01-10 00:00:00.000
4   2016-01-31 23:59:59.990
5   2016-02-01 00:00:00.000 -- this is not January

-- using < and >
select * from #test
where
    ( '2016-01-01' <= ActionDate )
    and ( ActionDate < '2016-02-01' )

2   2016-01-01 00:00:00.000
3   2016-01-10 00:00:00.000
4   2016-01-31 23:59:59.990


drop table #test 
于 2016-05-21T05:40:57.120 に答える
0
Select *
From Table
Where
  ( '2016-05-19 04:23:00.000' <= dateColumn )
  And ( dateColumn < '2016-05-19 04:50:00.000' )
于 2016-05-20T18:02:28.890 に答える