Concatenate with Nz

steve21nj

Registered User.
Local time
Yesterday, 21:53
Joined
Sep 11, 2012
Messages
260
In this example, I'm working with concatenated values.
CBNOPREF = "No Pref"
CBWORD = "Word"
CBEXCEL = "Excel"

So the if my data had each it would show (No Pref;Word;Excel)
That's fine until my data only has excel, and would show (;;Excel)

How would I had Nz to the following to eliminate the blank ";"?

Code:
DESIREDFORMAT: [CBNOPREF] & ";" & [CBWORD] & ";" & [CBEXCEL]
 
This isn't a problem to solve with NZ its an issue to solve with IIF. You need to add a semi-colon only where there is un-semicoloned data before the value you want to add.

I would do this in a custom function. I would pass it all the values you want to concatenate and then pass them into a return variable one at a time, adding semi colons as necessary:

Code:
Dim ret AS String

ret=Value1

if (IsNull(Value2)=False) then 
    if (isnull(ret)= False Then ret = ret & ";"
    ret = ret & Value2
    End If

if (IsNull(Value3)=False) then 
    if (isnull(ret)= False Then ret = ret & ";"
    ret = ret & Value3
    End If
.
.
.

This can be more elegant using an array and a loop, but that's the gist.
 
How about null propagation:

Code:
DESIREDFORMAT: ([CBNOPREF]+";") & ([CBWORD]+";") & ([CBEXCEL])

If any result inside the presens is NULL the hole expression is evaluated to null

Just my 2 cents

JR
 
Not a bad idea JANR, but if CBEXCEL is null it would end with a semi-colon as the last character, don't know if that's what he wants.
 
Not a bad idea JANR, but if CBEXCEL is null it would end with a semi-colon as the last character, don't know if that's what he wants

Yeah you are right, but how about:

Code:
DESIREDFORMAT: Replace(Trim(([CBNOPREF]) & (" "+[CBWORD]) & (" "+[CBEXCEL]))," ",";")

You insert a space between the elements, trim surplus space and finally insert your seperator of choice.

JR
 

Users who are viewing this thread

Back
Top Bottom