Search Shortcut cmd + k | ctrl + k
ng_postcode

Parse, validate, format, and group Nigeria's NIPOST digital postcode (NDAPS)

Maintainer(s): Adeniyikayodee

Compatibility Matrix

This table covers all DuckDB releases that have not reached end-of-life, including LTS releases, and shows the 2 most recent patch releases of each. Older patch releases are not listed, but the extension may be available for them as well.

Platform 1.4.4 1.4.5 1.5.5 1.5.6 2.0.0
Linux AMD64
Linux ARM64
macOS AMD64
macOS ARM64
Windows AMD64
Windows ARM64
WebAssembly

Installing and Loading

INSTALL ng_postcode FROM community;
LOAD ng_postcode;

Example

SELECT ng_postcode_parse('ek 01 a03 fk 01') AS code,
       ng_postcode_format(ng_postcode_truncate('ek 01 a03 fk 01', 'district')) AS district;

About ng_postcode

Nigeria's digital postcode has eleven letters and digits in five segments: state, local government area, district, area, and unit. It is written EK-01-A03-FK-01 and stored as EK01A03FK01.

This extension reads the code offline, with no network call and no lookup table. A value that is not a well-formed code gives NULL.

  • ng_postcode_parse(text) returns the compact form of a complete code.
  • ng_postcode_prefix_parse(text) accepts a code cut off after any segment.
  • ng_postcode_format(text) returns the hyphenated form.
  • ng_postcode_truncate(text, segment) cuts a code after state, lga, district, area, or unit.
  • ng_postcode_parent(text) returns the next coarser prefix.
  • ng_postcode_within(text, prefix) tells whether a code lies inside a prefix.
  • ng_postcode_compact(text) strips spaces and hyphens and uppercases.
  • ng_postcode_version() returns the extension version.

Counting rows by district needs no map:

SELECT ng_postcode_format(ng_postcode_truncate(postcode, 'district')) AS district, count(*)
FROM deliveries GROUP BY 1 ORDER BY 2 DESC;

The functions pass the shared test cases of the ng-postcode libraries for Rust, Python, JavaScript, and Java. The source is at https://github.com/Adeniyikayodee/ng-postcode-duckdb.

Added Functions

function_name function_type description comment examples
ng_postcode_parse scalar Parse a Nigerian NIPOST postcode in any case or spacing into its 11-character compact form, or NULL if malformed Returns VARCHAR. Rejects the old six-digit code. [SELECT ng_postcode_parse('ek 01 a03 fk 01'); – EK01A03FK01]
ng_postcode_prefix_parse scalar Parse a postcode cut off after any segment into its compact prefix, or NULL Returns VARCHAR. [SELECT ng_postcode_prefix_parse('ek-01-a03'); – EK01A03]
ng_postcode_format scalar Format a code or prefix in the hyphenated style, or NULL if malformed Returns VARCHAR. [SELECT ng_postcode_format('EK01A03FK01'); – EK-01-A03-FK-01]
ng_postcode_truncate scalar Cut a code after a segment: state, lga, district, area or unit Returns VARCHAR. NULL for an unknown segment. [SELECT ng_postcode_truncate('EK-01-A03-FK-01', 'district'); – EK01A03]
ng_postcode_parent scalar Return the next coarser prefix of a code or prefix Returns VARCHAR. [SELECT ng_postcode_parent('EK01A03FK01'); – EK01A03FK]
ng_postcode_within scalar Whether a code lies inside a prefix Returns BOOLEAN. [SELECT ng_postcode_within('EK-01-A03-FK-01', 'EK-01'); – true]
ng_postcode_compact scalar Strip spaces and hyphens and uppercase, without validating Returns VARCHAR. [SELECT ng_postcode_compact(' ek-01 a03 fk 01 '); – EK01A03FK01]
ng_postcode_version scalar The extension version Returns VARCHAR. [SELECT ng_postcode_version();]

Overloaded Functions

This extension does not add any function overloads.

Added Types

This extension does not add any types.

Added Settings

name description input_type scope aliases typed_value
temp_directory Set the directory to which temporary files are written. VARCHAR GLOBAL [] post.db.tmp