sql查询中的日期过滤器获取今天的值

时间:2015-03-25 10:14:53

标签: sql sql-server sql-server-2008 sql-server-2008-r2

我想要一个来自数据表的链接,其中徽标链接是< = fromdate和> = todate。

我'编写此查询以获取值

select *  from getlogo 

select * from getlogo 
where (Convert(date,logofrom) >= getdate()) and (Convert(date,logoto) <= getdate()) 
and active=0


 today = 03/25/2015
 from = database.value logofrom 
 to = database.value logoto     
 how to compare this and get value 

结果:

enter image description here

3 个答案:

答案 0 :(得分:0)

如果您希望徽标的日期范围在今天下降,请反转条件:

(Convert(date,logofrom) <= getdate()) and (Convert(date,logoto) >= getdate()) 

这会在您的示例中返回带有id = 2的徽标。

答案 1 :(得分:0)

您应该使用CAST GetDate来包含边缘日期:

SELECT  *
FROM    getlogo
WHERE   CAST(logofrom AS DATE) <= CAST(GETDATE() AS DATE)
        AND CAST(logoto AS DATE) >= CAST(GETDATE() AS DATE)
        AND active = 0

答案 2 :(得分:0)

如果根据您的描述,您需要第二条记录作为结果..

使用此查询

select * from getlogo where getdate() between convert(datetime,logofrom) 
and convert(datetime,logoto) and  active=0