NHS Number Validation

I’ve been working on NHS number validation recently so thought I’d share my code. Although there is guidance available on how to validate NHS numbers using modulus 11 check digit valiation, no one seems to have put up any code for it. It’s simple but hopefully it’ll be useful.

It will obviously not tell you whether a particular number has been assigned to a patient. It will only tell you if the number has a valid check digit. It is still a useful tool for checking large volumes of records for transcription errors and the like.

I’ve succesfully used it in three different languages: T-SQL, Excel VBA and R. In all three cases it is implemented as a user defined function which will return either 1 or 0 (1 = valid, 0 = invalid).


CREATE FUNCTION [dbo].[fnNHSvalidation] (@NHSnumber NVARCHAR(10))


/*Declare variables to hold values for calculation and output*/
DECLARE @UniformNumberCheck NVARCHAR(1)

/*Find the length of the supplied NHS number*/
SET @NHSlength = LEN(@NHSnumber)

/*Declare variables to hold each digit of the NHS number, note that J is the check digit*/

/*Set variables to respective digits*/
SET @A = SubString(@NHSnumber,1,1)
SET @B = SubString(@NHSnumber,2,1)
SET @C = SubString(@NHSnumber,3,1)
SET @D = SubString(@NHSnumber,4,1)
SET @E = SubString(@NHSnumber,5,1)
SET @F = SubString(@NHSnumber,6,1)
SET @G = SubString(@NHSnumber,7,1)
SET @H = SubString(@NHSnumber,8,1)
SET @I = SubString(@NHSnumber,9,1)
SET @J = SubString(@NHSnumber,10,1)

/*Step 1: Ensure NHS number digits are not all equal (there is probably a FAR more elegant way of doing this)*/
IF ((@A = @B) AND (@B = @C) AND (@C = @D) AND (@D = @E) AND (@E = @F) AND (@F = @G) AND (@G = @H) AND (@H = @I) AND (@I = @J))
SET @UniformNumberCheck = 1
SET @UniformNumberCheck = 0

/*Step 2: Calculate modulus 11*/
SET @Modulus = ((@A * 10.0)+(@B * 9.0)+(@C * 8.0)+ (@D * 7.0)+(@E * 6.0)+(@F * 5.0)+(@G * 4.0)+(@H * 3.0)+(@I * 2.0))
SET @Modulus = 11-(@Modulus % 11)

/*Step 3: Logical statement to establish if NHS number is valid based on results of previous steps*/
IF (@Modulus=@J AND @UniformNumberCheck 1 AND @NHSlength=10) OR (@Modulus = 11 AND @J = 0 AND @UniformNumberCheck 1 AND @NHSlength = 10)
SET @ReturnValue = 1
ELSE SET @ReturnValue = 0

/*Generate output: 1 indicates a valid NHS number, 0 indicates an invalid NHS number*/
RETURN @ReturnValue



Excel VBA

Function NHSvalidation(NHSnumber)

Dim Modulus As Integer
Dim UniformNumberCheck As Integer
Dim NHSlength As Integer

NHSlength = Len(NHSnumber)

Dim A As Integer
Dim B As Integer
Dim C As Integer
Dim D As Integer
Dim E As Integer
Dim F As Integer
Dim G As Integer
Dim H As Integer
Dim I As Integer
Dim J As Integer

A = CInt(Mid(NHSnumber, 1, 1))
B = CInt(Mid(NHSnumber, 2, 1))
C = CInt(Mid(NHSnumber, 3, 1))
D = CInt(Mid(NHSnumber, 4, 1))
E = CInt(Mid(NHSnumber, 5, 1))
F = CInt(Mid(NHSnumber, 6, 1))
G = CInt(Mid(NHSnumber, 7, 1))
H = CInt(Mid(NHSnumber, 8, 1))
I = CInt(Mid(NHSnumber, 9, 1))
J = CInt(Mid(NHSnumber, 10, 1))

If ((A = B) And (B = C) And (C = D) And (D = E) And (E = F) And (F = G) And (G = H) And (H = I) And (I = J)) Then
UniformNumberCheck = 1
UniformNumberCheck = 0
End If

Modulus = ((A * 10) + (B * 9) + (C * 8 ) + (D * 7) + (E * 6) + (F * 5) + (G * 4) + (H * 3) + (I * 2))
Modulus = (11 - (Modulus Mod 11))

If (Modulus = J And UniformNumberCheck <> 1 And NHSlength = 10) Or (Modulus = 11 And J = 0 And UniformNumberCheck <> 1 And NHSlength = 10) Then
NHSvalidation = 1
NHSvalidation = 0
End If

End Function


NHSvalidation <- function(NHSnumber){



if ((A==B)&(B==C)&(C==D)&(D==E)&(E==F)&(F==G)&(G==H)&(H==I)&(I==J))





if (

((Modulus==J) & (UniformNumberCheck!=1) & (NHSlength==10))|((Modulus==11) & (J==0) & (UniformNumberCheck!=1) & (NHSlength==10)))



GIS Data and Resources

Ok I’m going to try and get a few of these sorts of pages going in order to build a repository of data sources and applications. I’ll continually edit them to add any new stuff I come across.




Just a quick post to continue the mapping theme. I’ve never used it for any sort of analysis but I’m a big fan of OpenStreetMap and its community generated mapping. Essentially its produced through a combination of satellite photography tracings and local knowledge, purporting to be way more accurate and up to date than any of the big on-line mapping projects.

I think the most interesting use of OSM was during the recent events in Haiti. Rescue teams needed up to date maps of areas that simply hadn’t been accurately mapped before. The OpenStreetMap Community contributed their time and effort and mapped Haiti with the help of several satellite imagery providers.

More details from BBC News: http://news.bbc.co.uk/1/hi/8517057.stm


HealthMap is a really interesting use of geographical and health information, coupled with a nice interface. I remain a bit sceptical about it but I do believe that it serves two functions extremely well:

  • A proof of concept, showing what’s possible when an innovative approach is taken to combining data sources in an automated fashion.
  • A starting point for researchers, epidemiologists and even the general public to begin sourcing articles on current health issues.

You do have to accept it for what it is, essentially a geographical news aggregator. For instance, if you search HIV/AIDS then very few records come back, I’m guessing this is because it’s a known epidemic and therefore doesn’t feature as a ‘breaking’ news article that often.

One issue to be a little wary about when first using HealthMap, each point on the map relates to a collection of reports/news articles; it’s almost measuring how much attention an epidemic is getting rather than the numbers affected. For instance, a point could represent 6 news stories about a single case or a single news story about 70 cases. I don’t think this is a problem, just something for the end user to be aware of.

Oh, and they sell T-shirts too! I’m a sucker for a nerdy T-shirt.

Here are some other articles about HealthMap with more details:

Professionalising Health Informatics

I should mention this really, though I’m still not sure I’m completely following the purpose of it and I’m sure everyone has already heard about it:


Connecting for Health are ‘professionalising health informatics’, whatever that means. Personally, I thought I already was the consummate professional. It’s aimed at all health informatics staff, including the IT crowd but don’t let that put you off.

Does it mean I can work towards a decent professional qualification and membership of a professional body in the same way the swelling ranks of accountants can? No. But it does provide a link to this fairly useful site: http://www.hiqualificationsfinder.org.uk/.

There are lots of sexy sounding phrases, like ‘IT-enabled change capability’, ‘enabling capability assurance’ etc.

Social Networking: Microsoft Style

One thing that irritates me about the NHS is how ruddy big it is. It’s such a huge, sprawling organisation staffed by a huge number of staff, each trained in one of the hundreds of vocations necessary to maintain the service. It’s easy to feel a bit lost in the vastness of it all.

I’ve been in the NHS for about 5 years now and I have to say that it’s always felt like I’m a wee bit trapped in my organisation. There’s not so much networking between different organisations, even though we’re really all working for the same boss. It never feels like people are talking to each other effectively across the country, it’s all so region centric.

Anyway, Microsoft have come up with quite a good idea (shock horror) to get people talking. I’m not sure how long this has been around but they’ve set up a rather nice resource for all NHS types, fittingly called the ‘Microsoft NHS Resource Centre’. Someone in copy deserves a pay rise for that title.

It’s for all NHS staff but there are several relevant discussion groups for Information nerds as well. Unfortunately there doesn’t seem to be a way of e-mailing or messaging on there but there are plenty of discussion groups going on.

My only criticism of the thing is that it’s so damn quiet on there! There are thousands of members signed up to it and there are lots of useful tips, articles and discounts but the discussion groups are really pretty dead. I hope that changes.

Here’s the link, go join in:



We in the NHS are definitely Microsoft junkies. Office is used by absolutely everyone and as an analyst a fair amount of my working time is spent using Excel.

Excel is a fantastic piece of software, it has it’s problems but on the whole it is powerful, fully featured and integrates brilliantly with other Microsoft software. What more could you ask for?

Well, I have a bit of an interest in the graphical application of information. I’ll discuss this in more detail in another post but suffice to say, I like to make analytical work engaging through nice design. Unfortunately Excel isn’t great for this. Even if you create a fantastic looking chart in Excel, you are limited with what you can do with it. You can paste it as a meta object or as a flat image file but that’s about it. The chart is pretty much trapped in Excel.

I don’t want flat image formats, I want to be able to pull apart my graphics, re format them, break them apart, move them around etc.

To do this you really need the chart to be exported as a SVG (scalable vector graphic) file. This is what Gnumeric brings to the table.

Then you can import this SVG file into your favourite vector editing suite (Inkscape has been my favourite for a long time but there are others available).

Once in Inkscape, the image can be broken apart and worked into a design for use in posters, presentations or print.

Here are the links!