0

I have an address field like

'12345 dummycity 14'
'NL - 54321 City'
'12 City4  32154'

I want to extract only the substring where there are 5 numeric characters in a row.

Anybody has a clue how to extract this in T-SQL?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

1

Such string processing is a little tricky in SQL Server, but patindex() does much of the work for you:

select left(stuff(str, 1, patindex('%[0-9][0-9][0-9][0-9][0-9]%', str) - 1, ''), 5)
from (values ('NL - 54321 City')) v(str)
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786