选择与charindex问题

时间:2018-05-10 14:20:50

标签: sql sql-server sql-server-2008

我尝试使用' a'将此字符串分隔到不同的列中。作为分隔符

005031812180200000100aRemaining Inside Terminala Cassette 1(BIN1)/c:HTG 21,000a Cassette 2(BIN2)/c:HTG 23,000a Cassette 3(BIN3)/d:HTG 47,000a Cassette 4(BIN4)/d:HTG 48,000a Total : HTG 139,000aaaad

我用这段代码创建了一个对象:

CREATE FUNCTION dbo.splitstring ( @stringToSplit VARCHAR(MAX) )
RETURNS
 @returnList TABLE ([cassette_1] [nvarchar] (500),
                [cassette_2] [nvarchar] (500),
                [cassette_3] [nvarchar] (500),
                [cassette_4] [nvarchar] (500),
                [total] [nvarchar] (500)
               )
AS
BEGIN

 DECLARE @cassette_1 NVARCHAR(255)
 DECLARE @cassette_2 NVARCHAR(255)
 DECLARE @cassette_3 NVARCHAR(255)
 DECLARE @cassette_4 NVARCHAR(255)
 DECLARE @total NVARCHAR(255)
 DECLARE @pos INT

WHILE CHARINDEX('a ', @stringToSplit) > 0
 BEGIN
  SELECT @pos = CHARINDEX('a ', @stringToSplit)  
  SELECT @cassette_1 = SUBSTRING(@stringToSplit, 1, @pos-1)
  SELECT @cassette_2 = SUBSTRING(@stringToSplit, 1, @pos-1)
  SELECT @cassette_3 = SUBSTRING(@stringToSplit, 1, @pos-1)
  SELECT @cassette_4 = SUBSTRING(@stringToSplit, 1, @pos-1)
  SELECT @stringToSplit = SUBSTRING(@stringToSplit, @pos+1, LEN(@stringToSplit)-@pos)
 END

INSERT INTO @returnList (cassette_1)
SELECT @cassette_1
INSERT INTO @returnList (cassette_2)
SELECT @cassette_2
INSERT INTO @returnList (cassette_3)
SELECT @cassette_2
INSERT INTO @returnList (cassette_4)
SELECT @cassette_4
INSERT INTO @returnList (total)
SELECT @stringToSplit

 RETURN
END    

这是我调用该对象的代码:

SELECT * FROM dbo.splitstring('005031812180200000100aRemaining Inside Terminala Cassette 1(BIN1)/c:HTG 21,000a Cassette 2(BIN2)/c:HTG 23,000a Cassette 3(BIN3)/d:HTG 47,000a Cassette 4(BIN4)/d:HTG 48,000a Total : HTG 139,000aaaad')

该字符串将调用表中的字符串。

这是现在获得的结果: http://sqlfiddle.com/#!18/60eb5/2

我需要一个注册表中的所有内容(我知道插件是创建一个新的)但我似乎没有完成它,我也只是在每一列中都有最后一部分(盒式磁带) 4)

2 个答案:

答案 0 :(得分:0)

你的功能几乎就在那里...... 但不是

INSERT INTO @returnList (cassette_1)
SELECT @cassette_1
INSERT INTO @returnList (cassette_2)
SELECT @cassette_2
INSERT INTO @returnList (cassette_3)
SELECT @cassette_2
INSERT INTO @returnList (cassette_4)
SELECT @cassette_4
INSERT INTO @returnList (total)
SELECT @stringToSplit
你可以用这个

INSERT INTO @returnList 
SELECT @cassette_1,@cassette_2,@cassette_3,@cassette_4,@stringToSplit

CMIIW, 如果有这个帮助,请告诉我......

由于

答案 1 :(得分:0)

在循环内部,您将在每次迭代中设置所有@casette_n个变量。将循环展开到四组语句或根据计数器获取正确的变量并将值赋给它。我在以下代码中选择了第一种可能性。

最后INSERT可以用一个SELECT编写。所以只插入一行。

在开头似乎有一些带有名字或标题的标题。你也必须解决这个问题。我也把它添加到输出中,但是如果你不想要它,你可以丢弃它。但是在搜索第一个盒子之前必须从字符串中删除它。

CREATE FUNCTION dbo.splitstring ( @stringToSplit VARCHAR(MAX) )
RETURNS
 @returnList TABLE ([name] [nvarchar] (500),
                    [cassette_1] [nvarchar] (500),
                    [cassette_2] [nvarchar] (500),
                    [cassette_3] [nvarchar] (500),
                    [cassette_4] [nvarchar] (500),
                    [total] [nvarchar] (500)
                   )
AS
BEGIN
 DECLARE @name NVARCHAR(255)
 DECLARE @cassette_1 NVARCHAR(255)
 DECLARE @cassette_2 NVARCHAR(255)
 DECLARE @cassette_3 NVARCHAR(255)
 DECLARE @cassette_4 NVARCHAR(255)
 DECLARE @total NVARCHAR(255)
 DECLARE @pos INT

 -- getting the name
 SELECT @pos = CHARINDEX('a ', @stringToSplit)
 SELECT @name = SUBSTRING(@stringToSplit, 1, @pos-1)
 SELECT @stringToSplit = SUBSTRING(@stringToSplit, @pos+1, LEN(@stringToSplit)-@pos)

 -- getting 1st cassette
 SELECT @pos = CHARINDEX('a ', @stringToSplit)
 SELECT @cassette_1 = SUBSTRING(@stringToSplit, 1, @pos-1)
 SELECT @stringToSplit = SUBSTRING(@stringToSplit, @pos+1, LEN(@stringToSplit)-@pos)

 -- getting 2nd cassette
 SELECT @pos = CHARINDEX('a ', @stringToSplit)
 SELECT @cassette_2 = SUBSTRING(@stringToSplit, 1, @pos-1)
 SELECT @stringToSplit = SUBSTRING(@stringToSplit, @pos+1, LEN(@stringToSplit)-@pos)

 -- getting 3rd cassette
 SELECT @pos = CHARINDEX('a ', @stringToSplit)
 SELECT @cassette_3 = SUBSTRING(@stringToSplit, 1, @pos-1)
 SELECT @stringToSplit = SUBSTRING(@stringToSplit, @pos+1, LEN(@stringToSplit)-@pos)

 -- getting 4th cassette
 SELECT @pos = CHARINDEX('a ', @stringToSplit)
 SELECT @cassette_4 = SUBSTRING(@stringToSplit, 1, @pos-1)
 SELECT @stringToSplit = SUBSTRING(@stringToSplit, @pos+1, LEN(@stringToSplit)-@pos)

 -- inserting one row
 INSERT INTO @returnList (name, cassette_1, cassette_2, cassette_3, cassette_4, total)
 SELECT @name, @cassette_1, @cassette_2, @cassette_3, @cassette_4, @stringToSplit

 RETURN
END

但是只有恰好有四个磁带才能工作!如果必须处理这样的字符串,您可能需要重新考虑任意数量的磁带。

当然,如果'a '出现在数据的某个位置,就像在名称标题中那样,'It's a wonderful day',就会被搞砸。