基于第7个分隔符

时间:2016-10-13 15:37:50

标签: sql sql-server-2008

显示列值

(下面是临时表中的列值,这里的值是动态变化的)

45 | 00055 | 9/30/2016 | Vodafone | Randy Singh | Newyork | Test Msg | TBL101 | PC | 1.00 | COMP101 | CS | 1.00.............. etc

需要根据第7个PIPE划分,即在测试消息之后

输出应为

字符串1

45 | 00055 | 9/30/2016 | Vodafone | Randy Singh | Newyork | Test Msg

和(作为第二个字符串)

String 2

TBL101 | PC | 1.00 | COMP101 | CS | 1.00......... etc

功能

CREATE FUNCTION dbo.SUBSTRING_INDEX
(
   @str NVARCHAR(4000),
   @delim NVARCHAR(1),
   @count INT
)
RETURNS NVARCHAR(4000)
WITH SCHEMABINDING
BEGIN
   DECLARE @XmlSourceString XML;
   SET @XmlSourceString = (SELECT N'<root><row>' + REPLACE( (SELECT @str AS '*' FOR XML PATH('')) , @delim, N'</row><row>' ) + N'</row></root>');
RETURN STUFF
(
    ((
        SELECT  @delim + x.XmlCol.value(N'(text())[1]', N'NVARCHAR(4000)') AS '*'
        FROM    @XmlSourceString.nodes(N'(root/row)[position() <= sql:variable("@count")]') x(XmlCol)
        FOR XML PATH(N''), TYPE
    ).value(N'.', N'NVARCHAR(4000)')), 
    1, 1, N''
);
END

GO

DECLARE @EmpId NVARCHAR(1000)
select @EmpId = temp from OMSOrderTemp


SELECT dbo.SUBSTRING_INDEX(@EmpId, N'|', 7) AS Result;e

此处在结果中仅显示string1且仅显示第一行。

2 个答案:

答案 0 :(得分:1)

花时间和你一起快乐解决方案,我用自己的逻辑修改你的功能你可以尝试这个,这是表值函数,即这个函数将返回表

CREATE FUNCTION dbo.SUBSTRING_INDEX
(
  @str NVARCHAR(4000),
  @delim NVARCHAR(1),
  @count INT
)RETURNS @rtnTable TABLE 
(
   FirstString  NVARCHAR(2000),
   SecondString NVARCHAR(2000)
)
AS
BEGIN
    DECLARE @cnt INT=1;
    DECLARE @subStringPoint INT = 0
    WHILE @cnt <=@count
    BEGIN 
            SET @subStringPoint=CHARINDEX(@delim,@str,@subStringPoint)+1
            SET @cnt=@cnt+1
    END

    INSERT INTO @rtnTable
    SELECT SUBSTRING(@str,0,@subStringPoint-1) ,SUBSTRING(@str,@subStringPoint+1,LEN(@str)) 
    RETURN
END 

调用此功能

DECLARE @s varchar(MAX)='45 | 00055 | 9/30/2016 | Vodafone | Randy Singh | Newyork | Test Msg | TBL101 | PC | 1.00 | COMP101 | CS | 1.00'
SELECT * FROM dbo.SUBSTRING_INDEX (@s,'|',7)

这将提供两列输出

45 | 00055 | 9/30/2016 | Vodafone | Randy Singh | Newyork | Test Msg    TBL101 | PC | 1.00 | COMP101 | CS | 1.00

答案 1 :(得分:1)

终于找到了与@JaydipJ几乎相同的解决方案。我想以不同的方式实现,但以下应该使用While循环:

DECLARE @str VARCHAR(1000), 
        @str1 VARCHAR(1000), 
        @str2 VARCHAR(1000), 
        @pos INT, 
        @counter INT 

SET @str = '45 | 00055 | 9/30/2016 | Vodafone | Randy Singh | Newyork | Test Msg | TBL101 | PC | 1.00 | COMP101 | CS | 1.00.............. etc' 

SET @counter = 0 
SET @pos = 0 

WHILE @counter < 7 
BEGIN   
SET @pos = CHARINDEX('|', @str, @pos + 1) ---- Gets the position of delimiter '|'
SET @counter = @counter + 1 ---- Increments the counter on the given counter value
END 

SET @str1 = SUBSTRING(@str, 1, @pos) ---- Splits the string on the 7th position of delimiter '|'
SET @str2 = SUBSTRING(@str, @pos + 1, LEN(@str) - @pos) ---- Splits the rest of the string

Print 'str1='+ @str1 
Print 'str2='+ @str2 

SELECT @str1 AS String1, @str2 AS String2 

While循环用于迭代字符串并获取Delimiter位置,它会分割字符串。