求助SQL

bluesky1980 2008-03-13 05:10:27
表A有两个字段TIME(VARCHAR类型)NUM(INT类型)
TIME NUM
200803010000CST 10
200803011000CST 10
200803052000CST 10
200803053000CST 10
200803040000CST 10
200803031000CST 10
200803032000CST 10
200803043000CST 10

TIME的前八位代表插入该条数据时的日期
想写一个SQL语句,删除距今10天以外的数据,即删除掉
200803010000CST 10
200803011000CST 10
这两条数据
...全文
76 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
leisure_cool 2008-03-14
  • 打赏
  • 举报
回复

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()
*/

山之魂2 2008-03-13
  • 打赏
  • 举报
回复
不用那么麻烦的函数啊

delete from a where datediff(day , left(time,8) , getdate()) > 10

就可以了
山之魂2 2008-03-13
  • 打赏
  • 举报
回复
不用那么麻烦的函数啊

delete from a where datediff(day , left(time,8) , getdate()) > 10

就可以了
山之魂2 2008-03-13
  • 打赏
  • 举报
回复
不用那么麻烦的函数啊

delete from a where datediff(day , left(time,8) , getdate()) > 10

就可以了
山之魂2 2008-03-13
  • 打赏
  • 举报
回复
不用那么麻烦的函数啊

delete from a where datediff(day , left(time,8) , getdate()) > 10

就可以了
dawugui 2008-03-13
  • 打赏
  • 举报
回复
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 行)
*/
-狙击手- 2008-03-13
  • 打赏
  • 举报
回复
delete 
from a
where datediff(day , left(time,4) + '-' + substring(time,5,2) + '-' + substring(time,7,2) , getdate()) > = 10
dawugui 2008-03-13
  • 打赏
  • 举报
回复
delete from a where datediff(day , left(time,4) + '-' + substring(time,5,2) + '-' + substring(time,7,2) , getdate()) >= 10

34,593

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧