我正在编写一个SQL脚本来从表中删除一些行。
我需要删除具有特定类型(简单)的行,其中估值时间戳是创建日期小于下午4点的当天的16:00。这有意义吗?
delete from cfo_daily_trans_hist where dh_dd_type_id = 'valuation' --and dh_val_time_stamp is today at 16:00 where the dh_create_dt is today at a time earlier than 16:00
我真的不知道进行确切日期比较的语法。
谢谢你们。
答案 0 :(得分:2)
DELETE [dbo].[cfo_daily_trans_hist]
WHERE [dh_dd_type_id] = 'valuation'
AND [dh_val_time_stamp] = DATEADD(HH, 16, CONVERT(DATETIME,CONVERT(DATE,GETDATE()))) -- is today at 16:00
AND [the dh_create_dt] < DATEADD(HH, 16, CONVERT(DATETIME,CONVERT(DATE,GETDATE())))-- is today at a time earlier than 16:00
答案 1 :(得分:1)
And dh_val_time_stamp = DateAdd(Hour, 16, DateAdd(Day, DateDiff(Day, 0, GetDate()), 0))
And dh_create_dt >= DateAdd(Day, DateDiff(Day, 0, GetDate()), 0)
And dh_create_dt < DateAdd(Hour, 16, DateAdd(Day, DateDiff(Day, 0, GetDate()), 0))
答案 2 :(得分:0)
-- =================
-- sample data
-- =================
declare @t table
(
dh_val_time_stamp datetime,
dh_create_dt datetime
)
declare @d datetime
set @d = cast(cast(cast(getdate() as float) as int) as datetime)
insert into @t values (dateadd(hh, 16, @d), dateadd(hh, 14, @d))
insert into @t values (dateadd(hh, 1, @d), dateadd(hh, 2, @d))
-- =================
-- solution
-- =================
select
*
from @t t1
cross apply
(
select d = dateadd(hh, 16, cast(cast(cast(getdate() as float) as int) as datetime))
) t2
where dh_val_time_stamp = d
and dh_create_dt < d