Unix Technical Forum

Searching character data using like

This is a discussion on Searching character data using like within the SQL Server forums, part of the Microsoft SQL Server category; --> Hello All, SQL 2000, case insensitive database I have a situation where I need to find abbreviations in the ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 06:14 PM
Google User
 
Posts: n/a
Default Searching character data using like

Hello All,

SQL 2000, case insensitive database

I have a situation where I need to find abbreviations in the rows in a
table. The rule i came up is, get all the rows from the table where
there is more than one character is capitalized consequtively eg.
"USA", "TIMS", "AIR"

Here is the sample data:

create table test (mystring varchar(100))
go
insert into test (mystring) values ('I live in USA')
insert into test (mystring) values ('this is a test row. usa
(abbreviated wrongly).')
go

--expected result set
mystring
----------------------
I live in USA

Here is the query which I tried.
select * from test where mystring collate SQL_Latin1_General_CP1_CS_AS
like '%[A-Z][A-Z]%'

But the above query returns both the records. Any help?

Thanks
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 06:14 PM
Gert-Jan Strik
 
Posts: n/a
Default Re: Searching character data using like

That's strange, I get the same result. However, if I change the query
(see below), I get the correct results (run on a database with
SQL_Latin1_General_CP1_CS_AS collation)

select * from test where mystring like
'%[ABCDEFGHIJKLMNOPQRSTUVWXYZ][ABCDEFGHIJKLMNOPQRSTUVWXYZ]%'

Gert-Jan


Google User wrote:
>
> Hello All,
>
> SQL 2000, case insensitive database
>
> I have a situation where I need to find abbreviations in the rows in a
> table. The rule i came up is, get all the rows from the table where
> there is more than one character is capitalized consequtively eg.
> "USA", "TIMS", "AIR"
>
> Here is the sample data:
>
> create table test (mystring varchar(100))
> go
> insert into test (mystring) values ('I live in USA')
> insert into test (mystring) values ('this is a test row. usa
> (abbreviated wrongly).')
> go
>
> --expected result set
> mystring
> ----------------------
> I live in USA
>
> Here is the query which I tried.
> select * from test where mystring collate SQL_Latin1_General_CP1_CS_AS
> like '%[A-Z][A-Z]%'
>
> But the above query returns both the records. Any help?
>
> Thanks

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 06:14 PM
Google User
 
Posts: n/a
Default Re: Searching character data using like

Thanks..
Both the the solutions worked fine. I was playing around with the
expression after I posted the message. I got it working as per
Gert-Jan solution even before the post got appeared in the news group.
I will change according to Sommars solution since it is more straight
forward. Thanks again for both of you.

Erland Sommarskog <sommar@algonet.se> wrote in message news:<Xns93C2ECCFE3863Yazorman@127.0.0.1>...
> Google User (user_google@hotmail.com) writes:
> > Here is the query which I tried.
> > select * from test where mystring collate SQL_Latin1_General_CP1_CS_AS
> > like '%[A-Z][A-Z]%'
> >
> > But the above query returns both the records. Any help?

>
> This is because the range A-Z includes all characters in that range,
> and SQL_Latin1_General_CP1_CS_AS is not ASCII, but the order is
> AaBb....
>
> This query cuts it:
>
> select * from test where mystring
> like '%[A-Z][A-Z]%' collate Latin1_General_BIN
>
> I've chosen a binary collation. Also I've move the COLLATE expresion
> to the search pattern, although it does not seem to make a difference.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 02:17 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com