Solved

Proper syntax for Access 2010 SQL statement

Posted on 2014-09-14
13
598 Views
Last Modified: 2014-09-14
Hello,

I am trying to write a proper SQL statement that would combine multiple fields into a single field and if any of those fields were blank it would still put the rest of the information in with the proper punctuation.

I have written the following statement:

SELECT Trim(+ [BusinessStreet] & (", " + [BusinessCity]) & (", " + [BusinessState]) & " " & [BusinessPostalCode]) AS FullAddress, * INTO sample7
FROM MasterList;

When I then run this I do not get the results I am looking for.  If there is a PO Box in the BusinessStreet field then that is all that gets displayed...everything else is left out.  If there is nothing in the BusinessStreet field and BusinessStreet field then it still places a comma before the BusinessState field.

Hoping someone can tell me what I am missing and provide me with the proper syntax to accomplish.

Thank you for your time
0
Comment
Question by:FreddyBass
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
13 Comments
 
LVL 27

Expert Comment

by:MacroShadow
ID: 40321627
Try this:
SELECT Trim([BusinessStreet] & IIf(Nz([BusinessStreet] = "", ", "," ") & [BusinessCity] & IIf(Nz([BusinessStreet] = "", ", "," ") & [BusinessState] & " " & [BusinessPostalCode]) AS FullAddress, * INTO sample7
FROM MasterList;

Open in new window

0
 
LVL 85
ID: 40321631
Try using IIF and NZ statements to determine which fields hold a value:

SELECT IIF(Nz([BusinessStreet],"") <>"", BusinessStreet, "")  
& IIF(Nz([BusinessCity],"")<>"", ", " & [BusinessCity], "")
& IIF(Nz([BusinessState],"")<>"", ", " & [BusinessState],"" )
& IIF(Nz([BusinessPostalCode],"") <> "", [BusinessPostalCode], "") AS FullAddress, * INTO sample7
FROM MasterList;

You may need to modify that to better suit your needs in regard to commas, spaces, etc
0
 

Author Comment

by:FreddyBass
ID: 40321644
Hello MacroShadow,

Your sample said I had a syntax error when I ran it.

Hi Scott,

Thank you for your sample.  When I ran that it still placed a comma before the BusinessState field when the BusinessCity was missing.  What I am trying to have happen is if the field is blank then it will not put in a comma or blank space.  Hope you can tell me what I am missing.

Thank you both for your input... Looking forward
0
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
LVL 27

Expert Comment

by:MacroShadow
ID: 40321647
SELECT Trim([BusinessStreet] & IIf(Nz([BusinessStreet]) = "", ", "," ") & [BusinessCity] & IIf(Nz([BusinessStreet]) = "", ", "," ") & [BusinessState] & " " & [BusinessPostalCode]) AS FullAddress, * INTO sample7
FROM MasterList;
0
 

Author Comment

by:FreddyBass
ID: 40321663
Hi MacroShadow,

Just tried your suggestion...It does not place any punctuation when there is data in the field and it places punctuation when there is not.

I am trying to have syntax that will add proper punctuation when the field has data in it and not display punctuation if the field is blank.

Hope I am explaining properly?  Thank you for your input
0
 
LVL 27

Accepted Solution

by:
MacroShadow earned 500 total points
ID: 40321671
Sorry my bad.
SELECT Trim([BusinessStreet] & IIf(Nz([BusinessStreet]) = "", " ",", ") & [BusinessCity] & IIf(Nz([BusinessStreet]) = "", " ",", ") & [BusinessState] & " " & [BusinessPostalCode]) AS FullAddress, * INTO sample7
FROM MasterList;
0
 

Author Comment

by:FreddyBass
ID: 40321708
Hope people have not given up on me?

Could anyone tell me what it is I am missing or doing wrong with the syntax that is preventing me from having it work the way I would like?

Thank you in advance
0
 
LVL 27

Expert Comment

by:MacroShadow
ID: 40321721
You have to test the field for a value other than null (or a zero length string).
Then only if a value exists add a comma.
To do that you should use IIf together with Nz.

Did the last SQL I posted not work?
0
 

Author Comment

by:FreddyBass
ID: 40321734
Hi MacroShadow,

That did the trick!!!

Thank you so much...

If I wanted to combine First Name Middle Name and Last Name using the same principle would I just substitute the field names and be able to get the same results so if one of the fields were blank?

Thank you again for your input on helping me to resolve.
0
 

Author Closing Comment

by:FreddyBass
ID: 40321737
I would like to thank MacroShadow for sticking with it and providing me with the answer for what I was trying to accomplish.
0
 
LVL 27

Expert Comment

by:MacroShadow
ID: 40321739
If I wanted to combine First Name Middle Name and Last Name using the same principle would I just substitute the field names and be able to get the same results so if one of the fields were blank?
Yes.
0
 

Author Comment

by:FreddyBass
ID: 40321740
Thank you
0
 

Author Comment

by:FreddyBass
ID: 40321762
Hi MacroShadow,

I am trying to do some further testing so that if any of the fields are blank the end result field will have the proper punctuation and spacing.  i tried testing that if there was no PostalCode or no State info.

Also tried to substitute combining the FirstName MiddleName and LastName fields with the same kind of result.

Should I post another question since I had already accepted this one?

Because you did solve what I had originally ask...and thank you for that

Example:

SELECT Trim([BusinessStreet] & IIf(Nz([BusinessStreet]) = "", " ",", ") & [BusinessCity] & IIf(Nz([BusinessCity]) = "", " ",", ") & [BusinessState] & IIf(Nz([BusinessState]) = "", " ",", ") & " " & [BusinessPostalCode] & IIf(Nz([BusinessPostalCode]) = "", " ",", ")) AS FullName, * INTO sample7d
FROM MasterList;
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

707 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