Solved

Combining table fields within a query

Posted on 2013-06-22
5
318 Views
Last Modified: 2013-08-08
I have an access 2007 application that has a person table, with last_name, first_name attributes.  I would like to build a query where I can combine last_name and first_name, and represent the combined fields as one field "name".  I have tried the following:

name:last_name & ", " & first_name

but I get syntax errors from SQL.

Is there a way to do this?

Thanks in advance for your help.

Tom
0
Comment
Question by:tpigielski
5 Comments
 
LVL 12

Expert Comment

by:pdebaets
ID: 39268771
If you are in SQL view try

last_name & ", " & first_name as [name]

however, I would recommend NOT using a reserved word as a field name. Try "fullname" instead of "name".
0
 

Author Comment

by:tpigielski
ID: 39268825
Poor choice of identifiers on my part.  "name" is not what I am using, but something else.  My query is much more complicated than this (multiple tables).  What I really need to know is whether or not I can do this kind of an operation from a query?

Thanks..Tom
0
 
LVL 12

Accepted Solution

by:
pdebaets earned 250 total points
ID: 39268840
If you are in SQL view try

last_name & ", " & first_name as FullName

In query design view try

FullName: last_name & ", " & first_name

If the syntax error persists, please post the SQL here so we can see what the problem is.
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 39268961
Problem could be that some fields are empty:

full_name: IIf(last_name+first_name Is Null, Nz(last_name & first_name, "Unknown"),last_name & ", " & first_name)

/gustav
0
 
LVL 24

Assisted Solution

by:Bitsqueezer
Bitsqueezer earned 250 total points
ID: 39269040
Hi,

the problem can also be sometimes the query designer. In some cases it does strange conversions to your input, so it's better to write such expressions in SQL and then switch back to the query designer and you see the result.

I would concatenate such strings on this way (in case of a full name concatenation):

SELECT Nz(last_name) & Iif(Nz(last_name)="" OR Nz(first_name) ="", "", ", ") & Nz(first_name) AS FullName FROM YourTable

Open in new window


BTW: You should not use VBA functions in cases where there are SQL pendants. The "IsNull" function should only be used in VBA, in SQL it is always "IIf(MyField IS NULL, ...".

In general I see no error with your syntax except using "name" as alias.

Cheers,

Christian
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This article will show you how to use shortcut menus in the Access run-time environment.
In this article we discuss how to recover the missing Outlook 2011 for Mac data like Emails and Contacts manually.
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
The viewer will learn how to  create a slide that will launch other presentations in Microsoft PowerPoint. In the finished slide, each item launches a new PowerPoint presentation and when each is finished it automatically comes back to this slide: …

760 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

20 Experts available now in Live!

Get 1:1 Help Now