Link to home
Start Free TrialLog in
Avatar of cheryl9063
cheryl9063Flag for United States of America

asked on

strip out commas

My query below will test to see if there are 3 commas in the address and if there are 3 commas in the address than @location is used to run a proc I have.... However if there are only 2 commas or 1 comma or 4 commas etc. I want to strip them out all together from the variable called @location.... @location could have the following examples in it..

5222 Michaux Rd, Greensboro, NC, 27410
or
5222 Michaux Rd Greensboro, NC, 27410
or
5222 Michaux Rd Greensboro NC, 27410
or
5222 Michaux Rd Greensboro NC 27410

The only valid one is 5222 Michaux Rd, Greensboro, NC, 27410...

I have functions that will strip out each piece of the address and stuff them into @city, @State and @zip.. Then I remove them from @location so I have each piece of the address.. Then I put them back together again using below:

set @blocation = replace(@blocation, @city, '')
set @blocation = replace(@blocation, @Zip_Parsed, '')
set @blocation = replace(@blocation, @State, '')
set @blocation = rtrim(@blocation)+','+rtrim(@City) +','+rtrim(@State) +','+rtrim(@Zip_Parsed)


What I want to do is if there are not exactly 3 commas in @location remove all commas from @location or if I have to remove commas from @city, @state, and @zip.. Somehow there are commas still being passed...How do I get the commas out if there are not exactly 3?







Declare @count int = LEN(@Location) - LEN(REPLACE(@Location, ',', ''))
		IF @Count = 3
		Begin
		INSERT @AddressValidate exec usp_MelissaData_ValidateAddress @Location

Open in new window

SOLUTION
Avatar of dwe761
dwe761
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
ASKER CERTIFIED SOLUTION
Avatar of Lowfatspread
Lowfatspread
Flag of United Kingdom of Great Britain and Northern Ireland 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