Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

SQL Code to remove numbers and parans from string

Posted on 2009-04-22
6
Medium Priority
?
701 Views
Last Modified: 2013-12-25
I have a column that contains names and a zipe code, which is inside of parentheses.

For example:

John Smith (01223)
Michael Jones (06106)
etc

How do I strip out the numerics in the parens, and leave just the names?  My attempts to use the SUBSTRING function aren't working.

Thanks
0
Comment
Question by:rpcahoon
  • 3
  • 2
6 Comments
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 24207591
If the (#####) always comes at the end...


SELECT NameAndZip, LEFT(NameAndZip, CHARINDEX('(', NameAndZip) - 1) AS JustTheName
FROM SomeTable
0
 
LVL 2

Expert Comment

by:neo00000110
ID: 24207637


declare @str varchar(20)

set @str = 'blah (0222)'


select replace(@str, substring(@str,charindex('(',@str)-1,charindex(')',@str)),'')

0
 
LVL 2

Expert Comment

by:neo00000110
ID: 24207670
sorry forgot to say you can change @str with your column name and it shoudl remove anything within ()
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:rpcahoon
ID: 24207765
OK, sorry but there is a wrinke.

Some values may look something like:

Michael Jones (COnnecticut) (06106)
0
 
LVL 93

Accepted Solution

by:
Patrick Matthews earned 2000 total points
ID: 24207893
SELECT NameAndZip, REVERSE(SUBSTRING(REVERSE(NameAndZip), CHARINDEX(' ', REVERSE(NameAndZip)) + 1, LEN(NameAndZip))) AS NameNoZip
FROM SomeTable
0
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 24208011
BTW, if the original column is set up as char and not varchar...

SELECT NameAndZip, REVERSE(SUBSTRING(REVERSE(RTRIM(NameAndZip)), CHARINDEX(' ', REVERSE(RTRIM(NameAndZip))) + 1, LEN(NameAndZip))) AS NameNoZip
FROM SomeTable
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

581 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