user3370282
user3370282

Reputation: 21

sql server extract the "like substring"

I would like to get the "like substring" of select statement.

MyTable:

Container      ItemName

101            QQQ A12 DDD
101            QQTT A12 R33 
102            QQQ A3 AB3
103            QQ BB BB11

The select statement work fine to get the rows (records) I need (without the "like substring")

SELECT Container, <like substring>
  FROM MyTable
  Where (ItemName like '%A[0-9] %') OR (ItemName like '%A[0-9][0-9] %')
  GROUP BY Container, <like substring>

As a result I hope to get:

Container      "like substring"

101            A12
102            A3

The real question is how to get(display) the substring found by the like logical operator

How should I do it?

Thanks Yossi

Upvotes: 2

Views: 3289

Answers (1)

Yaakov Ellis
Yaakov Ellis

Reputation: 41510

This is not something that you can do with sql. Though the LIKE and PATINDEX do expose some features similar to regular-expressions, they are just used to filter the rows that meet the given criteria.

You are looking to do the equivalent of extracting a regex grouping within the Select section of the query, something that you can't do with native transact-sql. This is something that would be more appropriate to perform in your application code, after the results of the query have been returned.

Upvotes: 3

Related Questions