Since you've tagged this as sql-server, I'm going to assume that you're looking for recommendations on the database side, as opposed to how to do it in the front-end.
My recommendation is: Normalize it. If a ZoneCode belongs to a particular City, then you should have a table of Cities and a Table of ZoneCodes, with your Customer table (or whichever table has these columns now) referencing a ZoneCodeID
.
Example schema:
CREATE TABLE Cities
(
CityID int NOT NULL IDENTITY(1, 1)
CONSTRAINT PK_Cities PRIMARY KEY CLUSTERED,
CityName varchar(100) NOT NULL
)
CREATE INDEX IX_Cities_Name
ON Cities (CityName)
CREATE TABLE ZoneCodes
(
ZoneCodeID int NOT NULL IDENTITY(1, 1)
CONSTRAINT PK_ZoneCodes PRIMARY KEY CLUSTERED,
CityID int NOT NULL
CONSTRAINT FK_ZoneCodes_Cities FOREIGN KEY
REFERENCES Cities (CityID)
ON UPDATE NO ACTION,
ON DELETE NO ACTION
ZoneCode varchar(10) NOT NULL
)
CREATE INDEX IX_ZoneCodes_City
ON ZoneCodes (CityID)
INCLUDE (ZoneCode)
To filter it's as simple as:
SELECT ZoneCodeID, ZoneCode
FROM ZoneCodes
WHERE (@CityID IS NULL) OR (CityID = @CityID)
Or, if you only have the name:
SELECT z.ZoneCodeID, z.ZoneCode
FROM ZoneCodes z
INNER JOIN Cities c
ON c.CityID = z.CityID
WHERE (@CityName IS NULL) OR (CityName = @CityName)
If you can't do that - i.e. because this is freeform data, or because the data entry people might know the City but not the ZoneCode, etc., then all I can suggest is to make sure you're indexed properly:
CREATE INDEX IX_MyTable_City_ZoneCode
ON MyTable (City)
INCLUDE (ZoneCode)
Then your filter should be:
SELECT DISTINCT ZoneCode
FROM MyTable
WHERE (@City IS NULL) OR (City = @City)
...to get the ZoneCodes for a city, and:
SELECT FirstColumn, SecondColumn, ...
FROM MyTable
WHERE ((@City IS NULL) OR (City = @City))
AND ((@ZoneCode IS NULL) OR (ZoneCode = @ZoneCode))