无法在SQL Server 2008 R2中设置条件
我在测试表中有以下数据集:无法在SQL Server 2008 R2中设置条件,sql,sql-server,tsql,sql-server-2008-r2,Sql,Sql Server,Tsql,Sql Server 2008 R2,我在测试表中有以下数据集: create table test ( columndate date, columntime datetime ) insert into test values('2014-01-01','22:00:00') insert into test values('2014-01-02','06:00:00') insert into test values('2014-01-03','23:00:00') insert into test values('2014
create table test
(
columndate date,
columntime datetime
)
insert into test values('2014-01-01','22:00:00')
insert into test values('2014-01-02','06:00:00')
insert into test values('2014-01-03','23:00:00')
insert into test values('2014-01-04','05:00:00')
insert into test values('2014-02-01','10:00:00')
insert into test values('2014-02-01','13:00:00')
insert into test values('2014-02-01','15:00:00')
insert into test values('2014-02-01','05:00:00')
columndate columntime
------------------------------------
2014-01-01 1900-01-01 22:00:00.000
2014-01-02 1900-01-01 06:00:00.000
2014-01-03 1900-01-01 23:00:00.000
2014-01-04 1900-01-01 05:00:00.000
2014-02-01 1900-01-01 10:00:00.000
2014-02-01 1900-01-01 13:00:00.000
2014-02-01 1900-01-01 15:00:00.000
2014-02-01 1900-01-01 05:00:00.000
现在我只想在结果中显示夜间计时,例如:
columndate columntime
-----------------------------------
2014-01-01 1900-01-01 22:00:00.000
2014-01-02 1900-01-01 06:00:00.000
2014-01-03 1900-01-01 23:00:00.000
2014-01-04 1900-01-01 05:00:00.000
2014-02-01 1900-01-01 05:00:00.000
为此,我正在尝试以下脚本:
select * from test
where columndate between '2014-01-01' and '2014-02-01'
and cast(columntime as time) between '06:00:00' and '23:00:00'
select * from test
where columndate between '2014-01-01' and '2014-02-01'
and cast(columntime as time) between '05:00:00' and '23:00:00'
注意:我不会获得计时记录05:00:00
但当我使用以下脚本时:
select * from test
where columndate between '2014-01-01' and '2014-02-01'
and cast(columntime as time) between '06:00:00' and '23:00:00'
select * from test
where columndate between '2014-01-01' and '2014-02-01'
and cast(columntime as time) between '05:00:00' and '23:00:00'
注意:我将得到预期的结果,但我得到的是计时记录06:00:00
,我不想显示
如何修复它?这是否满足您的需要??:
select * from test
where columndate between '2014-01-01' and '2014-02-01'
and cast(columntime as time) between '05:00:00' and '23:00:00' and columntime ! ='06:00:00'
你真的很接近了:你需要使用一对
=
和而不是之间的,可能是05:30:00
或06:30:00
等等不符合我的需要。我不明白。。。你只想要23:00到06:00(即隔夜)的活动吗?@JoachimIsaksson,是的!为什么2014-01-01 1900-01-01 22:00:00.000
在您想要的结果中,而不是在23:00之后?打字错误?@JoachimIsaksson,道歉!我的打字错误。是的!这就是我要找的。伟大的非常感谢你。