在sql 2008中拆分一个字符串 [英] Split a string in sql 2008

查看:84
本文介绍了在sql 2008中拆分一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从数据库字段值中拆分字符串....

解决方案

请查看此链接....







http://blog.logiclabz.com/sql-server/split-function-in-sql-server-to-break-comma- separate-strings-into-table.aspx [ ^ ]



http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=50648 [ ^ ]


您可以创建分割字符串的功能。请尝试以下函数:

  CREATE  功能 dbo.Split 

@ RowData nvarchar 2000 ),
@ SplitOn nvarchar 5

RETURNS @ RtnValue table

Id int identity 1 1 ),
数据 nvarchar 100

AS
BEGIN
声明 @C nt int
设置 @ Cnt = 1

(Charindex( @ SplitOn @ RowData )> 0)
开始
插入 进入 @ RtnValue (数据)
选择
Data = ltrim(rtrim(Substring( @ RowData) 1 ,Charindex( @ SplitOn @RowData ) - 1)))

设置 @ RowData = Substring( @ RowData ,Charindex( @ SplitOn , @ RowData )+ 1,len( @ RowData ))
设置 @ Cnt = @ Cnt + 1
结束

插入 进入 @ RtnValue (数据)
选择 Data = ltrim(rtrim( @ RowData ))

返回
END





原始来源:这里 [ ^ ]。





- 阿米特


How can i split a string from database field value....

解决方案

Please review this links....



http://blog.logiclabz.com/sql-server/split-function-in-sql-server-to-break-comma-separated-strings-into-table.aspx[^]

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=50648[^]


You can create a function to split the strings. Try the following function:

CREATE FUNCTION dbo.Split
(
	@RowData nvarchar(2000),
	@SplitOn nvarchar(5)
)  
RETURNS @RtnValue table 
(
	Id int identity(1,1),
	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



Original Source : Here[^].


--Amit


这篇关于在sql 2008中拆分一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆