日期:2014-05-17 浏览次数:20493 次
----------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-05-28 17:42:16
-- Version:
-- Microsoft SQL Server 2008 R2 (SP1) - 10.50.2500.0 (X64)
-- Jun 17 2011 00:54:03
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.1 <X64> (Build 7601: Service Pack 1, v.721)
--
----------------------------
--> 测试数据:[a]
if object_id('[a]') is not null drop table [a]
go
create table [a]([id] int,[refName] varchar(20))
insert [a]
select 1,'zhangsan,lisi,wangwu'
--------------开始查询--------------------------
SELECT id ,
SUBSTRING([refName], number, CHARINDEX(',', [refName] + ',', number) - number) AS [refName]
FROM [a] a ,
master..spt_values
WHERE number >= 1
AND number < LEN([refName])
AND type = 'p'
AND SUBSTRING(',' + [refName], number, 1) = ','
----------------结果----------------------------
/*
id refName
----------- --------------------
1 zhangsan
1 lisi
1 wangwu
*/