SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Comma Data Cleanup
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

qman
Constraint Violating Yak Guru

USA
440 Posts

Posted - 10/30/2013 :  11:37:48  Show Profile  Reply with Quote
How can I get rid of comma's if not part of a "valid" value?
For instance, I have a 'Sample' table that contains a 'State' column
The values in the column could be empty, contain states separated by a comma, or contain one or more single commas. I want to remove those, while keeping states with a comma separating them.
Thoughts...?

CURRENT
TABLE = 'Sample'
COLUMN = 'State'

State
Alabama
, 
Alaska, Arizona
California, Colorado
empty string
, 
empty string
, , 
Deleware, Georgia, Utah 

DESIRED RESULTS

State
Alabama
empty string 
Alaska, Arizona
California, Colorado
empty string 
empty string 
Deleware, Georgia, Utah 

djj55
Constraint Violating Yak Guru

USA
332 Posts

Posted - 10/30/2013 :  11:47:54  Show Profile  Reply with Quote
DELETE FROM mytable WHERE LTRIM(REPLACE(State, ',', '')) = ''


Note this may not be too fast
djj

Edited by - djj55 on 10/30/2013 11:48:17
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 10/30/2013 :  12:48:00  Show Profile  Reply with Quote
Why delete? As per OPs required output it should be just this


UPDATE Sample
SET State = ''
WHERE LEN(LTRIM(REPLACE(State,',','')))=0


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

qman
Constraint Violating Yak Guru

USA
440 Posts

Posted - 10/30/2013 :  13:37:27  Show Profile  Reply with Quote
Thanks to you both...., I ended up using the update solutions which did the trick.
Go to Top of Page

djj55
Constraint Violating Yak Guru

USA
332 Posts

Posted - 10/30/2013 :  14:59:16  Show Profile  Reply with Quote
Sorry I thought from the output that the blank lines were removed.

djj
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22756 Posts

Posted - 10/31/2013 :  02:42:10  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
quote:
Originally posted by djj55

Sorry I thought from the output that the blank lines were removed.

djj


In that case you can use WHERE clause not DELETE statement

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.22 seconds. Powered By: Snitz Forums 2000