Concatenation Not Working (1 Viewer)

lhooker

Registered User.
Local time
Today, 14:36
Joined
Dec 30, 2005
Messages
399
Why is no 'Actual_Name' given when no 'Middle_Name' is provided in a select query ? Below is the syntax of the statement.

Actual_Name: [First_Name]+" "+[Middle_Name]+" "+[Last_Name]

:banghead:
 

BigHappyDaddy

Coding Monkey Wanna-Be
Local time
Today, 11:36
Joined
Aug 22, 2012
Messages
205
What is your complete SQL statement?
What is your actual results?
What is your expected results?
 

isladogs

MVP / VIP
Local time
Today, 19:36
Joined
Jan 14, 2017
Messages
18,209
Try this instead:

Code:
Actual_Name: [First_Name] & IIf(Nz([Middle_Name],"")<>""," " & [Middle_Name] & " "," ") & [Last_Name]

Notice I've replaced + separator with &
Also modified the handling of spaces around the middle name so you don't get two spaces if no middle name exists
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 02:36
Joined
May 7, 2009
Messages
19,229
Because it propagate the null when you use +, instead:

Actual_Name: ([First_Name] + " ") & ( [Middle_Name]) + " ") & [Last_Name]
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 14:36
Joined
Feb 19, 2002
Messages
43,230
The & is the standard VBA concatenation operator and Access also uses it in queries. The + operator does double duty as both an arithmetic and a concatenation operator but it works differently from the & in how it handles nulls. You can use this difference to your advantage when you are concatenating multiple strings such as First middle and last names. If you simply use:

First & " " & Middle & " " & Last

You will end up with two spaces between first and last whenever a person has no middle name. Using the expression shown by arnel which uses both + and & you can avoid introducing the extra spaces. Examine this statement closely to understand how it works. This is much more complicated to accomplish in other languages where + is the only option. You have to use a much more complicated statement with the IIf() to avoid the extra spaces.
 
Last edited:

lhooker

Registered User.
Local time
Today, 14:36
Joined
Dec 30, 2005
Messages
399
BigHappyDaddy/Ridders/Arnelgp/Pat Hartman,

Thanks to all ! ! ! I used Arnelgp's solution.
 

Users who are viewing this thread

Top Bottom