Solved

Query to show text in string enclosed in brackets eg Nevada (USA) -> USA

Posted on 2011-09-15
13
351 Views
Last Modified: 2012-05-12
My table has a list like this:

REGION
Nevada (USA)
Ontario (CAN)
Bretagne (FR)
Cornwall (UK)

I would like a query that shows results like this:
Region      Country
Nevade     USA
Ontario     CAN
Bretagne   FR
Cornwall    UK

I've experimented with various combinations of Left, Right, Mid, InStr, InStrRev, etc but just can't get the syntax right




0
Comment
Question by:Ludique
  • 7
  • 4
  • 2
13 Comments
 
LVL 75
ID: 36547293
Here you go:

SELECT Left(Replace(Replace([RgnCtry],"(",""),")",""),InStr(1,Replace(Replace([RgnCtry],"(",""),")","")," ")-1) AS CountryX, Mid(Replace(Replace([RgnCtry],"(",""),")",""),InStr(1,Replace(Replace([RgnCtry],"(",""),")","")," ")+1) AS RegionX
FROM tblRgn;


mx
0
 
LVL 75
ID: 36547295
CountryX      RegionX
Ontario      CAN
Nevada      USA
Bretagne      FR
Cornwall      UK

mx
0
 

Author Comment

by:Ludique
ID: 36547318
Apart from swapping RegionX and CountryX thus:

SELECT Left(Replace(Replace([Region],"(",""),")",""),InStr(1,Replace(Replace([Region],"(",""),")","")," ")-1) AS RegionX, Mid(Replace(Replace([Region],"(",""),")",""),InStr(1,Replace(Replace([Region],"(",""),")","")," ")+1) AS CountryX
FROM Regions;

Brilliant! Thank you very much.
0
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
LVL 75
ID: 36547322
You are welcome

Yes, I had to alias those because I used those names in a table I created.

mx
0
 

Author Comment

by:Ludique
ID: 36547329
Sorry, I spoke too soon

I just found out that if the Region has spaces it all goes wrong

e.g

New Mexico (USA)  comes out  
New                Mexico (USA)  instead of
New Mexico    USA
0
 
LVL 75
ID: 36547369
Right ...
0
 
LVL 75
ID: 36547379
How many variations are there?

mx
0
 
LVL 75
ID: 36547404
Standby ...

mx
0
 

Author Comment

by:Ludique
ID: 36547414
Well lots I suppose.  A region name could be made up of several words.  

Wouldn't it be easier to cut it up on the parentheses rather than the spaces?
0
 
LVL 75

Accepted Solution

by:
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform) earned 500 total points
ID: 36547415
Revised:

SELECT Left([RgnCtry],InStr(1,[RgnCtry],"(")-1) AS CountryX, Replace(Mid([RgnCtry],InStr(1,[RgnCtry],"(")+1),")","") AS RegionX
FROM tblRgn;


CountryX      RegionX
Ontario       CAN
Nevada       USA
Bretagne       FR
Cornwall       UK
New Mexico       USA

mx
0
 

Author Comment

by:Ludique
ID: 36547428
That's it!

Lovely.  Looks tidy too :)
0
 
LVL 19

Expert Comment

by:Bhavesh Shah
ID: 36547430
Hi,

Check out this.

SELECT	SUBSTRING(RgnCtry, 1, CHARINDEX('(',RgnCtry)-1)Country,
		SUBSTRING(RgnCtry, CHARINDEX('(',RgnCtry)+1, (CHARINDEX(')',RgnCtry)-CHARINDEX('(',RgnCtry)-1))Region

Open in new window


Sample Code

DECLARE @Input VarChar(50)

SET @Input = 'Nevada (USA)'
select CHARINDEX(')',@Input)-1

SELECT	SUBSTRING(@Input, 1, CHARINDEX('(',@Input)-1)Country,
		SUBSTRING(@Input, CHARINDEX('(',@Input)+1, (CHARINDEX(')',@Input)-CHARINDEX('(',@Input)-1))
		
		
		

 

Open in new window



- Bhavesh
0
 
LVL 19

Expert Comment

by:Bhavesh Shah
ID: 36547434
Hi,

Sorry guys....

I provide solution for SQL Server....

Pls ignore... :-)
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

749 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