Solved

concatinate string with , whilst removing duplicate ,'s for null or empty fields

Posted on 2015-02-24
6
60 Views
Last Modified: 2015-07-08
Hi all

I'm wondering if its possible to concatenate a set of results with a "," but work it in such as was so that if one or more of the fields are empty/null I do not end up with multiple ,'s

I'm using addresses so for example rather than the following address showing as:
flat 4,, made up street,,,medway, kent, me1 23e

I would like it to be just single ,'s
flat 4, made up street, medway, kent, me1 23e
 
Regards
Neil
0
Comment
Question by:Neil Thompson
6 Comments
 
LVL 50

Accepted Solution

by:
Gustav Brock earned 500 total points
ID: 40627842
Yes, you can use:

=[Field1] & (", " + [Field2]) & (", " + [Field3]) & (", " + [Field4]) & (", " + [Field5])

", " + Null returns Null.
The first field must have a value.

/gustav
0
 
LVL 30

Expert Comment

by:hnasr
ID: 40627864
Try this:

Type this code in a general module.

Public Function sqButl(strVar As String)
    Dim strVarl As String
    Dim finished As Boolean
    strVarl = strVar
    Do While Not (finished)
        strVar = Replace(strVar, ",,", ",")
        If strVarl = strVar Then
            finished = Not (finished)
            sqButl = strVar
        Else
            strVarl = strVar
        End If
    Loop
End Function

Open in new window

Try it from the immediate window. You can call it from your code or query.

? sqBut1 ("flat 4,, made up street,,,medway, kent, me1 23e"
Result:
flat 4, made up street,medway, kent, me1 23e

The code can be improved to adjust  space after comma.
For example
0
 
LVL 36

Expert Comment

by:PatHartman
ID: 40629450
Gus' solution is the simplest.  It works because it takes advantage of the different concatenation behavior exhibited by the & and the +.  Fields concatenated using & will substitute a ZLS if one of them is null whereas, fields concatenated using + will return null if one of the fields is null.

So:
"a" & "," = "a,"
but
null & "," = null

Just make sure to use parentheses to control the order of precedence to ensure that you get the result you want.
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 3

Author Comment

by:Neil Thompson
ID: 40630293
Thanks all, I'm back in the office Friday so Ill test and award points then
0
 
LVL 3

Author Closing Comment

by:Neil Thompson
ID: 40872162
Excellent, thank you
0
 
LVL 50

Expert Comment

by:Gustav Brock
ID: 40872188
You are welcome!

/gustav
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

749 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