-1

我在编写用于将数据从一列分离到多列的查询时遇到问题。

例子:

Col1
---------------------------
bank.wrong.transaction
bank.wrong.transaction.captured
business.unit.explored.wrong.way
application.failed

需要将数据放入这样的列中:

col1      col2           col3
-------------------------------------------------    
wrong     transaction    null
wrong     transaction    captured
unit      explored       wrong
failed    null           null

我根本不知道任何字符串的长度。

请帮我解决一下这个。

谢谢萨哈。。

4

2 回答 2

1

类似以下的内容可用于获取您请求的输出(不记得我从哪里抢走了 ParseString 函数 - 但我一直在使用它,效果很好)

CREATE FUNCTION dbo.fnParseString
    (
      @Section SMALLINT ,
      @Delimiter CHAR ,
      @Text VARCHAR(MAX)
    )
RETURNS VARCHAR(8000)
AS 
    BEGIN
        DECLARE @NextPos SMALLINT ,
            @LastPos SMALLINT ,
            @Found SMALLINT

        --#### Uncomment the following 2 lines to emulate PARSENAME functionality
        --IF @Section > 0 
        --    SELECT  @Text = REVERSE(@Text)

        SELECT  @NextPos = CHARINDEX(@Delimiter, @Text, 1) ,
                @LastPos = 0 ,
                @Found = 1

        WHILE @NextPos > 0
            AND ABS(@Section) <> @Found 
            SELECT  @LastPos = @NextPos ,
                    @NextPos = CHARINDEX(@Delimiter, @Text, @NextPos + 1) ,
                    @Found = @Found + 1

        RETURN  CASE
            WHEN @Found <> ABS(@Section) OR @Section = 0 THEN NULL
            --#### Uncomment the following lines to emulate PARSENAME functionality
            --WHEN @Section > 0 THEN REVERSE(SUBSTRING(@Text, @LastPos + 1, CASE WHEN @NextPos = 0 THEN DATALENGTH(@Text) - @LastPos ELSE @NextPos - @LastPos - 1 END))
            WHEN @Section > 0 THEN SUBSTRING(@Text, @LastPos + 1, CASE WHEN @NextPos = 0 THEN DATALENGTH(@Text) - @LastPos ELSE @NextPos - @LastPos - 1 END)

            ELSE SUBSTRING(@Text, @LastPos + 1, CASE WHEN @NextPos = 0 THEN DATALENGTH(@Text) - @LastPos ELSE @NextPos - @LastPos - 1 END)
        END
    END



DECLARE @TestData VARCHAR(255)
SET @TestData = 'business.unit.explored.wrong.way'

SELECT  dbo.fnParseString(2, '.', @TestData) ,
        dbo.fnParseString(3, '.', @TestData) ,
        dbo.fnParseString(4, '.', @TestData) ,
        dbo.fnParseString(5, '.', @TestData) ,
        dbo.fnParseString(6, '.', @TestData)
于 2012-05-15T20:40:36.313 回答
0

好吧,您还没有回答任何问题,但是如果我的某些假设是正确的(例如,您只关心中间三个值),我将展示如何以这种方式提取数据而无需冗长函数的开销:

DECLARE @x TABLE(col1 VARCHAR(255));

INSERT @x 
SELECT 'bank.wrong.transaction'
UNION ALL SELECT 'bank.wrong.transaction.captured'
UNION ALL SELECT 'business.unit.explored.wrong.way'
UNION ALL SELECT 'application.failed';

WITH x(col1, d) AS
( 
  SELECT col1, SUBSTRING(col1, CHARINDEX('.', col1) + 1, 255) + 
    CASE WHEN LEN(col1) - LEN(REPLACE(col1, '.', '')) BETWEEN 1 AND 3 THEN
     REPLICATE('./', 4-(LEN(col1)-LEN(REPLACE(col1, '.', '')))) ELSE '' END
    FROM @x
)
SELECT orig = col1, 
    col1 = NULLIF(PARSENAME(d, 4), '/'), 
    col2 = NULLIF(PARSENAME(d, 3), '/'),
    col3 = NULLIF(PARSENAME(d, 2), '/')
FROM x;

结果:

orig                              col1        col2         col3
--------------------------------  ----------  -----------  --------
bank.wrong.transaction            wrong       transaction  NULL
bank.wrong.transaction.captured   wrong       transaction  captured
business.unit.explored.wrong.way  unit        explored     wrong
application.failed                failed      NULL         NULL
于 2012-05-15T21:05:30.693 回答