W
warway
I am trying to make an address block from
Company eg Freds
Address1 eg 1 New Road
Address1
Town eg London
Region/County
Country Postcode eg UK EC1 4ZZ
using as store procedure shown below, but if there are two or more fields
empty the CompanyInfo 'block' is blank.
Is there a way achieve the desired reults without entering every
permuatation of the fields?
Alter PROCEDURE dbo.sp_CompanyAddress
AS
SELECT CompanyID,Company,MainContact,Telephone,Fax,Notes, 'CompanyInfo' =
CASE
WHEN Address1 IS NULL THEN Company + (Char(13) + Char(10)) + Address2 +
(Char(13) + Char(10)) + Town + (Char(13) + Char(10)) + Region + (Char(13) +
Char(10)) + Country + ', ' + PostCode
WHEN Address1 IS NULL AND Town IS NULL THEN Company + (Char(13) + Char(10))
+ Address2 + (Char(13) + Char(10)) + Region + (Char(13) + Char(10)) +
Country + ', ' + PostCode
WHEN Address1 IS NULL AND Region IS Null THEN Company + (Char(13) +
Char(10)) + Address2 + (Char(13) + Char(10)) + Town + (Char(13) + Char(10))+
Country + ', ' + PostCode
WHEN Address1 IS NULL AND Country IS NULL THEN Country + (Char(13) +
Char(10)) + Address2 + (Char(13) + Char(10)) + Town + (Char(13) + Char(10))
+ Region + (Char(13) + Char(10)) + PostCode
WHEN Address1 IS NULL AND PostCode IS NULL THEN Company + (Char(13) +
Char(10)) + Address2 + (Char(13) + Char(10)) + Town + (Char(13) + Char(10))
+ Region + (Char(13) + Char(10)) + Country
WHEN Address2 IS NULL THEN Company + (Char(13) + Char(10)) + Address1 +
(Char(13) + Char(10)) + Town + (Char(13) + Char(10)) + Region + (Char(13) +
Char(10)) + Country + ', ' + PostCode
WHEN Address2 IS NULL AND Town IS NULL THEN Company + (Char(13) + Char(10))
+ Address1 + (Char(13) + Char(10)) + Region + (Char(13) + Char(10)) +
Country + ', ' + PostCode
WHEN Address2 IS NULL AND Region IS Null THEN Company + (Char(13) +
Char(10)) + Address1 + (Char(13) + Char(10)) + Town + (Char(13) + Char(10))+
Country + ', ' + PostCode
WHEN Address2 IS NULL AND Country IS NULL THEN Country + (Char(13) +
Char(10)) + Address1 + (Char(13) + Char(10)) + Town + (Char(13) + Char(10))
+ Region + (Char(13) + Char(10)) + PostCode
WHEN Address2 IS NULL AND PostCode IS NULL THEN Company + (Char(13) +
Char(10)) + Address1 + (Char(13) + Char(10)) + Town + (Char(13) + Char(10))
+ Region + (Char(13) + Char(10)) + Country
WHEN Town IS NULL THEN Company + (Char(13) + Char(10)) + Address1 +
(Char(13) + Char(10)) + Address2 + (Char(13) + Char(10)) + Region +
(Char(13) + Char(10)) + Country + ', ' + PostCode
WHEN Town IS NULL AND Region IS Null THEN Company + (Char(13) + Char(10)) +
Address1 + (Char(13) + Char(10)) + Address2 + (Char(13) + Char(10))+ Country
+ ', ' + PostCode
WHEN Town IS NULL AND Country IS NULL THEN Country + (Char(13) + Char(10)) +
Address1 + (Char(13) + Char(10)) + Address2 + (Char(13) + Char(10)) + Region
+ (Char(13) + Char(10)) + PostCode
WHEN Town IS NULL AND PostCode IS NULL THEN Company + (Char(13) + Char(10))
+ Address1 + (Char(13) + Char(10)) + Address2 + (Char(13) + Char(10)) +
Region + (Char(13) + Char(10)) + Country
WHEN Region IS NULL THEN Company + (Char(13) + Char(10)) + Address1 +
(Char(13) + Char(10)) + Address2 + (Char(13) + Char(10)) + Town + (Char(13)
+ Char(10)) + Country + ', ' + PostCode
WHEN Region IS NULL AND Country IS NULL THEN Country + (Char(13) + Char(10))
+ Address1 + (Char(13) + Char(10)) + Address2 + (Char(13) + Char(10)) + Town
+ (Char(13) + Char(10)) + PostCode
WHEN Region IS NULL AND PostCode IS NULL THEN Company + (Char(13) +
Char(10)) + Address1 + (Char(13) + Char(10)) + Address2 + (Char(13) +
Char(10)) + Town + (Char(13) + Char(10)) + Country
WHEN Country IS NULL THEN Company + (Char(13) + Char(10)) + Address1 +
(Char(13) + Char(10)) + Address2 + (Char(13) + Char(10)) + Town + (Char(13)
+ Char(10)) + Region + (Char(13) + Char(10)) + PostCode
WHEN Country IS NULL AND PostCode IS NULL THEN Company + (Char(13) +
Char(10)) + Address1 + (Char(13) + Char(10)) + Town + (Char(13) + Char(10))
+ Region + (Char(13) + Char(10))
WHEN PostCode IS NULL THEN Company + (Char(13) + Char(10)) + Address1 +
(Char(13) + Char(10)) + Address2 + (Char(13) + Char(10)) + Town + (Char(13)
+ Char(10)) + Region + (Char(13) + Char(10)) + Country
ELSE Company + (Char(13) + Char(10)) + Address1 + (Char(13) + Char(10)) +
Address2 + (Char(13) + Char(10)) + Town + (Char(13) + Char(10)) + Region +
(Char(13) + Char(10)) + Country + ' ' + PostCode
END,
CAST(Company AS varchar(20)) AS 'Short Company',
Address1, Address2,Town,Region,Country,PostCode,SLACCOUNT
FROM CompanyAddressView
ORDER BY Company
Regards Warway
Company eg Freds
Address1 eg 1 New Road
Address1
Town eg London
Region/County
Country Postcode eg UK EC1 4ZZ
using as store procedure shown below, but if there are two or more fields
empty the CompanyInfo 'block' is blank.
Is there a way achieve the desired reults without entering every
permuatation of the fields?
Alter PROCEDURE dbo.sp_CompanyAddress
AS
SELECT CompanyID,Company,MainContact,Telephone,Fax,Notes, 'CompanyInfo' =
CASE
WHEN Address1 IS NULL THEN Company + (Char(13) + Char(10)) + Address2 +
(Char(13) + Char(10)) + Town + (Char(13) + Char(10)) + Region + (Char(13) +
Char(10)) + Country + ', ' + PostCode
WHEN Address1 IS NULL AND Town IS NULL THEN Company + (Char(13) + Char(10))
+ Address2 + (Char(13) + Char(10)) + Region + (Char(13) + Char(10)) +
Country + ', ' + PostCode
WHEN Address1 IS NULL AND Region IS Null THEN Company + (Char(13) +
Char(10)) + Address2 + (Char(13) + Char(10)) + Town + (Char(13) + Char(10))+
Country + ', ' + PostCode
WHEN Address1 IS NULL AND Country IS NULL THEN Country + (Char(13) +
Char(10)) + Address2 + (Char(13) + Char(10)) + Town + (Char(13) + Char(10))
+ Region + (Char(13) + Char(10)) + PostCode
WHEN Address1 IS NULL AND PostCode IS NULL THEN Company + (Char(13) +
Char(10)) + Address2 + (Char(13) + Char(10)) + Town + (Char(13) + Char(10))
+ Region + (Char(13) + Char(10)) + Country
WHEN Address2 IS NULL THEN Company + (Char(13) + Char(10)) + Address1 +
(Char(13) + Char(10)) + Town + (Char(13) + Char(10)) + Region + (Char(13) +
Char(10)) + Country + ', ' + PostCode
WHEN Address2 IS NULL AND Town IS NULL THEN Company + (Char(13) + Char(10))
+ Address1 + (Char(13) + Char(10)) + Region + (Char(13) + Char(10)) +
Country + ', ' + PostCode
WHEN Address2 IS NULL AND Region IS Null THEN Company + (Char(13) +
Char(10)) + Address1 + (Char(13) + Char(10)) + Town + (Char(13) + Char(10))+
Country + ', ' + PostCode
WHEN Address2 IS NULL AND Country IS NULL THEN Country + (Char(13) +
Char(10)) + Address1 + (Char(13) + Char(10)) + Town + (Char(13) + Char(10))
+ Region + (Char(13) + Char(10)) + PostCode
WHEN Address2 IS NULL AND PostCode IS NULL THEN Company + (Char(13) +
Char(10)) + Address1 + (Char(13) + Char(10)) + Town + (Char(13) + Char(10))
+ Region + (Char(13) + Char(10)) + Country
WHEN Town IS NULL THEN Company + (Char(13) + Char(10)) + Address1 +
(Char(13) + Char(10)) + Address2 + (Char(13) + Char(10)) + Region +
(Char(13) + Char(10)) + Country + ', ' + PostCode
WHEN Town IS NULL AND Region IS Null THEN Company + (Char(13) + Char(10)) +
Address1 + (Char(13) + Char(10)) + Address2 + (Char(13) + Char(10))+ Country
+ ', ' + PostCode
WHEN Town IS NULL AND Country IS NULL THEN Country + (Char(13) + Char(10)) +
Address1 + (Char(13) + Char(10)) + Address2 + (Char(13) + Char(10)) + Region
+ (Char(13) + Char(10)) + PostCode
WHEN Town IS NULL AND PostCode IS NULL THEN Company + (Char(13) + Char(10))
+ Address1 + (Char(13) + Char(10)) + Address2 + (Char(13) + Char(10)) +
Region + (Char(13) + Char(10)) + Country
WHEN Region IS NULL THEN Company + (Char(13) + Char(10)) + Address1 +
(Char(13) + Char(10)) + Address2 + (Char(13) + Char(10)) + Town + (Char(13)
+ Char(10)) + Country + ', ' + PostCode
WHEN Region IS NULL AND Country IS NULL THEN Country + (Char(13) + Char(10))
+ Address1 + (Char(13) + Char(10)) + Address2 + (Char(13) + Char(10)) + Town
+ (Char(13) + Char(10)) + PostCode
WHEN Region IS NULL AND PostCode IS NULL THEN Company + (Char(13) +
Char(10)) + Address1 + (Char(13) + Char(10)) + Address2 + (Char(13) +
Char(10)) + Town + (Char(13) + Char(10)) + Country
WHEN Country IS NULL THEN Company + (Char(13) + Char(10)) + Address1 +
(Char(13) + Char(10)) + Address2 + (Char(13) + Char(10)) + Town + (Char(13)
+ Char(10)) + Region + (Char(13) + Char(10)) + PostCode
WHEN Country IS NULL AND PostCode IS NULL THEN Company + (Char(13) +
Char(10)) + Address1 + (Char(13) + Char(10)) + Town + (Char(13) + Char(10))
+ Region + (Char(13) + Char(10))
WHEN PostCode IS NULL THEN Company + (Char(13) + Char(10)) + Address1 +
(Char(13) + Char(10)) + Address2 + (Char(13) + Char(10)) + Town + (Char(13)
+ Char(10)) + Region + (Char(13) + Char(10)) + Country
ELSE Company + (Char(13) + Char(10)) + Address1 + (Char(13) + Char(10)) +
Address2 + (Char(13) + Char(10)) + Town + (Char(13) + Char(10)) + Region +
(Char(13) + Char(10)) + Country + ' ' + PostCode
END,
CAST(Company AS varchar(20)) AS 'Short Company',
Address1, Address2,Town,Region,Country,PostCode,SLACCOUNT
FROM CompanyAddressView
ORDER BY Company
Regards Warway