Solved

Access - Truncating a Field

Posted on 2009-04-03
8
191 Views
Last Modified: 2012-05-06
In the following example:

366540 - Doe, John

I would like to separate this on field into three fields:

1. ID (366540)
2. Last Name (Doe)
3. First Name (John)

The Dash always separates the ID from the last and first names, and the comma always separates last name from first name

Thanks
Thanks
0
Comment
Question by:tahirih
  • 4
  • 4
8 Comments
 
LVL 28

Expert Comment

by:TextReport
ID: 24064550
The code below will do it for you
Cheers, Andrew
SELECT Table4.Field1

     , Trim(Left([Field1],InStr([Field1],"-")-1)) AS F1

     , Trim(Mid([Field1],InStr([Field1],"-")+1,Len([Field1])-Len([F1])-Len([F3])-2)) AS F2

     , Trim(Right([Field1],Len([Field1])-InStrRev([Field1],","))) AS F3

FROM Table4;

Open in new window

0
 

Author Comment

by:tahirih
ID: 24064643
This worked wonderfully. I do want to SELECT all fields, and insert into a new Table C:

I was not able to get the following to work:

SELECT * FROM A INTO C
     , Trim(Left([Provider],InStr([Provider],"-")-1)) AS F1
     , Trim(Mid([Provider],InStr([Provider],"-")+1,Len([Provider])-Len([F1])-Len([F3])-2)) AS F2
     , Trim(Right([Provider],Len([Provider])-InStrRev([Provider],","))) AS F3;

Thanks
0
 

Author Comment

by:tahirih
ID: 24064665
Also, the comma remains in F2, the Last Name field. Can we please recode to remove this?

Thank you.
0
 
LVL 28

Expert Comment

by:TextReport
ID: 24064836
Slight increase in the number to take off the extra characters in F2, had to changeteh -2 to -4 to compensate for the spaces that dont go into F1 and F3
Cheers, Andrew


SELECT Table4.Field1

     , Trim(Left([Field1],InStr([Field1]," - ")-1)) AS F1

     , Trim(Mid([Field1],InStr([Field1],"-")+1,Len([Field1])-Len([F1])-Len([F3])-4)) AS F2

     , Trim(Right([Field1],Len([Field1])-InStrRev([Field1],", "))) AS F3

FROM Table4

Open in new window

0
3 Use Cases for Connected Systems

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

 

Author Comment

by:tahirih
ID: 24064847
I would also like to create a new table C based on this SQL query. This offers a Query view, not a new table.

Thank you
0
 
LVL 28

Accepted Solution

by:
TextReport earned 500 total points
ID: 24064922
OK
SELECT Table4.Field1

     , Trim(Left([Field1],InStr([Field1]," - ")-1)) AS F1

     , Trim(Mid([Field1],InStr([Field1],"-")+1,Len([Field1])-Len([F1])-Len([F3])-4)) AS F2

     , Trim(Right([Field1],Len([Field1])-InStrRev([Field1],", "))) AS F3

INTO [Table C]

FROM Table4

Open in new window

0
 

Author Comment

by:tahirih
ID: 24064935
I was placing the INTO in the improper location. Thanks
0
 
LVL 28

Expert Comment

by:TextReport
ID: 24064943
No problem and best wishes, Andrew
0

Featured Post

3 Use Cases for Connected Systems

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

Question has a verified solution.

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

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

864 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now