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:
CREATE FUNCTION [dbo].[fnDSA2ISA_PostcodeBranch]()RETURNS @MyResult TABLE (PostcodeBranchCode VARCHAR(16), Postcode INT, BranchCode VARCHAR(16), MetaCreatieDatum DATETIME )ASBEGIN
DECLARE @TEMP_PostcodeBranch TABLE( BranchCode VARCHAR(16), PostcodeStart INT, PostcodeEind INT);
WITH BSAS(SELECT ROS.KostenPlaatsCode AS BranchCode , PostalCodeStart AS PostcodeStart , PostCodeEnd AS PostcodeEindFROM 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 @MyResultSELECT DISTINCT CAST(t1.BranchCode + '_' + CAST(t2.n AS VARCHAR(6)) AS VARCHAR(16)) AS PostcodeBranchCode, t2.N AS Postcode, t1.BranchCode, GETDATE() AS MetaCreatieDatumFROM @TEMP_PostcodeBranch t1JOIN Tally t2 ON t2.N BETWEEN t1.PostcodeStart AND t1.PostcodeEind;RETURN;
ENDThis article is part of a series:
- Part 1: Dutch postcode table + SQL script for import
- Part 2: Split postcode ranges into separate records
- Part 3: Enrich customer data with the postcode table
- 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
Related blogs
T-TSQL - Create 4 week Moving Total
Calculate Age in SQL Server
Linked Server - Why and how use them!