Link to home
Start Free TrialLog in
Avatar of MadIce
MadIce

asked on

T-SQL setup String

I pass a string to my stored procedure that can be setup in different formats. I trying to use Trim, replace, CharIndex and such to format it in a way I can use. Here is what the string usually looks like when passing:
'Widget1, Widget2, Widget3                          
Widget4, Widget5, Widget6                
Widget7, Widget'

It might be hard to tell from this but say after Widget3 there are a bunch of spaces. This is what I need the string to look like:
'Widget1, Widget2, Widget3, Widget4, Widget5, Widget6,  Widget7, Widget8'

so a coma after each widget or remove all comma and have one single space. either would work
Avatar of Ephraim Wangoya
Ephraim Wangoya
Flag of United States of America image


use REPLACE

  SET @Parameter = REPLACE(@Parameter, ' ', '')
Avatar of MadIce
MadIce

ASKER

That removes the spaces but how do I add the missing comas?
ASKER CERTIFIED SOLUTION
Avatar of Ephraim Wangoya
Ephraim Wangoya
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MadIce

ASKER

I have this so far
SET @Parameters = LTRIM(RTRIM(@Parameters))+ ','
set @Parameters = replace(@Parameters, ' ', '')
set @Parameters = replace(@Parameters, CHAR(10), ',')

Problem is it puts the coma on the next row as oppose to next to the "Widget". like this
'Widget1, Widget2, Widget3
,Widget4,

Avatar of MadIce

ASKER

Didn't see your last comment. That works. Thanks
SET @Parameters = Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(LTRIM(RTRIM(@Parameters)), ',', ' '), char(13), ' '), char(10), ' '), '    ', '  '), '    ', '  '), '   ', '  '), '  ', ' '), ' ', ', ')