Issue when combining fields

T

Tara

I have combined 3 fields (firstname, lastname, business) in a query to come
up with a concatenated field called custname. The only problem is when one
of the fields has no data it puts a space in where the data would have been
inserted into the string. For instance, if neither firstname nor lastname
have data, and business does, I end up with something like (space) Scott's.
The problem with this is that if I want to sort alphabetically, anything with
a space before it comes up first. Is there a way to fix this?

Thanks!
 
M

Michel Walsh

Trim(FirstName & " " & LastName )

If FirstName is null, or a strings of blanks, Trim will end by eliminating
them. So:


Trim(firstName & " " & LastName) & ( ", " + business )


could do (assuming business could be null, then the + will remove the ", "
pre-concatenated to it, since + propagates the null )




Hoping it may help,
Vanderghast, Access MVP
 
K

Klatuu

You can check for Null values before concatenating the values:

SELECT IIf(firstname Is Null,"",firstname & " ") & IIf(lastname Is
Null,"",lastname & " ") & IIf(business Is Null,"", business) AS custname
 
T

Tara

Worked like a charm! Thanks!

Klatuu said:
You can check for Null values before concatenating the values:

SELECT IIf(firstname Is Null,"",firstname & " ") & IIf(lastname Is
Null,"",lastname & " ") & IIf(business Is Null,"", business) AS custname
 

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

Top