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

问题描述 投票:0回答:3

我想要数据表中的链接,其中徽标链接为 <= fromdate and >= 今日。

我编写此查询是为了获取值

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

sql sql-server sql-server-2008 sql-server-2008-r2
3个回答
0
投票

如果您想要徽标具有今天所在的日期范围,则反转条件:

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

这将在您的示例中返回带有

id = 2
的徽标。


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

0
投票

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

使用此查询

select * from getlogo where getdate() between convert(datetime,logofrom) 
and convert(datetime,logoto) and  active=0
© www.soinside.com 2019 - 2024. All rights reserved.