SQL查询日期比较选项

时间:2014-06-13 18:23:51

标签: sql sql-server

我试图将当前日期与数据库日期时间列进行比较。我尝试了以下三个选项,但没有运气。有什么想法吗?

选项1

select something from tableA
where  cast(lastupdateddate as date)=cast(GETDATE() as date)

选项2

select something from tableA
where  CONVERT(date, lastupdateddate)=CONVERT(date, GETDATE())

选项3

select something from tableA
where  CONVERT(VARCHAR(8), lastupdateddate,1)=CONVERT(VARCHAR(8), GETDATE(),1)

1 个答案:

答案 0 :(得分:1)

如果列具有真正的datetime数据类型,则第一个选项应该已经可以正常工作

create table tablea (
  id int,
  lastupdateddate datetime
);

和查询

select *
from tablea
where cast(lastupdateddate as date) = cast(getdate() as date)

请参阅SQLFiddle