[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now


strip out commas

Posted on 2011-03-09
Medium Priority
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
5222 Michaux Rd Greensboro, NC, 27410
5222 Michaux Rd Greensboro NC, 27410
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
		INSERT @AddressValidate exec usp_MelissaData_ValidateAddress @Location

Open in new window

Question by:cheryl9063
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 10

Assisted Solution

dwe761 earned 200 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'
	SET @Location = REPLACE(@Location,',','')
	PRINT 'New Location: ' + @Location

Open in new window

LVL 50

Accepted Solution

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

Open in new window


Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

656 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