在SQL Server 2005中分割字串 [英] Splitting strings in sql server 2005

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

问题描述

我想拆分包含"1,2," .....的字符串,该字符串包含在ProgrammId列中.我在存储过程中将programmid声明为varchar,但在表结构中将其声明为int ... .所以请帮我,我对查询感到震惊...


I Want to split the string which contains ''1,2,''....which contains in ProgrammId column.. I declared programmid as varchar in stored procedure but in table structure it is declared as int.... So help me i am struck with the query...


while charindex(',',@PROGRAMID) <> 0
DECLARE @var1 nvarchar(30)
begin

select @var1=substring(@PROGRAMID,1,(charindex(',',@PROGRAMID)-1))

INSERT INTO dbo.Table_ProgramJoinDetails(ProgramId ,UserId,programjoindate,programenddate,Slotid)VALUES  (@var1 ,@USERID,@slot,@slot1,@Slotid)
   --select ProgramId from Table_ProgramJoinDetails where ProgramId=CAST(@var1 AS int)

SET @PROGRAMID = substring(@PROGRAMID,charindex(',',@PROGRAMID)+1,len(@PROGRAMID))
end

推荐答案

您可以使用以下线程中的任何拆分功能:

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

您将在一个表中获取数据,可以将其与主表连接以获得所需的输出.
You can use any of the split functions from the following thread:

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

You will get data in a table which you can join with your main table to get the required output.


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

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