Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

joining fields - exclude empty string

Status
Not open for further replies.

kristinac

Programmer
Jul 18, 2003
98
US
I am combining 4 fields in my table into one. The third field is often either Null or an empty string. I know to exclude the Null values you use the + instead of &.

SELECT Name & ' ' & Type & ' (' + SRNumber + ')' & ' ' & City AS FullName

That works great when SRNumber is Null. But many times it is blank. If so, I get the parentheses - it does not exclude the field. Is there a way to exclude SRNumber if it contains an empty string?

Thanks in advance...
 
How about this?

select name & ' ' & type & switch(srnumber,len(nz(srnumber,"")=0,' ',len(nz(srnumber,""))>0,' (' & srnumber & ')') & ' ' & city as fullname

Good luck,
Dan
 
Try:
SELECT Name & ' ' & Type & IIf(Len(SRNumber & "")>0,' (' & SRNumber & ')', '') & ' ' & City AS FullName

Duane
MS Access MVP
 
Okay. This is the error I get when I use your suggestion:

Wrong number of arguments used with function in query expression 'Name & ' ' & Type & switch(SRNumber,len(nz(SRNumber,")=0,' ',len(nz(SRNumber,"))>0,' (' & SRNumber & ')') & ' ' & City'.

I need to look up the switch function.
 
This is the error I get with the second one:

Syntax error in string in query expression 'Name & ' ' & Type & IIf(Len(SRNumber & ")>0,' (' & SRNumber & ')', '') & ' ' & City AS FullName FROM Streets ORDER BY Name, Type, SRNumber, City'.

You can use SQL functions if the database is in Access can't you?

Thank you both for your suggestions.
 
Okay I was trying to run a query within access itself using the IIF function.

SELECT Name, SRNumber, IIF(SRNumber>0, "no","yes") AS FullName FROM Streets

Anywhere SRNumber was NOT >0, the field was yes. Anywhere SRNumber was > 0, the field said #ERROR.

Any ideas?
 
Okay I don't know what I screwed up the first time Duane. Yours is working great. Thank you!
 
Glad to hear it is working for you. One other point/suggestion: name is a very poor name for a field since every object in Access has a name property. If you place a text box in any report
=[Name]
this will display the name of the report.

Duane
MS Access MVP
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top