---------------------------------------------------------------- -- Author :TravyLee(走自己的路,让狗去叫吧!) -- Date :2014-02-13 10:54:10 -- Version: -- Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86) -- Jul 9 2008 14:43:34 -- Copyright (c) 1988-2008 Microsoft Corporation -- Developer Edition on Windows NT 6.1 <X86> (Build 7601: Service Pack 1) -- ---------------------------------------------------------------- --> 测试数据:#TAB1 if object_id('tempdb.dbo.#TAB1') is not null drop table #TAB1 go create table #TAB1([A710] varchar(4),[A711] varchar(42)) insert #TAB1 select '张三','扣1;缺勤;11天;扣2;旷工;2天;扣3;考核不合格;' union all select '张五','扣1;旷工;2天;扣2;事假;3天;' go select A.A710,substring(A.A711, B.number, charindex(',', A.A711 + ',', B.number) - B.number) as [A711] into #tb from (select [A710],REPLACE([A711],'天;',',') as [A711] from #TAB1) A, master..spt_values B where substring(',' + A.A711, B.number, 1) = ',' and type='p' and number >=1
select A710,LEFT(A711,CHARINDEX(';',A711)-1) as 类别, ltrim(case when RIGHT(A711,LEN(A711)-CHARINDEX(';',A711))<>'' then RIGHT(A711,LEN(A711)-CHARINDEX(';',A711))+'天' else '' end) as 天数 from( select A710,right(A711,LEN(A711)-CHARINDEX(';',A711)) as A711 from #tb where A711<>'' )t