Solved

SQL Query optimise and clean up

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

803 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