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
Solved

strip out commas

Posted on 2011-03-09
2
374 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

856 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