日期:2014-05-18  浏览次数:20549 次

问一个简单的SQL一句
如何将字符串   "让梦冬眠-艾雨.vox,艾雨,2 "     引号里面的数据   的 ", "所在位置判断出来


------解决方案--------------------
declare @a varchar(100)
set @a= '让梦冬眠-艾雨.vox,艾雨,2 '
select top 100 id=identity(int,1,1) into # from syscolumns

select id pos
from #
where substring(@a,id,1)= ', '
drop table #

--result
/*
pos
-----------
12
15

(所影响的行数为 2 行)
*/
------解决方案--------------------
declare @s as varchar(50)
set @s = '让梦冬眠-艾雨.vox,艾雨,2 '

select charindex( ', ' , @s) 第一个, charindex( ', ' , @s , charindex( ', ' , @s) + 1) 第二个
select substring(@s , charindex( ', ' , @s) + 1 , charindex( ', ' , @s , charindex( ', ' , @s) + 1) - charindex( ', ' , @s) - 1) 两逗号之间的内容

/*
第一个 第二个
----------- -----------
12 15

(所影响的行数为 1 行)

两逗号之间的内容
--------------------------------------------------
艾雨

(所影响的行数为 1 行)
*/