0

I have a have a column which is set as character but users can insert numbers.

How can I identify when the field has numbers length 11 and not letters?

Example:

sample data {col1= aaaaaaaaaaa,11111122222}

select Col1
from
table
group by 1
having SUM(CHARACTER_LENGTH(Col1))=11

wrong output: aaaaaaaaaa

desired output: 11111122222

In short, I need to retrieve all values in the char column that could be identified as numbers and having 11 digits.

underscore_d
  • 6,309
  • 3
  • 38
  • 64
  • https://stackoverflow.com/questions/7288228/cast-as-int-only-when-character-is-a-number-on-firebird-2-5 Try this solution – Kelevra Nov 12 '19 at 16:44

1 Answers1

2

With similar to and character_length():

select * from tablename
where (col similar to '[0-9]+') and character_length(col) = 11

or:

select * from tablename
where col similar to '[0-9]{11}'

See the demo.

forpas
  • 160,666
  • 10
  • 38
  • 76
  • 1
    Hi forpas, awesome ! tks. – Jonathan Livingston Seagull Nov 12 '19 at 17:14
  • i think i heart there was something wrong about speed, when using "multipliers" within RegEx in Firebird. To the point that in FB4 they consider using different RegEx library (Google RE2) – Arioch 'The Nov 12 '19 at 20:25
  • @Arioch'The As far as I know, that problem had to do with complex patterns that trigger backtracking, that shouldn't be a problem here. BTW: They aren't considering a different library, they already switched in September. – Mark Rotteveel Nov 13 '19 at 11:50
  • @MarkRotteveel until FB4 is officially released I still consider it as "considered", because until anchored by release fait accompli it can always be switched back if any unexpected hardship with RE2 surfaces. – Arioch 'The Nov 13 '19 at 14:08