Forum Moderators: coopster
<?php
$sql=mysql_query("SELECT DISTINCT postcode FROM areas ORDER BY postcode");
$results=mysql_fetch_array($sql);
$count=mysql_num_rows($sql);
if ($count>0) {
while ($results) {
$postcode=$results['postcode'];
$postcode_start = ereg_replace("[^a-zA-Z]", "", $postcode);
print $postcode_start;
print "<br/>";
$results=mysql_fetch_array($sql);
}
}
?>
SELECT
CASE postcode
WHEN REGEXP '^[a-z]\d' THEN SUBSTRING(postcode, 1, 1)
WHEN REGEXP '^[a-z]{2}\d' THEN SUBSTRING(postcode, 1, 2)
WHEN REGEXP '^[a-z]{3}\d' THEN SUBSTRING(postcode, 1, 3)
WHEN REGEXP '^[a-z]{4}\d' THEN SUBSTRING(postcode, 1, 4)
WHEN REGEXP '^[a-z]{5}\d' THEN SUBSTRING(postcode, 1, 5)
WHEN REGEXP '^[a-z]{6}\d' THEN SUBSTRING(postcode, 1, 6)
ELSE ''
END AS postcode
FROM areas
ORDER BY postcode
;