**
将某一字段的数据以特殊符号截取分列显示
**
-
例子:‘hwfh:3;fsdfh|| wuhs:0;wfuwe|| sdgsf:8;dudui’
-
需求:
-
只截取前五组数据
-
只留下str1和str2数据
-
-
方法一:
SELECT DISTINCT A.截取的字段名,
PARSENAME(REPLACE(PARSENAME(REPLACE(A.截取的字段名,'||','.'),1),':','.'),2) AS str1_ONE,
PARSENAME(REPLACE(PARSENAME(REPLACE(A.截取的字段名,'||','.'),1),';','.'),1) AS str3_ONE,
PARSENAME(REPLACE(PARSENAME(REPLACE(A.截取的字段名,'||','.'),2),':','.'),2) AS 1_TWO,
PARSENAME(REPLACE(PARSENAME(REPLACE(A.截取的字段名,'||','.'),2),';','.'),1) AS str3_TWO,
PARSENAME(REPLACE(PARSENAME(REPLACE(A.截取的字段名,'||','.'),3),':','.'),2) AS str1_THREE,
PARSENAME(REPLACE(PARSENAME(REPLACE(A.截取的字段名,'||','.'),3),';','.'),1) AS str3_THREE,
PARSENAME(REPLACE(PARSENAME(REPLACE(A.截取的字段名,'||','.'),4),':','.'),2) AS str1_FOUR,
PARSENAME(REPLACE(PARSENAME(REPLACE(A.截取的字段名,'||','.'),4),';','.'),1) AS str3_FOUR,
PARSENAME(REPLACE(PARSENAME(REPLACE(A.截取的字段名,'||','.'),5),':','.'),2) AS str1_FIVE,
PARSENAME(REPLACE(PARSENAME(REPLACE(A.截取的字段名,'||','.'),5),';','.'),1) AS str3_FIVE
FROM table A
-
方法二:封装一个通用的截取字符串的函数
-
步骤:
- 截取所需要的字符串;先进行对特殊符号‘||’的个数进行判断,如果只有一个不需要截取,直接获取该字段;如果大于一个以上则先找到你想截取字段后的特殊符号‘||’的位置。
- 对截取一次的字符串进行第二次截取;先找到需要截取的字符串的特殊符号(‘;’和‘:’)的位置,然后截取需要的字符串
- 构造函数
CREATE FUNCTION [dbo].[split_str]
(@str NVARCHAR(4000),@code VARCHAR(10),@no int, @TYPE VARCHAR(20) ) --@str截取字符串,@code截取的特殊符号, @no截取第几个(从0开始的), @TYPE第二次截取的字段的类型
RETURNS VARCHAR(200)
AS
BEGIN
DECLARE @intlen int--获取特殊符号长度
DECLARE @count int
DECLARE @indexb int--截取字符串起始位
DECLARE @indexe int--截取字符串末位
DECLARE @resultstr VARCHAR(100) --截取后的数据
DECLARE @str1 VARCHAR(50)--截取str1的数据
DECLARE @str3 VARCHAR(50)--截取str3的数据
SET @intlen=len(@code)
SET @count=0
SET @indexb=1
IF @no=0
IF charindex(@code,@str,@indexb)<>0
SET @resultstr = left(@str,charindex(@code,@str,@indexb)-1)
ELSE
SET @resultstr = @str
WHILE charindex(@code,@str,@indexb)<>0
BEGIN
SET @count=@count+1
IF @count=@no
BREAK
SET @indexb=@intlen+charindex(@code,@str,@indexb)
END
IF @count=@no
BEGIN
SET @indexe=@intlen+charindex(@code,@str,@indexb)
IF charindex(@code,@str,@indexe)<>0
SET @resultstr = substring(@str,charindex(@code,@str,@indexb)+len(@code),charindex(@code,@str,@indexe)-charindex(@code,@str,@indexb)-len(@code))
ELSE
SET @resultstr = right(@str,len(@str)-charindex(@code,@str,@indexb)-len(@code)+1)
END
IF (@TYPE = 'str1')
BEGIN
RETURN PARSENAME(REPLACE(@resultstr,':','.'),2)
END
IF (@TYPE = 'str3')
BEGIN
RETURN PARSENAME(REPLACE(@resultstr,';','.'),1)
END
RETURN ''
END
4.调用函数
SELECT A.截取的字符串,
[dbo].[split_str](A.截取的字符串, '||', 0, 'str1') AS str1_ONE,
[dbo].[split_str](A.截取的字符串, '||', 0, 'str3') AS str3_ONE,
[dbo].[split_str](A.截取的字符串, '||', 1, 'str1') AS str1_TWO,
[dbo].[split_str](A.截取的字符串, '||', 1, 'str3') AS str3_TWO,
[dbo].[split_str](A.截取的字符串, '||', 2, 'str1') AS str1_THREE,
[dbo].[split_str](A.截取的字符串, '||', 2, 'str3') AS str3_THREE,
[dbo].[split_str](A.截取的字符串, '||', 3, 'str1') AS str1_FOUR,
[dbo].[split_str](A.截取的字符串, '||', 3, 'str3') AS str3_FOUR,
[dbo].[split_str](A.截取的字符串, '||', 4, 'str1') AS str1_FIVE,
[dbo].[split_str](A.截取的字符串, '||', 4, 'str3') AS str3_FIVE
FROM 表名 A
5.结果