2

How to find pattern like this in sql server in a table.

I've table with rows like this.

ST CLAIR, TERESA
JR VISH, DALVI
JACK RIDER
JR JK, SMITH

I want to find the rows which follows this pattern ST CLAIR, TERESA so it will return 3 rows like below.

ST CLAIR, TERESA
JR VISH, DALVI
JR JK, SMITH

Query

select * from table
where column like ?  
Vishwanath Dalvi
  • 35,388
  • 41
  • 123
  • 155
  • 2
    What pattern...? Please describe it more. Two words, comma, another word? – chx Jul 09 '12 at 04:18
  • possible duplicate of [microsoft sql equivalent of mysql REGEXP](http://stackoverflow.com/questions/2515259/microsoft-sql-equivalent-of-mysql-regexp) – chx Jul 09 '12 at 04:23
  • @chx Both are different questions. – Vishwanath Dalvi Jul 09 '12 at 04:26
  • Hard to say without clarification on what you want. `% %, %` will match `100 200, 300`. To match *words* you need regular expressions. – chx Jul 09 '12 at 07:43

3 Answers3

1

Maybe you're looking for a pattern that includes a space character: [column] LIKE '% %, %'

Or if the comma is all you care about just use LIKE '%,%'

shawnt00
  • 16,443
  • 3
  • 17
  • 22
1

You mean this?

select * from table where column like '% %, %'  

You can test it like this:

WITH Foo (datacol)
AS( SELECT *
    FROM (  VALUES  ('ST CLAIR, TERESA'),
                    ('JR VISH, DALVI'),
                    ('JACK RIDER'), 
                    ('JR JK, SMITH') 
                    ) AS F (datacol))
SELECT datacol
FROM Foo where datacol like '% %, %';

It will give you the result you are looking for:

 ST CLAIR, TERESA
 JR VISH, DALVI
 JR JK, SMITH
Luxspes
  • 6,268
  • 2
  • 28
  • 31
0

As suggested by Luxspes you can use '% %, %' to filter the data or you can use parsename to get the result

Declare @Sample table
(word varchar(255))

Insert into @Sample
values 
('ST CLAIR , TERESA'),('JR VISH, DALVI'),('JACK RIDER'),('JR JK, SMITH')

Select word from @Sample
where parsename(replace(word,',','.'),2) is not null
praveen
  • 12,083
  • 1
  • 41
  • 49