Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Select a string

Author  Topic 

ygeorge
Yak Posting Veteran

68 Posts

Posted - 2011-05-05 : 11:50:41
Hi,

Let's say I have a table like the following -

ID COL1
1 I like SQLTeam.
2 The SQLTeam is a good Team.
... ...

I am looking for a query that returns rows that contain the word Team or both SQLTeam and Team, but not SQLTeam only. Please help.

Thanks,
George

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-05-05 : 12:34:12
Would something like this work for you?

select * from YourTable where Col1 like '%[^a-z]Team[^a-z]%'
May need to be modified/enhanced based on your requirements, whether the collation is case-sensitive etc.
Go to Top of Page

ygeorge
Yak Posting Veteran

68 Posts

Posted - 2011-05-05 : 13:49:14
Excellent! That's exactly what I was looking for. Thank you!

quote:
Originally posted by sunitabeck

Would something like this work for you?

select * from YourTable where Col1 like '%[^a-z]Team[^a-z]%'
May need to be modified/enhanced based on your requirements, whether the collation is case-sensitive etc.

Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-05-05 : 14:00:45
You are quite welcome.

Just to be clear about what I was saying previously about the query needing modifications/enhancements:

1. If you have case-sensitive collation, you may need to modify it to '%[^a-zA-Z]Team[^a-zA-Z]%'.

2. If col1 started or ended with the word, for example, "Team from New York" (without the quotes) it would not work. You could get around it by changing the statement to ' ' + Col1 + ' ' like '%[^a-z]Team[^a-z]%'

And so on. I don't know what else I have not thought of.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2011-05-06 : 09:37:49
<<
And so on. I don't know what else I have not thought of.
>>

I think you have already covered all possibilities

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -