Include only first part of a field in a query

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,

I have a field called Given Names and it includes both the first and the
middle name of a customer. I would like to break the two names into
individual fields. In other words I want to show just the first name in a
field and just the middle name in a second field. How do I do this in a
query? Thanks.

Janet
 
Assuming that the first and middle names are separated byt a single space,
you can do this by locating that space in the string and then taking the
portions to either side of it, like so:
SELECT NameTable.Name, Left([name],InStr([name]," ")-1) AS Part1, Right([name]
,Len([name])-InStr([name]," ")) AS Part2
FROM NameTable;

Note that this doesn't work if the names are separated by something other
than a space, and if they're separated by more than one space you'll get
spaces in front of Part2. It also doesn't handle multiple spaces in the name
very well, but maybe it will do for your purposes...
 
Actually, I've just remembered that you can get rid of the extra spaces using
TRIM:
SELECT NameTable.Name, Left([name],InStr([name]," ")-1) AS Part1, Trim(Right(
[name],Len([name])-InStr([name]," "))) AS Part2
FROM NameTable;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top