日期:2014-05-17 浏览次数:20669 次
----------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-01-31 15:22:48
-- Version:
-- Microsoft SQL Server 2008 R2 (SP1) - 10.50.2500.0 (Intel X86)
-- Jun 17 2011 00:57:23
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition on Windows NT 6.1 <X86> (Build 7601: Service Pack 1)
--
----------------------------
--> 测试数据:[huang]
if object_id('[huang]') is not null drop table [huang]
go
create table [huang]([id] int,[str] varchar(9))
insert [huang]
select 1,'A;B' union all
select 2,'C;F;SS' union all
select 3,'张三;小三'
--------------开始查询--------------------------
select
id,
SUBSTRING([str],number,CHARINDEX(';',[str]+';',number)-number) as [str]
from
[huang] a,master..spt_values
where
number >=1 and number<len([str])
and type='p'
and substring(';'+[str],number,1)=';'
----------------结果----------------------------
/*
id str
----------- ---------
1 A
2 C
2 F
2 SS
3 张三
3 小三
(6 行受影响)
*/
SELECT TOP 8000 id = IDENTITY(int, 1, 1) INTO # FROM syscolumns a, syscolumns b
SELECT A.id, SUBSTRING(A.str, B.id, CHARINDEX(';', A.str + ';', B.id) - B.id) FROM tb A, # B WHERE SUBSTRING(';' + A.str, B.id, 1) = ';'
DROP TABLE #
select
id,
SUBSTRING([str],number,CHARINDEX(';',[str]+';',number+1)-1) as [str]
from
[huang] a,master..spt_values
where
number >=1
--and number<=len([str])
and type='p'
and substring(';'+[str],number,1)=';'