0

在 SELECT 之后,我得到了这样的价值:

Basic:           3 Years/36,000 Miles    Drivetrain:           3 Years/36,000 Miles    Corrosion:           6 Years/100,000 Miles    Roadside Assistance:           3 Years/36,000 Miles

我希望它分成两列,如:

    Text          Value

    Basic        3 Years/36,000 Miles
    Drivetrain   3 Years/36,000 Miles

etc

等等

笔记 :

Basic:-----------3 Years/36,000 Miles - 中间有 11 个空格

3 Years/36,000 Miles----Drivetrain: -中间有 4 个空格

4

1 回答 1

0

这适用于您的样本,但您必须根据真实数据对其进行测试才能确定。

WITH CTE_StringSplit AS
(
    SELECT CAST(NULL AS NVARCHAR(MAX)) AS [Text],  CAST(NULL AS NVARCHAR(MAX)) AS [Value] ,  CAST([Text] AS NVARCHAR(MAX)) AS RemainingText 
    FROM Info
    UNION ALL
    SELECT 
      SUBSTRING(RemainingText,0,CHARINDEX('           ',RemainingText))
      ,LTRIM(SUBSTRING(RemainingText,CHARINDEX('           ',RemainingText),CASE WHEN CHARINDEX('    ',LTRIM(SUBSTRING(RemainingText,CHARINDEX('           ',RemainingText),DATALENGTH(RemainingText)))) > 0 THEN CHARINDEX('    ',LTRIM(SUBSTRING(RemainingText,CHARINDEX('           ',RemainingText),DATALENGTH(RemainingText)))) ELSE DATALENGTH(RemainingText) END + 11))
      ,LTRIM(SUBSTRING(RemainingText,11 + CHARINDEX('           ',RemainingText) + CASE WHEN CHARINDEX('    ',LTRIM(SUBSTRING(RemainingText,CHARINDEX('           ',RemainingText),DATALENGTH(RemainingText)))) > 0 THEN CHARINDEX('    ',LTRIM(SUBSTRING(RemainingText,CHARINDEX('           ',RemainingText),DATALENGTH(RemainingText)))) ELSE DATALENGTH(RemainingText) END,DATALENGTH(RemainingText)))
     FROM CTE_StringSplit
     WHERE LEN(RemainingText)>0
)
SELECT [Text],[Value]
FROM CTE_StringSplit
WHERE [Text] IS NOT NULL

如果需要更正,请使用其他示例更新问题。

于 2013-10-21T10:41:37.510 回答