2

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

user3370282
  • 21
  • 1
  • 4
  • Yossi, it is realy unclear what you are asking. – Yair Nevet Mar 02 '14 at 09:13
  • As SQL Server does not support regular expressions (apart from it's non-standard LIKE "extension") I don't think you can do it with T-SQL –  Mar 02 '14 at 09:24

1 Answers1

3

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.

Community
  • 1
  • 1
Yaakov Ellis
  • 40,752
  • 27
  • 129
  • 174