Solved

SQL Query optimise and clean up

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
MS SQL Bulk load data error 5 34
SQL Server memory Issue 7 74
Caste datetime 2 22
MSSQL: Wrong result in a query (Nulls) 6 0
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

762 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

19 Experts available now in Live!

Get 1:1 Help Now