Solved

expression to string different text strings together while ignoring null fields

Posted on 2009-04-13
4
174 Views
Last Modified: 2012-05-06
ive got 5 fields of text and i want to create a query that runs an expression that strings all of the non-null fields (of that 5) together (intesperced with " - "). ive been creating a series of if statments which is ok for 3 fields, but gets crazy with any more than that.

can anyone help?

td
0
Comment
Question by:sci-aid
  • 2
  • 2
4 Comments
 
LVL 22

Expert Comment

by:Kelvin Sparks
ID: 24133836
Use a code module - for instances

Function StringJoin(Field1 as variant, Feild2 as variant, Field3 as variant, Field4 as variant, Field5 as variant) as string


if not isnull(Field1) then
     stringjoin = Field1
end if

If not IsNull(Field2) then
     If StringJoin <> "" Then
            StringJoin = StringJoin & "-" & Fiel2
     Else
            StringJoin = Field2
    End If
End If

Repeat for fields3 to 5

In Query call the function

Yourfield:StringJoin(Field1, Field2,......)




Kelvin


end function
0
 

Author Comment

by:sci-aid
ID: 24133882
thanks kelvin - ill give it a go. im a newbie with code so give me a while. td.
0
 

Author Comment

by:sci-aid
ID: 24149878
hi kelvin - as i said, bare with me as im a big time novice with the code side of access. ive had a play around with what you suggested but im not sure how to place a function within the query. i know its wrong, but here is what ive got so far:

SELECT frm_temp_protocol_01.Habitat, frm_temp_protocol_01.DataSource, frm_temp_protocol_01.Photoperiod24h, frm_temp_protocol_01.PhotoperiodObs

Function StringJoin(Habitat as variant, DataSource as variant, Photoperiod24h as variant, PhotoperiodObs as variant) as string
if not isnull(Habiat) then
     StringJoin = Habitat
end if

If not IsNull(DataSource) then
     If StringJoin <> "" Then
            StringJoin = StringJoin & ", " & DataSource
     Else
            StringJoin = DataSource
    End If
End If

If not IsNull(Photoperiod24h) then
     If StringJoin <> "" Then
            StringJoin = StringJoin & ", " & Photoperiod24h
     Else
            StringJoin = Photoperiod24h
    End If
End If

If not IsNull(PhotoperiodObs) then
     If StringJoin <> "" Then
            StringJoin = StringJoin & ", " & PhotoperiodObs
     Else
            StringJoin = PhotoperiodObs
    End If
End If

end function

FROM frm_temp_protocol_01;
0
 
LVL 22

Accepted Solution

by:
Kelvin Sparks earned 250 total points
ID: 24153281
The function is saved into the code modules.

The query is

SELECT StringJoin(frm_temp_protocol_01.Habitat, frm_temp_protocol_01.DataSource, frm_temp_protocol_01.Photoperiod24h, frm_temp_protocol_01.PhotoperiodObs) as NewString from frm_temp_protocol_01

My use of NewString in the query can be replaced with whatever you want to call the created column.


Kelvin
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In database programming, custom sort order seems to be necessary quite often, at least in my experience and time here at EE. Within the realm of custom sorting is the sorting of numbers and text independently (i.e., treating the numbers as number…
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

705 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now