SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Simple LIKE with wildcard


Simple LIKE with wildcard

Author
Message
ZeroFusion
ZeroFusion
Old Hand
Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)

Group: General Forum Members
Points: 302 Visits: 153
Great question, I'll keep that in mind as I also work with Czech_CI_AS collations.

tommyh (5/25/2010)
Out of curiousity. If one would like to get all strings containing a C from the below code... how would you do that in that collation?

create table #t (string varchar (5))

insert into #t (string) values ('ACH')
insert into #t (string) values ('ACY')
insert into #t (string) values ('AHHC')

select string from #t
where string like '%C%' -- will miss ACH

drop table #t


Suppose you have a Czech_CI_AS collation set up as your DB / table collation. Then you do this:

create table #t (string varchar (5))

insert into #t (string) values ('ACH')
insert into #t (string) values ('ACY')
insert into #t (string) values ('AHHC')

select string from #t
where string COLLATE Czech_BIN like '%C%' -- won't miss ACH

drop table #t


honza.mf
honza.mf
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1943 Visits: 1323
tommyh (5/25/2010)
Out of curiousity. If one would like to get all strings containing a C from the below code... how would you do that in that collation?

create table #t (string varchar (5))

insert into #t (string) values ('ACH')
insert into #t (string) values ('ACY')
insert into #t (string) values ('AHHC')

select string from #t
where string like '%C%' -- will miss ACH

drop table #t


It's very hard.

And if you are playing with our very special language, you can continue with case in case insensitive collation w00t : there is a difference if you are using 'ch', 'CH', 'Ch' and 'cH'.



See, understand, learn, try, use efficient
© Dr.Plch
vk-kirov
vk-kirov
Hall of Fame
Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)

Group: General Forum Members
Points: 3710 Visits: 4408
Very nice question, I learned something new.

honza.mf
On SQL Server with CZECH_CI_AS collation returns 3 rows ('CH', 'Ch', and 'ch') - this collation is case insensitive.

Note that CZECH_CS_AS (case sensitive collation) returns the same 3 rows.

...congratulations on winning the world hockey championship ;-)
honza.mf
honza.mf
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1943 Visits: 1323
ZeroFusion (5/25/2010)

Suppose you have a Czech_CI_AS collation set up as your DB / table collation. Then you do this:

create table #t (string varchar (5))

insert into #t (string) values ('ACH')
insert into #t (string) values ('ACY')
insert into #t (string) values ('AHHC')

select string from #t
where string COLLATE Czech_BIN like '%C%' -- will miss ACH

drop table #t


You are right, it works.
Are you really doing it? I must say, sometimes I change collation from case-insensitive to case-sensitive, but I have never used BIN type collation yet.



See, understand, learn, try, use efficient
© Dr.Plch
vk-kirov
vk-kirov
Hall of Fame
Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)

Group: General Forum Members
Points: 3710 Visits: 4408
honza.mf (5/25/2010)
If someone discover another collation in another language with similar behaviour, I will be very glad to have such information.

Here is a script which finds that collations (you should have the table #t from the QOTD):
SET NOCOUNT ON;

DECLARE @name SYSNAME;

DECLARE cur CURSOR LOCAL STATIC FOR
SELECT name
FROM ::fn_helpcollations();

CREATE TABLE #result (collation SYSNAME, string VARCHAR(2));

OPEN cur;

FETCH NEXT FROM cur INTO @name;

WHILE @@FETCH_STATUS = 0
BEGIN
EXEC('ALTER TABLE #t ALTER COLUMN string VARCHAR(2) COLLATE ' + @name);

IF @@ERROR = 0
INSERT #result (collation, string)
SELECT @name, string
FROM #t
WHERE string LIKE '_';


FETCH NEXT FROM cur INTO @name;
END;

CLOSE cur;

DEALLOCATE cur;

SELECT DISTINCT collation FROM #result;

SELECT DISTINCT collation, UPPER(string) FROM #result;

DROP TABLE #result;



It returns Croatian, Czech, Danish_Norwegian, Hungarian, Slovak, Traditional_Spanish, and Vietnamese on my machine.
ZeroFusion
ZeroFusion
Old Hand
Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)Old Hand (302 reputation)

Group: General Forum Members
Points: 302 Visits: 153
honza.mf (5/25/2010)
You are right, it works.
Are you really doing it? I must say, sometimes I change collation from case-insensitive to case-sensitive, but I have never used BIN type collation yet.

Well, as I said, I just learned how LIKE behaves with Czech_CI_AS collation, so no, I haven't used it yet. However, I use Czech_BIN when I need to order Czech strings (with Czech collation) in order of English alphabet:

CREATE TABLE #t ([string] VARCHAR (5));

INSERT INTO #t ([string])
VALUES ('ACH');
INSERT INTO #t ([string])
VALUES ('ACY');
INSERT INTO #t ([string])
VALUES ('AHHC');

-- Output: 1. ACY, 2. AHHC, 3. ACH
SELECT
Row_Number() OVER (ORDER BY [string]), [string]
FROM
#t;

-- Output: 1. ACH, 2. ACY, 3. AHHC
SELECT
Row_Number() OVER (ORDER BY [string] COLLATE Czech_BIN), [string]
FROM
#t;

DROP TABLE #t;



A note for people not familiar with Czech language: in the Czech alphabet, a letter "CH" is just between letters "H" and "I".
malleswarareddy_m
malleswarareddy_m
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2205 Visits: 1189
Hugo Kornelis (5/25/2010)
malleswarareddy_m (5/24/2010)
hi ,

like operator will search for _ only.and return any characters. then it does not return any rows.because i worked many times using like operator.but if we use '%_%' or '%_' or '_%'. then i think it will display the number of characters.but directly with like '_' it will not return the rows.and also i extecuted the above query but it will not return any rows.
Did you try setting the collation to CZECH_CI_AS, as Honza indicated in the explanation?

Certain Nordic collations will also consider some two-character combinations as a single character, and I expect German collations to do that as well (e.g. ss = ß, ue = ü, etc)



Hi I checked with different collations

with two queries

select string from #t where string COLLATE CZECH_CI_AS like '_'

the above you have given returns gives
CH
Ch
ch
¢h

four rows.

But when i using my default collation using this query

select string from #t where string COLLATE SQL_Latin1_General_CP1_CI_AS like '_'

It does not return any results.I got wrong.Because I did not checked on different collations.You answer is correct.The Result depens upon collations.Thanks for posting good answer.
learned some thing new for the day.

Malleswarareddy
I.T.Analyst
MCITP(70-451)
malleswarareddy_m
malleswarareddy_m
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2205 Visits: 1189
stewartc-708166 (5/25/2010)
Tricky
Requires some knowledge of collations

malleswarareddy_m (5/24/2010)
hi ,

like operator will search for _ only.and return any characters. then it does not return any rows.because i worked many times using like operator.but if we use '%_%' or '%_' or '_%'. then i think it will display the number of characters.but directly with like '_' it will not return the rows.and also i extecuted the above query but it will not return any rows.


Remember, malleswarareddy_m, when used with a LIKE, the underscore is also a wildcard, representing "any single character"


I know that _ is also wildcard character.but when i got wrong i executed on different combinations of wild chars but not concentrated on collations.Now i find the answer.now i am clear about the answer.

Malleswarareddy
I.T.Analyst
MCITP(70-451)
Hugo Kornelis
Hugo Kornelis
SSChampion
SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)

Group: General Forum Members
Points: 11020 Visits: 11994
honza.mf (5/25/2010)
stewartc-708166 (5/25/2010)
Tricky
Requires some knowledge of collations

Yes, the question was in Collations category :-P

Laugh I had not even seen that!
(note to self: in future, check question category before answering!)


Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
honza.mf
honza.mf
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1943 Visits: 1323
ZeroFusion (5/25/2010)

A note for people not familiar with Czech language: in the Czech alphabet, a letter "CH" is just between letters "H" and "I".


If you want to know more about Czech language have a look at http://www.omniglot.com/writing/czech.htm, very well done.
If you want to know more and have a fun, you can read Three Men on the Bummel by Jerome Klapka Jerome, chapter about Prague, all whole the book.



See, understand, learn, try, use efficient
© Dr.Plch
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search