One requirement might be to show market coverage within the Netherlands on a map. The postcode areas of the Netherlands are divided by region. A region in turn can consist of ranges with postcodes. Or a certain location is linked to a postcode range, or a salesperson has to cover area 1234 to 4567.

Because the source data for this requirement is stored as a range, I had to split the postcode range into separate rows.

The source data looks like this:

Vestiging- PostcodeStart - PostcodeEind

1 - 2700 - 2750
2- 2751 - 2800…………etc

I had to split the result so I would get a unique row for every postcode4. With this data I can then do nice things, for example in Microsoft Excel with the Maps function.

1 - 2700
1 - 2701
1 - 2702
….etc

Solution in SQL:

SQL
CREATE FUNCTION [dbo].[fnDSA2ISA_PostcodeBranch]()
RETURNS @MyResult TABLE
(PostcodeBranchCode VARCHAR(16),
Postcode INT,
BranchCode VARCHAR(16),
MetaCreatieDatum DATETIME
)
AS
BEGIN
DECLARE @TEMP_PostcodeBranch TABLE(
BranchCode VARCHAR(16),
PostcodeStart INT,
PostcodeEind INT);
WITH BS
AS(
SELECT ROS.KostenPlaatsCode AS BranchCode
, PostalCodeStart AS PostcodeStart
, PostCodeEnd AS PostcodeEind
FROM SourceTable
)
INSERT INTO @TEMP_PostcodeBranch (BranchCode, PostcodeStart, PostcodeEind)
SELECT * FROM BS;
WITH Tens (N) AS (SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1),
Hundreds(N) AS (SELECT 1 FROM Tens t1, Tens t2),
Millions(N) AS (SELECT 1 FROM Hundreds t1, Hundreds t2),
Tally (N) AS (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM Millions)
INSERT @MyResult
SELECT DISTINCT
CAST(t1.BranchCode + '_' + CAST(t2.n AS VARCHAR(6)) AS VARCHAR(16)) AS PostcodeBranchCode,
t2.N AS Postcode,
t1.BranchCode,
GETDATE() AS MetaCreatieDatum
FROM @TEMP_PostcodeBranch t1
JOIN Tally t2 ON t2.N BETWEEN t1.PostcodeStart AND t1.PostcodeEind;
RETURN;
END

This article is part of a series:

  1. Part 1: Dutch postcode table + SQL script for import
  2. Part 2: Split postcode ranges into separate records
  3. Part 3: Enrich customer data with the postcode table
  4. Part 4: Visualize data with coordinates in Power BI

Frequently asked questions

How do I split a postcode range into separate records in SQL Server?

Use a tally table and join it with a BETWEEN on PostcodeStart and PostcodeEind. You then get a unique row for every postcode4 in the range.

Why do I have to split a postcode range into separate rows?

When the source data is stored as a range, for example 2700 to 2750 per branch, you need one row per postcode4. With those rows you can use the Maps function in Excel, for example.

How do I build a tally table with only CTEs in T-SQL?

Make a CTE Tens with ten rows and cross join it with itself for Hundreds and Millions. Then ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) gives the numbers in the CTE Tally.

Which columns does the function fnDSA2ISA_PostcodeBranch return?

PostcodeBranchCode, Postcode, BranchCode and MetaCreatieDatum. PostcodeBranchCode is the BranchCode, an underscore and the postcode. The function uses DISTINCT so every row is unique.

How do I link a branch or salesperson to a postcode area?

Store a PostcodeStart and a PostcodeEind per branch, for example 1234 to 4567. Then split that range into separate postcodes with the SQL function.

Comments

Your email address is not shown. A photo comes from Gravatar, if you have one.