Regular expressions have been supported in DB2 for a while now and, while they are not something I often need, there are times when they come in very handy indeed. A simple example involves cleaning up some data so that it can be exported from an old (poorly maintained) database to a new, shiny one that (I’m promised) absolutely will be used.
The database in question is an employee master and, in order to protect the innocent, I have both simplified it and changed the names for the purpose if this example. There are only two fields to worry about empnumber
and empname
, which represent the employee number and name respectively. There are a couple of issues to address. The first one is that the empnumber
is ten characters in the old database but five numeric in the new, so I need to ensure that all the numbers fit.
The second issue is that this database has been very poorly maintained. If you allow someone to enter a character in a numeric field, they will. And they did.
Here’s an idea of how the database looked:
EMPNUMBER EMPNAME 00001 Bugs Bunny OOOO2 Elmer Fudd 0000000003 Daffy Duck -4 Speedy Gonzales 00005 Sylvester
As you can see, there are lots of ways in which the employee number can be invalid, and these have all been very well explored. And watch out for Elmer Fudd — he’s a bit confused about the difference between letters and numbers.
Fortunately REGEXP_INSTR is your friend:
The REGEXP_INSTR returns the starting position or the position after the end of the matched substring, depending on the value of the return_option argument.
So all I need is a regular expression that identifies which employee numbers are five digits followed by spaces, and which aren’t. And here it is:
select * from employees where regexp_instr(empnumber, '^\d{5}\s+$') != 1
There are several optional parameters but I only need to use the source string (empnumber) and the pattern expression. The expression I am using looks for digits in the first five characters followed by spaces to the end of the field. And this is the result:
EMPNUMBER EMPNAME OOOO2 Elmer Fudd 0000000003 Daffy Duck -4 Speedy Gonzales
I told you that Elmer Fudd was tricksy.
It should also be possible to use REGEXP_REPLACE to automate some of the cleanup, but you really do need your business users to buy in before you go down that particular rabbit hole.