I am using the substring function in a case statment to return all numbers between ‘B0’ – ‘B9’
WHEN SUBSTRING (postcode, 1, 2) like 'B[0-9]'
but i do not want to return the number ‘B5’ in this list. I can get it working as this
WHEN SUBSTRING (postcode, 1, 2) like 'B[0-4]'
WHEN SUBSTRING (postcode, 1, 2) like 'B[6-9]'
but is there a way to add this to one line like the following?
WHEN SUBSTRING (postcode, 1, 2) like 'B[0-9]' and not like 'B5'
EDIT:
how would you do this if you where dealing with numbers between 0-100 so the sql would be like
WHEN SUBSTRING (postcode, 1, 3) like 'B[0-9][0-9]'
but you did not want to include 16 and 17?
LIKE patterns are awesome. Mini regex, if you will.
To carry this out to the tens place or more for numbers, you might want to consider putting these values into a table that you can use for a
join,existsorin.Or, since we’re treating the numeric portion of this column as though they were numbers and not a string, this is a sign that they might have been better stored as two columns: one for the alpha, one for the numeric.
Barring any change to your schema, you might remove the alpha characters ad-hoc so you can compare them to a range of numbers, for example:
These are just a couple of possibilities. You would know best how to massage your data.