SQL语句 怎么获取第一个逗号到第二个逗号之间或者第二个逗号到第三个逗号之间的字符串

 

--获取第一段 abcd

SELECT LEFT('abcd,1234,ABCD,efg',CHARINDEX(',','abcd,1234,ABCD,efg')-1) 

 --获取最后一段 efg

SELECTREVERSE(LEFT(REVERSE('abcd,1234,ABCD,efg'),CHARINDEX(',',REVERSE('abcd,1234,ABCD,efg'))-1))

--获取第二段 1234

SELECT left(stuff('abcd,1234,ABCD,efg',1,charindex(',','abcd,1234,ABCD,efg'),''),charindex(',',stuff('abcd,1234,ABCD,efg',1,charindex(',','abcd,1234,ABCD,efg'),''))-1)

--获取第三段 ABCD

SELECT  REVERSE(LEFT(REVERSE(LEFT(stuff('abcd,1234,ABCD,efg',1,charindex(',','abcd,1234,ABCD,efg'),''),

charindex(',',stuff('abcd,1234,ABCD,efg',1,charindex(',','abcd,1234,ABCD,efg'),''))+

LEN(REVERSE(LEFT(REVERSE('abcd,1234,ABCD,efg'),CHARINDEX(',',REVERSE('abcd,1234,ABCD,efg'))))))),CHARINDEX(',','abcd,1234,ABCD,efg')-1)) 

posted @ 2021-12-15 13:49  丶Ronnie  阅读(3073)  评论(0)    收藏  举报