SQL 服务器查询 - 根据 2 个不同的公共分隔符将字符串拆分为 3 个部分
SQL Server Query - Split a string into 3 parts based on 2 different common delimiters
我正在尝试构建一个查询,该查询可以将组合的产品描述、颜色和尺寸拆分为各自的值。我有一个 table 的产品描述、颜色和尺寸。一些产品描述包含颜色和尺寸,每个都用特定字符串分隔。一些颜色和尺寸包含在它们自己的列中。很多时候,描述和 color/size 列都包含 color/size 值。结合颜色和尺寸的常见产品描述如下所示:
ProductDescription..-..Color--.-- Size,其中 Color 由“..-..”分隔,Size 由“--.--”分隔。有时颜色 and/or 大小不存在并且没有分隔符供查询参考,但我仍然希望它拆分 description/color 或 description/size,或者只是 return 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
所有问题都与以下案例陈述有关:
,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
这就解决了问题。我不知道为什么...如果有人这样做,请随时解释!
感谢所有的输入。
我正在尝试构建一个查询,该查询可以将组合的产品描述、颜色和尺寸拆分为各自的值。我有一个 table 的产品描述、颜色和尺寸。一些产品描述包含颜色和尺寸,每个都用特定字符串分隔。一些颜色和尺寸包含在它们自己的列中。很多时候,描述和 color/size 列都包含 color/size 值。结合颜色和尺寸的常见产品描述如下所示: ProductDescription..-..Color--.-- Size,其中 Color 由“..-..”分隔,Size 由“--.--”分隔。有时颜色 and/or 大小不存在并且没有分隔符供查询参考,但我仍然希望它拆分 description/color 或 description/size,或者只是 return 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
所有问题都与以下案例陈述有关:
,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
这就解决了问题。我不知道为什么...如果有人这样做,请随时解释! 感谢所有的输入。