日期:2014-05-17 浏览次数:20507 次
--> 测试数据:#tb
IF OBJECT_ID('TEMPDB.DBO.#tb') IS NOT NULL DROP TABLE #tb
GO
CREATE TABLE #tb([id] INT,[name] VARCHAR(50),[date] datetime)
INSERT #tb
SELECT 5,'张三,2012-01-01',NULL UNION ALL
SELECT 6,'a,qer',NULL UNION ALL
SELECT 7,'bkk',NULL UNION ALL
SELECT 8,'cdd',NULL UNION ALL
SELECT 9,'李四,2012-01-08',NULL UNION ALL
SELECT 10,'agr[fg0)',NULL UNION ALL
SELECT 11,'dhg',NULL UNION ALL
SELECT 12,'e3y',NULL UNION ALL
SELECT 13,'王五,2012-03-09',NULL UNION ALL
SELECT 15,'fhg',NULL UNION ALL
SELECT 16,'guy',NULL
GO
--> 测试语句:
;with cte as
(
select *,[date1]=(case when charindex( ',',[name])> 0
and isdate(stuff([name],1,charindex( ',',[name]), ''))=1
then stuff([name],1,charindex( ',',[name]), '') end )
from #tb
)
update t set [date]=(select top 1 [date1] from cte where id<=t.id order by [date1] desc)
from cte as t
select * from #tb
/*
id name date
5 张三,2012-01-01 2012-01-01 00:00:00.000
6 a,qer 2012-01-01 00:00:00.000
7 bkk 2012-01-01 00:00:00.000
8 cdd 2012-01-01 00:00:00.000
9 李四,2012-01-08 2012-01-08 00:00:00.000
10 agr[fg0) 2012-01-08 00:00:00.000
11 dhg 2012-01-08 00:00:00.000
12 e3y 2012-01-08 00:00:00.000
13 王五,2012-03-09 201