Search code examples
sqldb2sql-like

DB2 complex like


I have to write a select statement following the following pattern:

[A-Z][0-9][0-9][0-9][0-9][A-Z][0-9][0-9][0-9][0-9][0-9]

The only thing I'm sure of is that the first A-Z WILL be there. All the rest is optional and the optional part is the problem. I don't really know how I could do that.

Some example data:

B/0765/E  3
B/0765/E3
B/0764/A   /02
B/0749/K
B/0768/
B/0784//02
B/0807/

My guess is that I best remove al the white spaces and the / in the data and then execute the select statement. But I'm having some problems writing the like pattern actually.. Anyone that could help me out?

The underlying reason for this is that I'm migrating a database. In the old database the values are just in 1 field but in the new one they are splitted into several fields but I first have to write a "control script" to know what records in the old database are not correct.

Even the following isn't working:

where someColumn LIKE '[a-zA-Z]%';

Solution

  • You can use Regular Expression via xQuery to define this pattern. There are many question in StackOverFlow that talk about patterns in DB2, and they have been solved with Regular Expressions.