SQL Server Query - Split a string into 3 parts based on 2 different common delimiters -
i'm trying build query can split combined product descriptions, colors, , sizes individual values. have table full of product descriptions, colors, , sizes. of product descriptions contain color , size each delimited specific string. of colors , sizes contained in own columns. many times, both description , color/size columns contain both color/size values. common product description combined color , size this: productdescription..-..color--.--size color delimited "..-.." , size delimited "--.--". color and/or size not exist , there no delimiter query refer to, still want split description/color or description/size, or return description , blank values color/size when neither exists...
description , size split fine, i'm having trouble color. following error:
invalid length parameter passed left or substring function.
any appreciated!
here's have far isn't working:
select ps.id ,case when ps.colorstart <= 5 , ps.sizestart <= 5 ps.description when ps.colorstart <= 5 , ps.sizestart > 5 left(ps.description, ps.sizestart - 6) when ps.colorstart > 5 left(ps.description, ps.colorstart - 6) else ps.description end descriptionwithoutcolorandsize ,case when ps.pcolor not null , ps.pcolor <> '' ps.pcolor when ps.colorstart <= 5 or ps.description null or ps.description = '' '' when ps.sizestart <= 5 , ps.colorstart > 5 substring(ps.description, ps.colorstart, 299) when ps.sizestart > 5 , ps.colorstart > 5 substring(ps.description, ps.colorstart, ps.colorendifsizeexists - ps.colorstart + 1) --the prior line fails else '' end color ,case when ps.psize not null , ps.psize <> '' ps.psize when ps.sizestart <= 5 '' else substring(ps.description, ps.sizestart, 299) end size ( select p.id ,p.description ,p.color pcolor ,p.size psize ,charindex('..-..',p.description,0) + 5 colorstart ,charindex('--.--',p.description,0) -1 colorendifsizeexists ,len(p.description) colorendifsizedoesnotexist ,charindex('--.--',p.description,0) + 5 sizestart myproductstable p ) ps
all of problems related following case statement:
,case when ps.pcolor not null , ps.pcolor <> '' or ps.colorendifsizeexists - ps.colorstart + 1 < 0 ps.pcolor when ps.colorstart <=5 or ps.description null or ps.description = '' '' when ps.sizestart <=5 , ps.colorstart > 5 substring(ps.description, ps.colorstart, 299) when ps.sizestart > 5 , ps.colorstart > 5 substring(ps.description, ps.colorstart, ps.colorendifsizeexists - ps.colorstart + 1) else '' end color
i changed instances of "<=5" "<6", , added new when statment follows:
,case when ps.colorendifsizeexists - ps.colorstart + 1 < 0 substring(ps.description, ps.colorstart, 299) when ps.pcolor not null , ps.pcolor <> '' ps.pcolor when ps.colorstart <6 or ps.description null or ps.description = '' '' when ps.sizestart <6 , ps.colorstart > 5 substring(ps.description, ps.colorstart, 299) when ps.sizestart > 5 , ps.colorstart > 5 substring(ps.description, ps.colorstart, ps.colorendifsizeexists - ps.colorstart + 1) else '' end color
and fixed problem. don't know why... if does, please feel free explain! input.
Comments
Post a Comment