简体   繁体   中英

SQL Query Date compare options

I'm trying to compare the current date with database datetime column. I tried the following three options, but no luck. Any idea?

Option 1

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

Option 2

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

Option 3

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

Right the first option should already work, provided the column has really datetime datatype, eg

create table tablea (
  id int,
  lastupdateddate datetime
);

and the query

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

See SQLFiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM