[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

SQL Query optimise and clean up

Posted on 2011-03-08
2
Medium Priority
?
226 Views
Last Modified: 2012-05-11
Hello

Could someone help me clean up this query, it's a bit of a mess and I'm sure it could be optimised. It does work though.

Thanks!

DECLARE @PostCode nvarchar(4);
DECLARE @PostCode2 nvarchar(4);
SET @PostCode = 'EH1';
SET @PostCode2 = 'EH12';

/*  P = 3 and P2 = 3 */

IF LEN(@PostCode) = 3 AND LEN(@PostCode2) = 3

BEGIN
SELECT DISTINCT Emails.email
FROM            Emails LEFT JOIN
                         Customers ON Customers.EmailAddress = Emails.email
WHERE        LEN(REPLACE(REPLACE(Customers.PostCode, ' ', ''), '-', '')) = 6 AND 
				SUBSTRING(REPLACE(Customers.PostCode, ' ', ''), 1, 2) = SUBSTRING(@PostCode, 1, 2)
				AND SUBSTRING(REPLACE(Customers.PostCode, ' ', ''), 3, 1) BETWEEN 
				SUBSTRING(@PostCode, 3,1) AND SUBSTRING(@PostCode2, 3,1)
END ELSE 

/* IF P = 4 and P2 = 4 */

IF LEN(@PostCode) = 4 AND LEN(@PostCode2) = 4
BEGIN
SELECT DISTINCT Emails.email
FROM            Emails LEFT JOIN
                         Customers ON Customers.EmailAddress = Emails.email
WHERE        LEN(REPLACE(REPLACE(Customers.PostCode, ' ', ''), '-', '')) = 7 AND 
				SUBSTRING(REPLACE(Customers.PostCode, ' ', ''), 1, 2) = SUBSTRING(@PostCode, 1, 2)
				AND SUBSTRING(REPLACE(Customers.PostCode, ' ', ''), 3, 2) BETWEEN 
				SUBSTRING(@PostCode, 3,2) AND SUBSTRING(@PostCode2, 3,2)				
END ELSE

/* P = 3 AND P2 = 4 */

IF LEN(@PostCode) = 3 AND LEN(@PostCode2) = 4
BEGIN
SELECT DISTINCT Emails.email
FROM            Emails LEFT JOIN
                         Customers ON Customers.EmailAddress = Emails.email
WHERE        (LEN(REPLACE(REPLACE(Customers.PostCode, ' ', ''), '-', '')) = 6 AND 
				SUBSTRING(REPLACE(Customers.PostCode, ' ', ''), 1, 2) = SUBSTRING(@PostCode, 1, 2)
				AND SUBSTRING(REPLACE(Customers.PostCode, ' ', ''), 3, 1) BETWEEN 
				SUBSTRING(@PostCode, 3,1) AND 9)
				OR
				(LEN(REPLACE(REPLACE(Customers.PostCode, ' ', ''), '-', '')) = 7 AND 
				SUBSTRING(REPLACE(Customers.PostCode, ' ', ''), 1, 2) = SUBSTRING(@PostCode, 1, 2)
				AND SUBSTRING(REPLACE(Customers.PostCode, ' ', ''), 3, 2) BETWEEN 
				10 AND SUBSTRING(@PostCode2, 3,2))		
END

Open in new window

0
Comment
Question by:badpilot
[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
2 Comments
 
LVL 41

Accepted Solution

by:
Sharath earned 2000 total points
ID: 35071306
You can try like this.
DECLARE @PostCode nvarchar(4);
DECLARE @PostCode2 nvarchar(4);
SET @PostCode = 'EH1';
SET @PostCode2 = 'EH12';

SELECT DISTINCT Emails.email 
  FROM Emails 
       LEFT JOIN Customers 
         ON Customers.EmailAddress = Emails.email 
 WHERE ((LEN(@PostCode) = 3 
         AND LEN(@PostCode2) = 3) 
        AND (LEN(REPLACE(REPLACE(Customers.PostCode,' ',''),'-','')) = 6 
             AND SUBSTRING(REPLACE(Customers.PostCode,' ',''),1,2) = SUBSTRING(@PostCode,1,2)
             AND SUBSTRING(REPLACE(Customers.PostCode,' ',''),3,1) BETWEEN SUBSTRING(@PostCode,3,1) AND SUBSTRING(@PostCode2,3,1)))
        OR ((LEN(@PostCode) = 4 
             AND LEN(@PostCode2) = 4) 
            AND (LEN(REPLACE(REPLACE(Customers.PostCode,' ',''),'-','')) = 7 
                 AND SUBSTRING(REPLACE(Customers.PostCode,' ',''),1,2) = SUBSTRING(@PostCode,1,2)
                 AND SUBSTRING(REPLACE(Customers.PostCode,' ',''),3,2) BETWEEN SUBSTRING(@PostCode,3,2) AND SUBSTRING(@PostCode2,3,2)))
        OR ((LEN(@PostCode) = 3 
             AND LEN(@PostCode2) = 4) 
            AND ((LEN(REPLACE(REPLACE(Customers.PostCode,' ',''),'-','')) = 6 
                  AND SUBSTRING(REPLACE(Customers.PostCode,' ',''),1,2) = SUBSTRING(@PostCode,1,2)
                  AND SUBSTRING(REPLACE(Customers.PostCode,' ',''),3,1) BETWEEN SUBSTRING(@PostCode,3,1) AND 9)
                  OR (LEN(REPLACE(REPLACE(Customers.PostCode,' ',''),'-','')) = 7 
                      AND SUBSTRING(REPLACE(Customers.PostCode,' ',''),1,2) = SUBSTRING(@PostCode,1,2)
                      AND SUBSTRING(REPLACE(Customers.PostCode,' ',''),3,2) BETWEEN 10 AND SUBSTRING(@PostCode2,3,2))))

Open in new window

0
 

Author Closing Comment

by:badpilot
ID: 35071425
Beautifull!!! Thanks!
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

650 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