按日期从SQL Server删除记录并忽略时间戳

时间:2016-12-20 09:48:24

标签: sql-server

由于时间戳而按日期删除的SQL Server脚本无效。如果我有正确的时间戳作为表中的实际记录,那么它会删除记录,但我需要忽略时间

USE [abc]
GO

DELETE FROM [dbo].[COURSE_INSTANCE_EK]
  WHERE [BAInstanceCacheDate] = '2016-12-20 00:00:00.00' 
GO

3 个答案:

答案 0 :(得分:3)

您只需要将数据转换为date数据类型,这将删除时间部分:

USE [abc]
GO

DELETE FROM [dbo].[COURSE_INSTANCE_EK]
  WHERE cast([BAInstanceCacheDate] as date) = '20161220' 
GO

答案 1 :(得分:3)

尝试这样的事情

Declare @tbl table (id int,dt datetime)

insert into @tbl values(1,'2016-12-20 15:21:23.120')
insert into @tbl values(2,'2016-12-18 15:21:23.120')
insert into @tbl values(3,'2016-12-07 15:21:23.120')
insert into @tbl values(4,'2016-12-11 15:21:23.120')
insert into @tbl values(5,'2016-12-11 15:21:23.120')

select * from @tbl
select * from @tbl where convert(date,dt,0) = '2016-12-11'
delete from @tbl where convert(date,dt,0) = '2016-12-11'
select * from @tbl

答案 2 :(得分:2)

试试这个..

USE [abc]
GO

DELETE FROM [dbo].[COURSE_INSTANCE_EK]
  WHERE CAST([BAInstanceCacheDate] AS DATE) = '2016-12-20'; 
GO