I would like to return records based on a user specified postcode range, here is code I've got below which doesn't work.
DECLARE @FromPostCode nvarchar(4)
DECLARE @ToPostCode nvarchar(4)
SELECT Emails.Email, Customers.PostCode
LEFT JOIN ON
Customers.EmailAddress = Emails.Email
WHERE Customers.PostCode BETWEEN @FromPostCode AND @ToPostCode
Another problem is that some of the postcodes in the database aren't formatted correctly or even a valid post code so the query has to be build not to fail when it hits one of these.
For example :
SET @FromPostCode = EH1;
SET @ToPostCode = EH11;
would return EH1,2,3,4,5,6,7,8,9,10,11