Solved

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

Posted on 2011-09-15
13
350 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

792 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