Home >Database >Mysql Tutorial >sqlserver拆分字符串函数 及应用

sqlserver拆分字符串函数 及应用

WBOY
WBOYOriginal
2016-06-07 14:50:501096browse

USE [dataChange]GO/****** Object: UserDefinedFunction [dbo].[f_splitSTR] Script Date: 05/09/2016 15:04:21 span style=color:#ff0000;自己修改数据库名,直接执行就可以了/span******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOALTER FUNCTION [d

USE [dataChange]
GO
/****** Object:  UserDefinedFunction [dbo].[f_splitSTR]    Script Date: 05/09/2016 15:04:21 <span style="color:#ff0000;">自己修改数据库名,直接执行就可以了</span>******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[f_splitSTR](
	@s   varchar(8000),   --待分拆的字符串
	@split varchar(10)     --数据分隔符
)RETURNS @re TABLE(
		col varchar(max))
AS
BEGIN
	DECLARE 
		@splitlen int

	-- 取分隔符的长度, 在分隔符后面加一个字符是为了避免分隔符以空格结束时, 取不到正确的长度
	SET @splitlen = LEN(@split + 'a') - 2
	-- 如果待分拆的字符串中存在数据分隔符, 则循环取出每个数据项
	WHILE CHARINDEX(@split, @s)>0
	BEGIN
		-- 取第一个数据分隔符前的数据项
		INSERT @re VALUES(LEFT(@s, CHARINDEX(@split, @s) - 1))
		
		-- 将已经取出的第一个数据项和数据分隔符从待分拆的字符串中去掉
		SET @s = STUFF(@s, 1, CHARINDEX(@split, @s) + @splitlen, '')
	END
	
	-- 保存最后一个数据项(最后一个数据项后面没有数据分隔符, 故在前面的循环中不会被处理)
	INSERT @re VALUES(@s)
	RETURN   
  end

应用sql:实现数据分割 如数据:

B41J-002/145%B41J-002/15%B41J-003/36%B41J-000/00%B43K-001/00%B41J-002/045%B41J-002/055%B41J-002/21%B43K-008/00%B81B-007/04%B43K-029/00%B43K-027/00%B43K-029/093  

取每一组%数据中的前一位组合起来用%分割 得到数据为

B%B%B%B%B%B%B%B%B%B%B%B%B

update [<span style="font-family: Arial, Helvetica, sans-serif;">dataChange</span><span style="font-family: Arial, Helvetica, sans-serif;">].[dbo].[Patent_Info]</span>
  set IPC_ALL1 = temp.IPC1
			from (
				 select PUB_NO,IPC1 = STUFF(
								(select '%'+ltrim(IPC1) 
								from 
									(select c.PUB_NO, SUBSTRING(COL,0,2) as IPC1
									from(
										Select B.*,A.IPC_ALL,A.PUB_NO FROM [<span style="font-family: Arial, Helvetica, sans-serif;">dataChange</span><span style="font-family: Arial, Helvetica, sans-serif;">].[dbo].[Patent_Info] A cross apply [</span><span style="font-family: Arial, Helvetica, sans-serif;">dataChange</span><span style="font-family: Arial, Helvetica, sans-serif;">].[dbo].f_splitSTR(IPC_ALL,'%')  B</span>
										)c 
								)tt
								where PUB_NO= t.PUB_NO for XML path('')
							),1,1,'')
					from 
					(select c.PUB_NO, SUBSTRING(COL,0,2) as IPC1
						from(
							Select B.*,A.IPC_ALL,A.PUB_NO FROM [<span style="font-family: Arial, Helvetica, sans-serif;">dataChange</span><span style="font-family: Arial, Helvetica, sans-serif;">].[dbo].[Patent_Info] A cross apply [</span><span style="font-family: Arial, Helvetica, sans-serif;">dataChange</span><span style="font-family: Arial, Helvetica, sans-serif;">].[dbo].f_splitSTR(IPC_ALL,'%')B)c</span><span style="font-family: Arial, Helvetica, sans-serif;">
</span>							)t							
					group by PUB_NO
			)temp
	where Patent_Info.PUB_NO = temp.PUB_NO

PUB_NO为数据表唯一字段标识

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn