Solved

strip out commas

Posted on 2011-03-09
2
371 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:cheryl9063
2 Comments
 
LVL 10

Assisted Solution

by:dwe761
dwe761 earned 50 total points
ID: 35085367
I had no problems using the example below.  Change the @Location and run it each way.  Maybe you've got a different problem?
declare @Location varchar(100),
	@count int

set @Location = 'aaaaaaaaa,aaaaaa,aaaaaaaaaa'
--set @Location = 'aaaaaaaaa,aaaaaa,aaaaa,aaaaa'
--set @Location = 'aaaaaaaaa,aaaaaa,aaaaa,aaaaa,bbbbb'

set @count = LEN(@Location) - LEN(REPLACE(@Location,',',''))
IF @Count = 3 BEGIN
	PRINT 'Run my proc'
END
ELSE BEGIN
	SET @Location = REPLACE(@Location,',','')
	PRINT 'New Location: ' + @Location
END

Open in new window

0
 
LVL 50

Accepted Solution

by:
Lowfatspread earned 450 total points
ID: 35088908
do you mean something like this?
IF len(@location+'x') - Len(replace(@location+'x',',','') = 3 
		Begin
		    INSERT @AddressValidate
		      exec usp_MelissaData_ValidateAddress @Location
		    ....  
		end
		else 
		begin
		    set @location=REPLACE(@location,',',' ')
		end

Open in new window

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question