日期:2014-05-18  浏览次数:20546 次

求一条高级SQL替换语句(内详)
表 a
date txt_desc
2012-03-09 up[have: +11],code:and,[case]
2012-03-10 up[have: +5],code:or,[then]
2012-03-11 down[have: +20],code:and,[then]
2012-03-12 up[have: +3],code:or,[or]

希望可以实现 根据date 最后一条的 txt_desc 的内容插入一条新的数据,效果如下
2012-05-06 up[have: +57],code:or,[or]

简单的说, 利用 a 中最后一条数据的txt_desc 的 格式 加入新的数据,只改变[have: +N]这个的值
希望能用1条sql语句实现, 各位大大帮帮忙,交流交流

------解决方案--------------------
SQL code

--> 测试数据:[a1]
if object_id('[a1]') is not null drop table [a1]
create table [a1]([date] datetime,[txt_desc] varchar(100))
insert [a1]
select '2012-03-09','up[have:+11],code:and,[case]' union all
select '2012-03-10','up[have:+5],code:or,[then]' union all
select '2012-03-11','down[have:+20],code:and,[then]' union all
select '2012-03-12','up[have:+3],code:or,[or]'



insert [a1]
select top 1 [date],replace([txt_desc],left(substring([txt_desc]+' ',
patindex( '%[0-9]% ',[txt_desc]+' '),
len([txt_desc]+' ')),patindex( '%[^0-9]% ',substring([txt_desc]+' ',
patindex( '%[0-9]% ',[txt_desc]+' '),len([txt_desc]+' ')))-1),'57') 
from a1 order by [date] desc

select * from a1

/*
date    txt_desc
2012-03-09 00:00:00.000    up[have:+11],code:and,[case]
2012-03-10 00:00:00.000    up[have:+5],code:or,[then]
2012-03-11 00:00:00.000    down[have:+20],code:and,[then]
2012-03-12 00:00:00.000    up[have:+3],code:or,[or]
2012-03-12 00:00:00.000    up[have:+57],code:or,[or]
*/

更正一下