Wrox Programmer Forums
|
Access Discussion of Microsoft Access database design and programming. See also the forums for Access ASP and Access VBA.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 22nd, 2007, 10:45 AM
Authorized User
 
Join Date: Mar 2007
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default Extract part of String

Hi,
I have a table with a column for region names. Region Names are in 2 formats basically - "NAME-BU*RM" OR "NAME*RM".
I want to extract just "Name" from this string.
Can anyone advise what the query/SQL statement would look like?

 
Old August 22nd, 2007, 11:36 PM
Authorized User
 
Join Date: Jul 2007
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to vksingh24
Default

You can use the following sql query

SELECT LEFT(region,(INSTR(region,"*")-1))FROM MYTABLE;

__________________
Vikash Kumar Singh
 
Old August 23rd, 2007, 02:36 PM
Authorized User
 
Join Date: Mar 2007
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Vikash,

Thanks for your response however its giving me an error that "Instr" is not a recognized function. Anyhow I wrote a query using parse function as below and it works fine.
declare @test table
( regionName varchar(40)
)
insert into @test
select RegionName from [Guidant Field Force Table]
select * from @test
select regionName,
     isnull( parsename( replace(regionName,'-','.'),2),
             parsename( replace(regionName,'*','.'),2))
     as firstPart
from @test

The problem I'm having is that there is another column named "ID" in the table that should be linked to the new "firstpart". So can you suggest any way to link the original column "ID" to this query result?

So basically original "Guidant field force table" is:

ID RegionName
289 SIERRA NEVADA-CRM*CORDERO
482 CARDIAC NW REGION* TREBBE
US20 SOUTH BAY-CRM*HENKHAUS
UF40 INLAND EMPIRE-CRM*LIPKOWSI

And my result should look like:

ID RegionName
289 SIERRA NEVADA
482 CARDIAC NW REGION
US20 SOUTH BAY
UF40 INLAND EMPIRE

So I'm able to achieve the correct result for column "RegionName" using my query but I don't know how to get the associated ID no. as well. Please advise
 
Old August 23rd, 2007, 11:10 PM
Authorized User
 
Join Date: Jul 2007
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to vksingh24
Default

Instr function is applicable in MS-Access.

I have tested this query in MS-Access. but i guess your are using T-SQL

__________________
Vikash Kumar Singh
 
Old August 23rd, 2007, 11:17 PM
Authorized User
 
Join Date: Jul 2007
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to vksingh24
Default

CHARINDEX

__________________
Vikash Kumar Singh
 
Old August 23rd, 2007, 11:18 PM
Authorized User
 
Join Date: Jul 2007
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to vksingh24
Default

Use this sql query if you are using T-SQL

SELECT ID, LEFT(region,(CHARINDEX(region,"*")-1))FROM MYTABLE;

__________________
Vikash Kumar Singh
 
Old August 24th, 2007, 10:04 AM
Authorized User
 
Join Date: Mar 2007
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Vikash,

You're right. I'm using T-SQL.
I used the query:
SELECT ID,LEFT(regionname,(CHARINDEX('*', regionname)-1))
FROM MyTable;

The output I get is:

ID RegionName
289 SIERRA NEVADA-CRM
482 CARDIAC NW REGION
US20 SOUTH BAY-CRM
UF40 INLAND EMPIRE-CRM

So this is not the exact output.

The query I mentioned previously using parsename does give me the correct output. All i need to modify in that query is add column "ID". You have any thoughts on modifying tht to add anthr column?





Similar Threads
Thread Thread Starter Forum Replies Last Post
Extract first numeric string blkskullwork Javascript 1 November 6th, 2006 02:37 AM
how to extract from a string fraijo SQL Server 2000 3 October 25th, 2006 09:55 PM
Getting Part of a String needelp Access VBA 4 September 19th, 2006 06:22 AM
Extract part of field from inconsistent data Roly Reefer Access VBA 4 March 31st, 2005 09:25 AM
A query to extract part of a string RayL Access 3 March 10th, 2004 09:42 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.