2015-06-03 69 views
0

我试图构建一个查询,可以将组合的产品描述,颜色和大小拆分为各自的值。我有一张充满产品说明,颜色和尺寸的表格。某些产品说明包含每个用特定字符串分隔的颜色和大小。一些颜色和尺寸包含在他们自己的栏目中。很多时候,说明和颜色/尺寸列都包含颜色/尺寸值。结合颜色和大小的常见产品说明如下所示: ProductDescription ..- ..颜色 - .--颜色由“..- ..”分隔的尺寸,尺寸由“--.- - ”。有时颜色和/或大小不存在,查询无法引用分隔符,但我仍然希望它将描述/颜色或描述/大小分开,或者仅在返回描述和空白值以用于颜色/大小时既不存在...SQL Server查询 - 根据2个不同的公共分隔符将字符串拆分为3个部分

描述和大小分裂就好了,但我有颜色的麻烦。我收到以下错误:

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 
+0

当你在select语句中只有SUBSTRING(Ps.Description,Ps.ColorStart,Ps.ColorEndIfSizeExists - Ps.ColorStart + 1)时,你的查询返回什么? – Sushil

+0

想知道这一点,因为在运行calc之前没有检查ColorEndIfSizeExists的长度。 – CargoMeister

+0

我得到相同的错误。说明也可以为空或空白,我试图在该案例的第二行中说明这一点,但这可能会导致错误......我只是不能说出来,因为它错误了。 –

回答

0

所有的都涉及到以下case语句的问题:

,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“,并添加了新的时间表如下:

,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 

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

相关问题