Solved

if-statement for customer bill-to address  different than regular  address

Posted on 2008-10-10
6
155 Views
Last Modified: 2010-03-20
Hi, my custómer table has the bill-to address null, whenever the customer's bill-to address is the same as the regular adress.  please give me the complete SQL that lists the customer bill-to adress, but if the customer doesn't have a bill-to address, then I want it to default to the regular address.
my criteria:
1.  if C.BILL_TO_ADDR_1 is null, then I want to list ADDR_1
2.  if C.BILL_TO_ADDR_1 is null, then I want it to list ADDR_2, if it is not null, then I wan to list C.BILL_TO_ADDR_2
3.  if C.BILL_TO_ADDR_1 is null, then I want it to list ADDR_3, if it is not null, then I wanto list C.BILL_TO_ADDR_3
4.  if C.BILL_TO_ADDR_1 is null, then I want it to list C.CITY, if it is not null, then I wanto list C.BILL_TO_CITY
5.  if C.BILL_TO_ADDR_1 is null, then I want it to list C.STATE, if it is not null, then I wanto list C.BILL_TO_STATE
6.  if C.BILL_TO_ADDR_1 is null, then I want it to list C.COUNTRY, if it is not null, then I wanto list C.BILL_TO_COUNTRY


please give me the complete sql, for me to copy/paste. (I dont know SQL) thank you very much

SELECT C.ID AS customer_ID, C.NAME, C.ADDR_1, C.ADDR_2, C.ADDR_3, C.CITY, C.STATE, C.COUNTRY, C.BILL_TO_NAME, C.BILL_TO_ADDR_1, C.BILL_TO_ADDR_2, C.BILL_TO_CITY, C.BILL_TO_STATE, C.BILL_TO_COUNTRY
FROM SYSADM_CUSTOMER AS C
GROUP BY C.ID, C.NAME, C.ADDR_1, C.ADDR_2, C.ADDR_3, C.CITY, C.STATE, C.ZIPCODE, C.COUNTRY, C.BILL_TO_NAME, C.BILL_TO_ADDR_1, C.BILL_TO_ADDR_2, C.BILL_TO_CITY, C.BILL_TO_STATE, C.BILL_TO_COUNTRY;

Open in new window

0
Comment
Question by:gringotani
[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
  • 3
  • 2
6 Comments
 
LVL 17

Expert Comment

by:Daniel Reynolds
ID: 22690368
SELECT C.ID AS customer_ID, C.NAME, C.ADDR_1, C.ADDR_2, C.ADDR_3, C.CITY, C.STATE, C.COUNTRY, coalesce(C.BILL_TO_NAME,C.NAME) [BILL_TO_NAME], coalesce(C.BILL_TO_ADDR_1,C.ADDR_1) [BILL_TO_ADDR_1]
, coalesce(C.BILL_TO_ADDR_2,C.ADDR_2) [BILL_TO_ADDR_2]
, coalesce(C.BILL_TO_CITY,C.ADDR_2) [BILL_TO_CITY]
, coalesce(C.BILL_TO_STATE,C.ADDR_2) [BILL_TO_STATE]
, coalesce(C.BILL_TO_COUNTRY,C.ADDR_2) [BILL_TO_COUNTRY]
FROM SYSADM_CUSTOMER AS C
GROUP BY C.ID, C.NAME, C.ADDR_1, C.ADDR_2, C.ADDR_3, C.CITY, C.STATE, C.ZIPCODE, C.COUNTRY, C.BILL_TO_NAME, C.BILL_TO_ADDR_1, C.BILL_TO_ADDR_2, C.BILL_TO_CITY, C.BILL_TO_STATE, C.BILL_TO_COUNTRY;
0
 
LVL 17

Expert Comment

by:Daniel Reynolds
ID: 22690380
oops, in my hurry, I forgot to update the 2nd value in the last 3 items. they should be

, coalesce(C.BILL_TO_CITY,C.CITY) [BILL_TO_CITY]
, coalesce(C.BILL_TO_STATE,C.STATE) [BILL_TO_STATE]
, coalesce(C.BILL_TO_COUNTRY,C.COUNTRY) [BILL_TO_COUNTRY]
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22690518
Although I would consider it an anomaly, your solution could provide inconsistent data of the address2 field would ever be null for the bill but not the general customer address.

So if the customers address (addr1, addr2, city, state and country) all have values, but the BILL to address has only addr1, city state and country, you would get addr2 from the customer table as if it were part of the billing address.

To get around this, it should be implemented with a case statement (or iif for access).

Make sense:

Test these results.
SELECT C.ID AS customer_ID, C.NAME, C.ADDR_1, C.ADDR_2, C.ADDR_3, C.CITY, C.STATE, C.COUNTRY
, iif(c.bill_to_name is null, c.name, c.bill_to_name) [bill_to_name]
, iif(c.bill_to_addr_1 is null, c.addr_1, c.bill_to_addr1) [bill_to_addr1]
 
, iif(c.bill_to_addr_1 is null, c.ADDR_2, c.bill_to_addr2) [bill_to_addr1]
, iif(c.bill_to_addr_1 is null, c.CITY, c.BILL_TO_CITY) [bill_to_addr1]
, iif(c.bill_to_addr_1 is null, c.STATE, c.BILL_TO_STATE) [bill_to_addr1]
, iif(c.bill_to_addr_1 is null, c.COUNTRY, c.BILL_TO_COUNTRY) [bill_to_addr1]
 
FROM SYSADM_CUSTOMER AS C
GROUP BY C.ID, C.NAME, C.ADDR_1, C.ADDR_2, C.ADDR_3, C.CITY, C.STATE, C.ZIPCODE, C.COUNTRY, C.BILL_TO_NAME, C.BILL_TO_ADDR_1, C.BILL_TO_ADDR_2, C.BILL_TO_CITY, C.BILL_TO_STATE, C.BILL_TO_COUNTRY;

Open in new window

0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:gringotani
ID: 22691427
Brandon, I tried ur query , but got the following error messsage:Syntax error (missing operator) in query expression 'IIF(C.BILL_TO_NAME IS NULL, C.NAME,C.BILL_TO_NAME) [BILL_TO_NAME'.]

pls advise, thanks
0
 
LVL 17

Accepted Solution

by:
Daniel Reynolds earned 50 total points
ID: 22691562
remove the '. from teh [BILL_TO_NAME'.]  so it is [BILL_TO_NAME]
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22694100
You have this tagged as access.  Are you running it in access, or as a passthrough query to a different db engine such as sql server?
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

726 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