I've learnt a lot about the NHS recently. Specifically, how the NHS works with technology or in many cases how it fails to work with technology.
As part of this work, we've had to work with NHS numbers. An NHS number is just an unique 10-digit number that represents a person. It is (I think), the only actual unique bit of data stores per person in the UK that ties them to parts of their medical records. It's usually formatted like
123 345 2345.
When we started working with these numbers I had, mistakenly, assumed that these numbers were just incrementing ID numbers; just a standard auto-incrementing primary key. Turns out I was wrong.
I started bashing in 10-digit numbers in to our testing clinical systems which were telling me that these numbers weren't valid.
Numbers like this aren't valid:
- 123 233 1234
- 347 478 2737
- 289 358 5890
but numbers like this are valid:
- 931 331 7125
- 546 606 6893
It turns out, it's not quite as simple as I had assumed. Never one to shy away from a rabbit hole, it was time to investigate. It quickly transpired that generating an NHS number wasn't as simple as incrementing the number.
Firstly, it turns out an NHS number must be between
400000000(that's four-hundred million) to
708800001. This, unfortunately, raises more questions than it answers:
Why the gap of 100,000,000 IDs in the middle?
I have no idea. Please someone enlighten me.
Why do those IDs have only 9 digits? I thought NHS numbers had 10-digits.
The 10th digit is actually a checking digit. A
parity bit in computer speak. It's presence allows us to check that the previous 9 numbers are correct as the 10th number is a function of the other 9.
How do we generate the checking digit?
For each of the first 9 digits of a candidate NHS number, multiply its value by 11 minus its position and sum the result.
For example, in the following NHS number:
931 331 7125
We would do the following sum:
(9 * (11 - 1)) + ^ number |^ one-indexed position (3 * (11 - 2)) + (1 * (11 - 3)) + (3 * (11 - 4)) + (3 * (11 - 5)) + (1 * (11 - 6)) + (7 * (11 - 7)) + (1 * (11 - 8)) + (2 * (11 - 9))
which evaluates to:
90 + 27 + 8 + 21 + 18 + 5 + 28 + 3 + 4 = 204
The calculate the remainder when dividing this number by 11 (known as the modulus and denoted with the
% symbol) and subtract that result from 11.
204 % 11 = 6 11 - 6 = 5
Then celebrate because you got all the maths right first time. We calculated 5 as our check digit, and as you can see that matches the 10th digit in our candidate NHS number.
That gives us a (relatively) simple way of checking if all of the digits of an NHS number have been recorded correct as just one wrong digit will result in the checking digit (probably) being incorrect.
There are, as always, a couple of edge cases though:
- If the checking digit evaluates to 11, then just replace it with 0.
- If the checking digit is 10. It's invalid.
Caveat: This only applies to NHS England (and probably Wales?), but definitely not Scotland or Northern Ireland as they have their own systems.