Here's one method using PATINDEX
with LEFT
and RIGHT
.
declare @var varchar(64)= ',,,,,,,,asdf,dsf,sdfsd,asdf,,,,,,,,'
select
left(right(@var,len(@var) - patindex('%[^,]%',@var) + 1)
,len(right(@var,len(@var) - patindex('%[^,]%',@var) + 1)) - patindex('%[^,]%',reverse(right(@var,len(@var) - patindex('%[^,]%',@var) + 1))) + 1)
Just change @var
to your column name.
This code strips the leading commas by searching for the first value that isn't a comma, via patindex('%[^,]%',@var)
and takes everything to the RIGHT
of this character. Then, we do the same thing using LEFT
to remove the trailing commas.
select
Special = left(right(Special,len(Special) - patindex('%[^,]%',Special) + 1),len(right(Special,len(Special) - patindex('%[^,]%',Special) + 1)) - patindex('%[^,]%',reverse(right(Special,len(Special) - patindex('%[^,]%',Special) + 1))) + 1)
,[Application] = left(right([Application],len([Application]) - patindex('%[^,]%',[Application]) + 1),len(right([Application],len([Application]) - patindex('%[^,]%',[Application]) + 1)) - patindex('%[^,]%',reverse(right([Application],len([Application]) - patindex('%[^,]%',[Application]) + 1))) + 1)
,[ProductType] = left(right([ProductType],len([ProductType]) - patindex('%[^,]%',[ProductType]) + 1),len(right([ProductType],len([ProductType]) - patindex('%[^,]%',[ProductType]) + 1)) - patindex('%[^,]%',reverse(right([ProductType],len([ProductType]) - patindex('%[^,]%',[ProductType]) + 1))) + 1)
FROM PRODUCTFINDER