Tidy Text Concatenation (1 Viewer)

Dmak

Registered User.
Local time
Today, 22:43
Joined
May 10, 2012
Messages
65
Hello there.

I have a query with 3 text fields; Title, FirstName, Lastname.

I've concatenated them with Fullname: [Title] & " " & [Firstname] & " " & [LastName] so it produced eg Mr John Smith.

Slight problem is there isn't always text in all fields and I get redundant spaces.

Not a major problem but I'd like my db to look smart throughout.

If anyone can let me know how I achieve this it would be greatly appreciated.

Many thanks,

Dan
 

pr2-eugin

Super Moderator
Local time
Today, 22:43
Joined
Nov 30, 2011
Messages
8,494
Hello Dan, This is just my way of doing it.. But you could wait for others to respond, I would create a function to do this..
Code:
Fullname: getFullName([Title], [Firstname], [LastName])
Where getFullName is a Function in a Module as,
Code:
Public Function getFullName(titleVar As Variant, fNameVar As Variant, lNameVar As Variant) As String
    Dim retStr As String
    If Len(titleVar & vbNullString) <> 0 Then retStr = retStr & titleVar & " "
    If Len(fNameVar & vbNullString) <> 0 Then retStr = retStr & fNameVar & " "
    If Len(lNameVar & vbNullString) <> 0 Then retStr = retStr & lNameVar & " "
    getFullName = Trim(retStr)
End Function
 

Dmak

Registered User.
Local time
Today, 22:43
Joined
May 10, 2012
Messages
65
Hello Dan, This is just my way of doing it.. But you could wait for others to respond, I would create a function to do this..
[/CODE]


This is really appreciated, thank you!
 

GanzPopp

Registered User.
Local time
Today, 23:43
Joined
Jan 14, 2013
Messages
37
If you only want to use SQL try the following

Code:
TRIM(REPLACE([Title] & " " & [Firstname] & " " & [LastName], "  ", " ")) AS FullName
in your query.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 17:43
Joined
Feb 19, 2002
Messages
43,477
You could also take advantage of the different way that + works when used as a concatenation operator. When you concatenate two fields, one of which is null, you get null returned if you use + but you get the non-null field when you use &. So:
Null + " " results in null But
Null & " " results in a space.

(Title + " ") & (FirstName + " ") & LastName
 

Users who are viewing this thread

Top Bottom