34,593
社区成员
发帖
与我相关
我的任务
分享
declare @table table (Time varchar(20),Num int)
insert into @table
select '200803010000CST',10
union all
select '200803011000CST',10
union all
select '200803052000CST',10
union all
select '200803053000CST',10
union all
select '200803040000CST',10
union all
select '200803031000CST',10
union all
select '200803032000CST',10
union all
select '200803043000CST',10
delete from @table
where datediff(day,convert(datetime,left(Time,8)),convert(datetime,'2008-03-13'))>10
/*
Time Num
---------------------
200803010000CST 10
200803011000CST 10
*/
/*
备注,用convert(datetime,'2008-03-13'),是因为您是昨天发的贴,
如果是今天发的贴则用getdate()
*/
create table A(TIME varchar(20) , NUM int)
insert into A values('200803010000CST', 10 )
insert into A values('200803011000CST', 10 )
insert into A values('200803052000CST', 10 )
insert into A values('200803053000CST', 10 )
insert into A values('200803040000CST', 10 )
insert into A values('200803031000CST', 10 )
insert into A values('200803032000CST', 10 )
insert into A values('200803043000CST', 10 )
go
delete from a where datediff(day , left(time,4) + '-' + substring(time,5,2) + '-' + substring(time,7,2) , getdate()) > 10
select * from A
drop table A
/*
TIME NUM
-------------------- -----------
200803052000CST 10
200803053000CST 10
200803040000CST 10
200803031000CST 10
200803032000CST 10
200803043000CST 10
(所影响的行数为 6 行)
*/
delete
from a
where datediff(day , left(time,4) + '-' + substring(time,5,2) + '-' + substring(time,7,2) , getdate()) > = 10