Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# Access - Truncating a Field

Posted on 2009-04-03
Medium Priority
227 Views
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
Question by:tahirih
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 4
• 4

LVL 28

Expert Comment

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;
``````
0

Author Comment

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

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

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
``````
0

Author Comment

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

TextReport earned 2000 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
``````
0

Author Comment

ID: 24064935
I was placing the INTO in the improper location. Thanks
0

LVL 28

Expert Comment

ID: 24064943
No problem and best wishes, Andrew
0

## Featured Post

Question has a verified solution.

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

As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
###### Suggested Courses
Course of the Month11 days, 9 hours left to enroll