I would like to write a select query to output only the text containing of two letters and five digits.
The data type is varchar(max)
.
Each column may contain more than one of these numbers and also may contain a lots of other text. The server is a SQL Server 2012.
Sample data 1:
Reg.nr: AA11111, Dette er et teksttekst pa et test abonnement.
Referanse: Faktura 2222222222, Beløp: 1572.91,
Referanse: Purregebyr, Beløp: -64.00.
12.12.12 13:00:00 AAA
Utring ingen svar
18.12.14 18:00:00 BBB
tekst tekst tekst
14.01.15 15:00:00 CCC
tekst tekst tekst
Sample data 2:
text text text
AA11111 text,
BB22222 text,
text text
Desired output:
Sample data 1:
AA11111
Sample data 2:AA11111, BB22222
Try this:
SELECT
SUBSTRING(col, PATINDEX('%[a-zA-Z][a-zA-Z][0-9][0-9][0-9][0-9][0-9]%', '' + col), 7)
FROM TEST
WHERE PATINDEX('%[a-zA-Z][a-zA-Z][0-9][0-9][0-9][0-9][0-9]%', '' + col) <> 0
Here is a SQLFiddle with how the query works.