我需要包含昨天註冊的所有記錄,但不包括今天已經註冊的任何記錄。我不確定這個查詢是否給了我正確的記錄數。請幫助SQL-select前一天,今天排除
select id, lastmodifieddate from stu_tbl
where last_modified_date > DATEADD(d, - 1, GETDATE()))
and _last_modified_date <> GETDATE())
我需要包含昨天註冊的所有記錄,但不包括今天已經註冊的任何記錄。我不確定這個查詢是否給了我正確的記錄數。請幫助SQL-select前一天,今天排除
select id, lastmodifieddate from stu_tbl
where last_modified_date > DATEADD(d, - 1, GETDATE()))
and _last_modified_date <> GETDATE())
試試這個:
select id, last_modifieddate from stu_tbl
where last_modified_date >= DATEADD(d, -1, cast(GETDATE() as date))
and last_modified_date < cast(GETDATE() as date)
謝謝,但最後一行'和last_modified_date
這裏是一個SQL Server 2008+的解決方案:
select id, lastmodifieddate
from stu_tbl
where last_modified_date >= cast(dateadd(day, -1, GetDate()) as Date)
and last_modified_date < cast(GetDate() as Date)
試試這個
declare @Dfrom datetime
declare @Dto datetime
declare @Dyesterday datetime
--Get date before current date
set @Dyesterday = dateadd(day, -1, getdate())
--Set date from to 12am
set @Dfrom = CONVERT(varchar(20), @Dyesterday, 101)
--set date to to 11:59:59PM
set @Dto = CONVERT(varchar(20), @Dyesterday, 101) + ' 23:59:59'
--if date and time is required
select id, lastmodifieddate
from stu_tbl
where last_modified_date between @Dfrom and @Dto
OR
--if date only and time is not required
select id, lastmodifieddate
from stu_tbl
where last_modified_date = @Dfrom
請參閱下面的答案。 – Jade