将带分隔符的字段拆分为同一记录中的单独列



我需要分割一个以管道分隔的字段('Y|N|Y|Y')。

我找到了这里列出的递归函数T-SQL:与字符串连接相反——如何将字符串分割成多个记录

,但是它会在新记录中创建值

row   field
 1      Y
 2      N
 3      Y
 4      N

我需要变换'Y|N|Y|Y'

field
'Y|N|Y|Y' 

field1  |  field2  | field3  | field4
    y         N         Y        N
谁能给我指个正确的方向?如果我说值的数量是固定的(它可能是8个值在一个字段中分隔),会不会更容易?

Update:一个可能的字段值可能是这样的(注意空值):"Y | 10 N | 1 | | | Y '

**EDIT: **我的仍然适用于示例字符串

这是另一个答案,因为你知道宽度是固定长度:

DECLARE @myString AS nvarchar(20) = 'Y|10|N|1||Y'
;WITH cte
AS
(
SELECT
    KeyCol = @@IDENTITY,
    CONVERT(XML,'<i>' + REPLACE(@myString, '|', '</i><i>') + '</i>') AS delimited_str
)
SELECT 
    [1] AS Field1,
    [2] AS Field2,
    [3] AS Field3,
    [4] AS Field4,
    [5] AS Field5,
    [6] AS Field6,
    [7] AS Field7,
    [8] AS Field8
FROM(
    SELECT 
        KeyCol,
        ROW_NUMBER() OVER (partition by KeyCol order by KeyCol)as col_nbr,
        x.i.value('.', 'VARCHAR(50)') AS delimited_VAL
    FROM cte
    CROSS APPLY delimited_str.nodes('//i') AS x(i)
    ) as PivotedDataTable
PIVOT
(MAX(delimited_VAL) FOR col_nbr IN
([1], [2], [3], [4], [5], [6], [7], [8])
) AS PivotTable;

编辑:当我遇到类似的问题时,我知道我以前在某个地方看到过这个:http://social.msdn.microsoft.com/forums/en - us/transactsql/thread/381e4164 f1e0 - 4 - b54 - 828 f - 2795 d2cdcb3e/

如果字段的宽度也总是固定的,即1个字符(Y/N),那么就像使用substring函数获得所有单个字段值一样简单:

;with Data as (
    select 'Y|N|Y|Y' as choices union
    select 'Y|Y|Y|Y' as choices union
    select 'Y|N|N|Y' as choices union
    select 'Y|N|N|N' as choices union
    select 'N|N|Y|N' as choices union
    select 'Y|Y|N|Y' as choices
)
select
    substring(choices, 1, 1) as field1,
    substring(choices, 3, 1) as field2,
    substring(choices, 5, 1) as field3,
    substring(choices, 7, 1) as field4
from
    Data
输出:

field1  field2  field3  field4
N       N       Y       N
Y       N       N       N
Y       N       N       Y
Y       N       Y       Y
Y       Y       N       Y
Y       Y       Y       Y

如果您不能保证字段的宽度是相同的,您可以使用charindex和字段索引的辅助表来生成您正在寻找的输出。随着字段数量的增加,这将变得非常冗长,但是如果字段数量是固定的,则只需要编写一次:

;with Data as (
    select 1 as id, 'Y|N|Y|Y' as choices union
    select 2,'Y|Y|Y|Y' as choices union
    select 3,'Y|No|N|Y' as choices union
    select 4,'Yes|N|N|N' as choices union
    select 5,'N|N|Yes|No' as choices union
    select 6,'Y|Y|N|Yes' as choices
), Fields as (
    select
        id,
        charindex('|', choices) as field1end,
        charindex('|', choices, charindex('|', choices) + 1) as field2end,
        charindex('|', choices, charindex('|', choices, charindex('|', choices) + 1) + 1) as field3end,
        len(choices) + 1 as field4end
    from
        Data
)
select
    substring(choices, 1, field1end - 1) as field1,
    substring(choices, field1end + 1, field2end - field1end - 1) as field2,
    substring(choices, field2end + 1, field3end - field2end - 1) as field3,
    substring(choices, field3end + 1, field4end - field3end - 1) as field4
from
    Data D
inner join
    Fields F on D.id = F.id
输出:

field1  field2  field3  field4
Y       N       Y       Y
Y       Y       Y       Y
Y       No      N       Y
Yes     N       N       N
N       N       Yes     No
Y       Y       N       Yes

最新更新