Merging data only if not null


im trying to merge data from 20 columns into one column with "and" or a comma as a separator. unfortunately, i keep on getting the null values as well.

ex. column 1 - null
     column 2 - null
     column 3 - gate

    merge into one column it looks like - "and and gate" or ", , gate"

how can i make access return the value only if it is not null - for twenty columns, i do not think it is practical to create if statements for all the possibilities. please help!!!!!


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You will need to something on each column (except the first), but only once on each column (or write a function to do it and pass

field1 + iif(isnull(field2),""," and "+field2) +  iif(isnull(field3),""," and "+field3) +  ect

This will only add the 'and' or comma if the field you are adding has a value.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mike EghtebasDatabase and Application DeveloperCommented:
if you want like:   ABC,n/a,CBS,n/a    use:

Nz(Field1,"n/a") & ", " & Nz(Field2,"n/a") & ", " & Nz(Field3,"n/a") & ", " & Nz(Field4,"n/a")
Mike EghtebasDatabase and Application DeveloperCommented:
But, if you like ABC,CBS  and ignoring null values, then use

Dim strTemp As String
if Nz(Field1,"") <>"" Then strTemp =strTemp &Field1 & ", "
if Nz(Field2,"") <>"" Then strTemp =strTemp &Field2 & ", "
if Nz(Field3,"") <>"" Then strTemp =strTemp &Field3 & ", "
if Nz(Field4,"") <>"" Then strTemp =strTemp &Field4 & ", "

strTemp = Left(strTemp,Len(strTemp)-2)
Mike EghtebasDatabase and Application DeveloperCommented:
FYI: and empty field, depending in its field type could be Null or Empty.  Therefore we need to use following code to handle both:

Nz(Field1,"") <>"" 

Now, Field1 if it is and Empty string (""), Nz() will become "" and then will be compared with <>"".
But, if it is a null value, it will change to "" thus covering both possiblities.

Therefore, first code need to be:

iif(Nz(Field1,"")="","n/a", Field1) & ", " & iif(Nz(Field2,"")="","n/a", Field2) & ", " & ...

Also, you may consider using | (in my keyboard it is above Enter key).

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.