Solved

SQL Query optimise and clean up

Posted on 2011-03-08
2
224 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 500 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

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?
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

724 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