相关疑难解决方法(0)

将一列拆分为多行

谁能告诉我如何实现这一目标?在某些情况下,我的表中的列包含逗号分隔值.如果是,我需要为这些值创建新行.

此外,作为示例,表包含1行和4列Col1 | Col2 | Col3 | Col4具有以下值A | B | C | 分别为1,2,3.因此,Col4包含字符串'1,2,3',我需要拆分逗号分隔值并将它们放在自己的行上,这样表格就会包含1行,其中1 2和3位于他们自己的行上在Col4.

sql-server

9
推荐指数
1
解决办法
2万
查看次数

SQL Server将CSV拆分为多行

我之前已经意识到这个问题,但是由于某些原因我无法让它工作.

我正在使用此SQL Team线程中的split函数(第二篇文章)和以下查询.

--This query converts the interests field from text to varchar
select
    cp.id
    ,cast(cp.interests as varchar(100)) as interests
into #client_profile_temp
from
    client_profile cp

--This query is supposed to split the csv ("Golf","food") into multiple rows            
select
    cpt.id
    ,split.data
from
    #client_profile_temp cpt
    cross apply dbo.split(
    cpt.interests, ',') as split  <--Error is on this line
Run Code Online (Sandbox Code Playgroud)

但是我得到了一个

Incorrect syntax near '.'
Run Code Online (Sandbox Code Playgroud)

我在上面标出的错误.

最后,我想要

ID              INTERESTS
000CT00002UA    "Golf","food"
Run Code Online (Sandbox Code Playgroud)

成为

ID              INTERESTS
000CT00002UA    "Golf"
000CT00002UA    "food"
Run Code Online (Sandbox Code Playgroud)

我正在使用SQL Server 2008,并根据StackOverflow问题得出答案.我对SQL很陌生,所以任何其他智慧的话也会受到赞赏.

sql-server

4
推荐指数
2
解决办法
1万
查看次数

标签 统计

sql-server ×2