主题:sql server 2000 语法问题
junlin
[专家分:0] 发布于 2005-10-27 21:59:00
就是下面这样
id | text
-----------
01 | A:B:C:D
先创上面这样简单的资料库
答案是下面这样
---
A
---
B
---
C
---
D
---
要下什么语法才能分割这样呢
就是上面先创一个简单的资料库~~
要下""什么sql语法""
才可以将上面的资料库里的a:b:c:d分割
将":"砍掉并且显示出下面那个答案呢
如果不是":"而是另外个种中继符号如-->a@@b@@c@@d
也能将""@@""符号砍掉显示出答案~~~
谢谢~~麻烦了~~
回复列表 (共3个回复)
沙发
cwb1128 [专家分:3120] 发布于 2005-10-28 09:28:00
我做了个简单的例子,你看看吧,思想是这样。
declare @string varchar(30)
declare @flagindex int
declare @flag varchar(10)
declare @singlestring varchar(10)
set @string = 'A:B:C:D'+':'
set @flag = ':'
set @flagindex = charindex(@flag,@string)
create table #cwb(id int identity(1,1),string varchar(10) null primary key(id))
while(@flagindex>0)
begin
set @singlestring=substring(@string,1,@flagindex-1)
insert into #cwb(string)values(@singlestring)
set @string= substring(@string,@flagindex+1,len(@string)-@flagindex)
set @flagindex=charindex(@flag,@string)
end
select string from #cwb
drop table #cwb
板凳
junlin [专家分:0] 发布于 2005-10-29 21:25:00
题目大样是这样--创一个资料库
http://tw.pg.photos.yahoo.com/ph/r0534/detail?.dir=/cbb1&.dnm=b3dd.jpg&.src=ph
答案要显示出这个
http://tw.pg.photos.yahoo.com/ph/r0534/detail?.dir=/cbb1&.dnm=aa77.jpg&.src=ph
希望有人可以帮我写出完整的语法~~感激不尽
3 楼
junlin [专家分:0] 发布于 2005-10-30 07:59:00
иρ畍倒氮°°
临璶ぐ或┪эぐ或粂猭
程ê氮㎡°硂磅︽穦弧㏑ЧΘ°ぃ穦瞷氮
CREATE Procedure sp_Split(@sText varchar(8000), @sDelim varchar(20) = ' ' )
AS
BEGIN
DECLARE @idx smallint,
@value varchar(8000),
@bcontinue bit,
@iStrike smallint,
@iDelimlength tinyint
IF @sDelim = 'Space'
BEGIN
SET @sDelim = ' '
END
SET @idx = 0
SET @sText = LTrim(RTrim(@sText))
SET @iDelimlength = DATALENGTH(@sDelim)
SET @bcontinue = 1
Delete From tempSplit
IF NOT ((@iDelimlength = 0) or (@sDelim = 'Empty'))
BEGIN
WHILE @bcontinue = 1
BEGIN
--If you can find the delimiter in the text, retrieve the first element and
--insert it with its index into the return table.
IF CHARINDEX(@sDelim, @sText)>0
BEGIN
--SUBSTRING ( expression , start , length )
SET @value = SUBSTRING(@sText,1, CHARINDEX(@sDelim,@sText)-1)
BEGIN
INSERT INTO tempSplit (idx, value)
VALUES (@idx, @value)
END
--Trim the element and its delimiter from the front of the string.
--Increment the index and loop.
SET @iStrike = DATALENGTH(@value) + @iDelimlength
SET @idx = @idx + 1
SET @sText = LTrim(Right(@sText,DATALENGTH(@sText) - @iStrike))
END
ELSE
BEGIN
--If you canˇt find the delimiter in the text, @sText is the last value in
--@retArray.
SET @value = @sText
BEGIN
INSERT INTO tempSplit (idx, value)
VALUES (@idx, @value)
END
--Exit the WHILE loop.
SET @bcontinue = 0
END
END
END
ELSE
BEGIN
WHILE @bcontinue=1
BEGIN
--If the delimiter is an empty string, check for remaining text
--instead of a delimiter. Insert the first character into the
--retArray table. Trim the character from the front of the string.
--Increment the index and loop.
IF DATALENGTH(@sText)>1
BEGIN
SET @value = SUBSTRING(@sText,1,1)
BEGIN
INSERT INTO tempSplit (idx, value)
VALUES (@idx, @value)
END
SET @idx = @idx+1
SET @sText = SUBSTRING(@sText,2,DATALENGTH(@sText)-1)
END
ELSE
BEGIN
--One character remains.
--Insert the character, and exit the WHILE loop.
INSERT INTO tempSplit (idx, value)
VALUES (@idx, @sText)
SET @bcontinue = 0
END
END
END
RETURN
END
我来回复