0

我正在尝试构建一个查询,该查询可以将组合的产品描述、颜色和尺寸拆分为各自的值。我有一张桌子,里面全是产品描述、颜色和尺寸。一些产品描述包含颜色和尺寸,每个都用特定的字符串分隔。一些颜色和尺寸包含在它们自己的列中。很多时候,描述和颜色/大小列都包含颜色/大小值。结合颜色和尺寸的常见产品描述如下所示: ProductDescription..-..Color--.--Size 其中 Color 由“..-..”分隔,Size 由“--.-- 分隔” ”。有时颜色和/或大小不存在,查询没有分隔符来引用,但我仍然希望它拆分描述/颜色或描述/大小,

描述和尺寸分离得很好,但我在颜色方面遇到了问题。我收到以下错误:

Invalid length parameter passed to the LEFT or SUBSTRING function.

任何帮助将非常感激!

这是我到目前为止不起作用的内容:

Select
    Ps.ID
    ,Case
        When Ps.ColorStart <= 5 And Ps.SizeStart <= 5 Then Ps.Description
        When Ps.ColorStart <= 5 And Ps.SizeStart > 5 Then Left(Ps.Description, Ps.SizeStart - 6)
        When Ps.ColorStart > 5 Then Left(Ps.Description, Ps.ColorStart - 6)
        Else Ps.Description
    End As DescriptionWithoutColorAndSize

    ,Case
        When Ps.PColor Is Not Null And Ps.PColor <> '' Then Ps.PColor
        When Ps.ColorStart <= 5 Or Ps.Description Is Null Or Ps.Description = '' Then ''
        When Ps.SizeStart <= 5 And Ps.ColorStart > 5 Then SUBSTRING(Ps.Description, Ps.ColorStart, 299)
        When Ps.SizeStart > 5 And Ps.ColorStart > 5 Then SUBSTRING(Ps.Description, Ps.ColorStart, Ps.ColorEndIfSizeExists - Ps.ColorStart + 1)
            --The prior line is what fails
        Else ''
    End As Color


    ,Case
        When Ps.PSize Is Not Null And Ps.PSize <> '' Then Ps.PSize
        When Ps.SizeStart <= 5 Then ''
        Else SUBSTRING(Ps.Description, Ps.SizeStart, 299)
    End As Size

From
    (
    Select
        P.ID
        ,P.Description
        ,P.Color As PColor
        ,P.Size As PSize
        ,CHARINDEX('..-..',P.Description,0) + 5 As ColorStart
        ,CHARINDEX('--.--',P.Description,0) -1 As ColorEndIfSizeExists
        ,Len(P.Description) As ColorEndIfSizeDoesNotExist
        ,CHARINDEX('--.--',P.Description,0) + 5 As SizeStart

    From
        MYProductsTable P
    ) Ps
4

1 回答 1

0

所有问题都与以下案例陈述有关:

,Case
    When Ps.PColor Is Not Null And Ps.PColor <> '' Or Ps.ColorEndIfSizeExists - Ps.ColorStart + 1 < 0 Then Ps.PColor
    When Ps.ColorStart <=5 Or Ps.Description Is Null Or Ps.Description = '' Then ''
    When Ps.SizeStart <=5 And Ps.ColorStart > 5 Then SUBSTRING(Ps.Description, Ps.ColorStart, 299)
    When Ps.SizeStart > 5 And Ps.ColorStart > 5 Then SUBSTRING(Ps.Description, Ps.ColorStart, Ps.ColorEndIfSizeExists - Ps.ColorStart + 1)

    Else ''
End As Color

我将“<=5”的所有实例更改为“<6”,并在语句中添加了一个新的When,如下所示:

    ,Case
        When Ps.ColorEndIfSizeExists - Ps.ColorStart + 1 < 0 Then SUBSTRING(Ps.Description, Ps.ColorStart, 299)
        When Ps.PColor Is Not Null And Ps.PColor <> '' Then Ps.PColor
        When Ps.ColorStart <6 Or Ps.Description Is Null Or Ps.Description = '' Then ''
        When Ps.SizeStart <6 And Ps.ColorStart > 5 Then SUBSTRING(Ps.Description, Ps.ColorStart, 299)
        When Ps.SizeStart > 5 And Ps.ColorStart > 5 Then SUBSTRING(Ps.Description, Ps.ColorStart, Ps.ColorEndIfSizeExists - Ps.ColorStart + 1)
        Else ''
    End As Color

这解决了问题。我不知道为什么......如果有人这样做,请随时解释!感谢所有的投入。

于 2015-06-04T00:07:53.707 回答