日期:2014-05-17 浏览次数:20609 次
----------------------------------------------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-11-06 16:38:11
-- Version:
-- Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64)
-- Dec 28 2012 20:23:12
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: )
--
----------------------------------------------------------------
--> 测试数据:[huang]
if object_id('[huang]') is not null drop table [huang]
go
create table [huang]([id] int,[tname] varchar(7),[toption] varchar(11))
insert [huang]
select 1,'选择题1','A,B,C,B,C,D' union all
select 2,'选择题2','A,B,C,B,C,D'
--------------开始查询--------------------------
select
id,
a.[tname],
SUBSTRING([toption],number,CHARINDEX(',',[toption]+',',number)-number) as [toption]
from
[huang] a,master..spt_values
where
number >=1 and number<=len([toption])
and type='p'
and substring(','+[toption],number,1)=','
----------------结果----------------------------
/*
id tname toption
----------- ------- -----------
1 选择题1 A
1 选择题1 B
1 选择题1 C
1 选择题1 B
1 选择题1 C
1 选择题1 D
2 选择题2 A
2 选择题2 B
2 选择题2 C
2 选择题2 B
2 选择题2