Solved

strip out commas

Posted on 2011-03-09
2
369 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
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
In this article I will describe the Detach & Attach 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.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

743 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now