且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用于拆分连接字符串的函数

更新时间:2023-02-03 08:59:57

尝试这篇文章 [ ^ ]






创建以下功能



Hi,

Create below function

CREATE FUNCTION [dbo].[String_Tokenizer]
(
	@RowData nvarchar(max),
	@SplitOn nvarchar(5)
)  
RETURNS @RtnValue table 
(
	Data nvarchar(100)
) 
AS  
BEGIN 
	Declare @Cnt int
	Set @Cnt = 1
 
	While (Charindex(@SplitOn,@RowData)>0)
	Begin
		Insert Into @RtnValue (data)
		Select 
			Data = ltrim(rtrim(Substring(@RowData,1,Charindex(@SplitOn,@RowData)-1)))
 
		Set @RowData = Substring(@RowData,Charindex(@SplitOn,@RowData)+1,len(@RowData))
		Set @Cnt = @Cnt + 1
	End
	
	Insert Into @RtnValue (data)
	Select Data = ltrim(rtrim(@RowData))
 
	Return
END







上述函数将返回一个临时表。


示例代码:






The above function will return a temporary table.

Sample code:

select * from [dbo].[String_Tokenizer]('Business Solutions + Business Solutions in Bangalore + Business Solutions in Mysore','+')





输出:



Output:

Data

Business Solutions
Business Solutions in Bangalore
Business Solutions in Mysore


如果您有这方面的知识,并且有可能这样做,我建议您使用您可能需要的CLR用户定义函数创建一个.net库(请参阅: http://msdn.microsoft.com/en-us/library/ w2kae45k(v = vs.80).aspx [ ^ ])。在.net中,你有更多的可能来解决这些简单的任务。

这是一个例子,用来制作表值clr udf: http://msdn.microsoft.com/en-us/library/ms131103.aspx [ ^ ]
If you have the knowledge, and the possibility to do that, I suggest, you make a .net library with CLR User Defined Functions you might need (see: http://msdn.microsoft.com/en-us/library/w2kae45k(v=vs.80).aspx[^]). In .net, you have so much more possibilities to resolve such simple tasks.
Here is an example, ho to make table-valued clr udf: http://msdn.microsoft.com/en-us/library/ms131103.aspx[^]