Solved

SQL Query optimise and clean up

Posted on 2011-03-08
2
220 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
2 Comments
 
LVL 40

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

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…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

910 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now