0

現在のDateTimeが2つのDateTimesの間にあるかどうかをチェックしたいですか?Sqlテーブルから有効な日付時刻を確認する方法

初めての場合は2016-05-19 04:23:00.000、もう1度の場合は2016-05-19 04:50:00.000です。

現在のDateTimeが1回目と2回目の間にある場合にtrueを返すクエリを書き込む方法、それ以外の場合はfalseを返しますか?

+7

グーグルなら、ほんの数百例あります。ここにソリューションのSOのポストがあります... http://stackoverflow.com/questions/11745650/isdate-function-in-sql-evaluates-invalid-dates-as-valid – dinotom

答えて

0
Select * 
From Table 
Where 
    ('2016-05-19 04:23:00.000' <= dateColumn) 
    And (dateColumn < '2016-05-19 04:50:00.000') 
+0

私は時間があればどうすればいいのでしょうか? 05-19 04:23:00.000 ''テーブルに保存されていますか?私はここでこの時間をどうやって得るのですか? –

+0

@JaniManiあなたの受け入れられた答えに対する私の編集と私のコメントを見てください。 'Between'と' DateTimes'を使用しないでください。 – shadow

1

基本的なケースの表現でこれを非常に簡単に行うことができます。あなたは、あなたが何をしているか知っている絶対確実であり、あなたは絶対に日時概念理解しない限り、日付時刻との間で使用して

case when FirstTime <= getdate() AND getdate() <= SecondDate 
    then 'True' 
    else 'False' 
end 
0

ストップ。

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 
関連する問題