用于搜索3个日期字段的SQL查询

时间:2015-01-21 20:05:48

标签: sql sql-server

我试图针对3个不同的字段运行查询。我希望它返回满足第一个范围的所有帐户,带回所有符合第二个范围的帐户,并且第三个帐户相同。我尝试使用and,但获得的范围超出了范围。

select 
    *
from 
    Permits 
where 
    created between '1/1/2015' and '1/21/2015'  
    and updated between '1/1/2015' and '1/21/15'  
    and noResponseDateSet between '1/1/15' and '1/21/15' 
order by 
    alarmNo 

感谢您的帮助

2 个答案:

答案 0 :(得分:1)

使用ISO标准日期格式:

select created, updated, noResponseDateSet
From Permits
where created between '2015-01-01' and '2015-01-21' and
      updated between '2015-01-01' and '2015-01-21' and
      noResponseDateSet between '2015-01-01' and '2015-01-21' ;

这应该可以解决问题,除非你有一个相当神秘的国际日期设置的组合。

答案 1 :(得分:0)

CONVERT可以为您执行脏解析工作。它将接受1/1/201501-01-2015或大多数其他组合。

select *
From Permits
where created between CONVERT(DATETIME,'1/1/2015') and  CONVERT(DATETIME,'1/21/2015')
and updated between CONVERT(DATETIME,'1/1/2015') and CONVERT(DATETIME,'1/21/15')
and noResponseDateSet between CONVERT(DATETIME,'1/1/15') and CONVERT(DATETIME,'1/21/15')
order by alarmNo