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 2000 Forums
 Transact-SQL (2000)
 SQL using LIKE with many options

Author  Topic 

leandroengel
Starting Member

3 Posts

Posted - 2004-12-09 : 15:33:48
Hi everyone,

How can I use the LIKE with many options to do a SELECT?

For example:

SELECT * FROM CLIENT
WHERE NAME_CLIENTE LIKE ('joh%', 'carl%', 'j%')

I now that I could do as below, but I want use only 1 time the LIKE:

SELECT * FROM CLIENT
WHERE
NAME_CLIENTE LIKE ('joh%') OR
NAME_CLIENTE LIKE ('carl%') OR
NAME_CLIENTE LIKE ('j%') OR


Thanks a lot.

Leandro

chadmat
The Chadinator

1974 Posts

Posted - 2004-12-09 : 16:07:44
Why?

You have the solution. There is no shorter way.

-Chad

http://www.clrsoft.com

Software built for the Common Language Runtime.
Go to Top of Page

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2004-12-09 : 16:20:51
well its not necessarily shorter, but it may be what he is looking for:


Declare @dataTable table (colA varchar(20))
Insert Into @dataTable
Select 'Corey Aldebol'
Union Select 'Seventhnight'
Union Select 'Kaiden Elizabeth'
Union Select 'Julie Aldebol'


Declare @criteriaTable table (criteria varchar(20))
Insert Into @criteriaTable
Select '%Aldebol'
Union Select '%Liz%'

Select *
From @dataTable A
Inner Join @criteriaTable B
On A.colA like B.criteria


Corey
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2004-12-09 : 16:40:57
EDIT: by a good 20 minutes....

See...you should have never said never...or no way


USE Northwind
GO

SET NOCOUNT ON
CREATE TABLE myTable99(NAME_CLIENTE varchar(20))
GO

INSERT INTO myTable99(NAME_CLIENTE)
SELECT 'AjarnMark' UNION ALL SELECT 'Byrmol' UNION ALL SELECT 'Chadmat' UNION ALL SELECT 'DougG' UNION ALL
SELECT 'Ehorn' UNION ALL SELECT 'Frank Kalis' UNION ALL SELECT 'Graz' UNION ALL SELECT 'Harshal_in' UNION ALL
SELECT 'izaltsman' UNION ALL SELECT 'jsmith8858' UNION ALL SELECT 'Kristen' UNION ALL SELECT 'Lumbago' UNION ALL
SELECT 'Merkin' UNION ALL SELECT 'Nr' UNION ALL SELECT 'Onamuji' UNION ALL SELECT 'Page47' UNION ALL
SELECT 'Quazibubble' UNION ALL SELECT 'rrb' UNION ALL SELECT 'SamC' UNION ALL SELECT 'tduggan' UNION ALL SELECT 'Utpal' UNION ALL
SELECT 'ValterBorges' UNION ALL SELECT 'Wanderer' UNION ALL SELECT 'x002548' UNION ALL SELECT 'yakoo' UNION ALL SELECT 'zippy'

SELECT * FROM myTable99

DECLARE @x table(Col1 varchar(20))

INSERT INTO @x(Col1) SELECT 'j' UNION ALL SELECT 'c' UNION ALL SELECT 't'

SELECT * FROM @x

SELECT * FROM myTable99 m INNER JOIN @x x ON m.NAME_CLIENTE LIKE x.Col1 + '%'
GO

SET NOCOUNT ON
DROP TABLE myTable99
GO





Brett

8-)
Go to Top of Page

leandroengel
Starting Member

3 Posts

Posted - 2004-12-09 : 16:54:44
FANTASTIC!!!!!!!

Thanks for all!

The posts saved my problem!

It´s impressing! Just a little time, and you solved my problem!

Thanks.

Leandro

Go to Top of Page

chadmat
The Chadinator

1974 Posts

Posted - 2004-12-09 : 18:45:25
Maybe we should have asked what the problem was? Seems silly to me to create a table variable/temp table to avoid ORing a few like statements.


-Chad

http://www.clrsoft.com

Software built for the Common Language Runtime.
Go to Top of Page

chadmat
The Chadinator

1974 Posts

Posted - 2004-12-09 : 18:48:09
quote:
See...you should have never said never...or no way



BTW, I said no shorter way.

http://www.clrsoft.com

Software built for the Common Language Runtime.
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2004-12-09 : 20:28:50
well you don't have to create the table - but you probably should for clarity.

select tbl.*
from tbl
join (select s = 'a%' union all select 'b%' union all select 'c%') a
on tbl.fld like a.s

you should also use select distinct or

select *
from tbl
where exists
(select *
from (select s = 'a%' union all select 'b%' union all select 'c%') a
where tbl.fld like a.s
)


==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2004-12-10 : 09:34:03
And if you add this

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=25830&SearchTerms=udf,csv,string

You could pass a single string in to a sproc, use the link to break it up and insert in to the table, so you could pass in n number of search criteria...


Brett

8-)
Go to Top of Page

chadmat
The Chadinator

1974 Posts

Posted - 2004-12-10 : 11:34:22
I like Nigel's way. While it still uses TempDB, it avoids the temp table.

-Chad

http://www.clrsoft.com

Software built for the Common Language Runtime.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2004-12-10 : 12:36:04
Well aren't table variables stored in memory?

And why would Nigel's method use tempdb?

And wouldn't you have to use dynamic sql if the number of criteria or the criteria itself changes?



Brett

8-)
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2004-12-10 : 13:00:32
graz posted something not too long ago (a few months at least) that pointed to a KB article that said table variables will sometimes use tempdb. If the available RAM is not big enough the store the table variable then it would have to go to tempdb.
Go to Top of Page

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2004-12-10 : 13:14:12
Using the splitter you can do:


select tbl.*
from tbl
join (Select s=data From dbo.Split('a%,%b%,%c',',')) a
on tbl.fld like a.s


No table variable, to temp table... only the udf.

Corey
Go to Top of Page

chadmat
The Chadinator

1974 Posts

Posted - 2004-12-10 : 16:14:53
quote:
Originally posted by X002548

Well aren't table variables stored in memory?

And why would Nigel's method use tempdb?

And wouldn't you have to use dynamic sql if the number of criteria or the criteria itself changes?



Brett

8-)



1. No, Table Variables always use TempDB, they really are no different that temp table except that they go away automaically when they go out of scope, and you can't put indexes on them.

2. Unions use TempDB


-Chad

http://www.clrsoft.com

Software built for the Common Language Runtime.
Go to Top of Page

chadmat
The Chadinator

1974 Posts

Posted - 2004-12-10 : 16:29:40
Let me clarify, in the same way a temp table works, if the table will fit in memory, it won't necissarily use TempDB, but it is no different than a Temp Table in it's memory/tempdb behavior.

-Chad

http://www.clrsoft.com

Software built for the Common Language Runtime.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2004-12-10 : 16:30:13
quote:
Originally posted by chadmat

No, Table Variables always use TempDB, they really are no different that temp table



Huh?

Not that it references it in BOL, but Brian Knight does

quote:

One reason the table data type is so much faster is that it doesn't occupy space in the tempdb database as it's temporary table counterpart. Instead, the table data type uses the SQL Server's memory to store the data




Brett

8-)
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2004-12-10 : 16:32:12
quote:
Originally posted by chadmat

Let me clarify, in the same way a temp table works, if the table will fit in memory, it won't necissarily use TempDB, but it is no different than a Temp Table in it's memory/tempdb behavior.

-Chad

http://www.clrsoft.com

Software built for the Common Language Runtime.



OK, but doesn't a temp table always go to te,pdb?



Brett

8-)
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2004-12-10 : 16:34:18
Yes. And like all tables, it can also gets cached in the data buffer. A cached temp table performs no worse than a table variable, and if it's indexed and stat'ed, it can perform even better.
Go to Top of Page

chadmat
The Chadinator

1974 Posts

Posted - 2004-12-10 : 16:37:21
quote:
Originally posted by X002548

quote:
Originally posted by chadmat

No, Table Variables always use TempDB, they really are no different that temp table



Huh?

Not that it references it in BOL, but Brian Knight does

quote:

One reason the table data type is so much faster is that it doesn't occupy space in the tempdb database as it's temporary table counterpart. Instead, the table data type uses the SQL Server's memory to store the data




Brett

8-)



quote:

Q4: Are table variables memory-only structures that are assured better performance as compared to temporary or permanent tables, because they are maintained in a database that resides on the physical disk?

A4: A table variable is not a memory-only structure. Because a table variable might hold more data than can fit in memory, it has to have a place on disk to store data. Table variables are created in the tempdb database similar to temporary tables. If memory is available, both table variables and temporary tables are created and processed while in memory (data cache).


http://support.microsoft.com/default.aspx?scid=kb;en-us;305977

http://www.clrsoft.com

Software built for the Common Language Runtime.
Go to Top of Page

chadmat
The Chadinator

1974 Posts

Posted - 2004-12-10 : 16:43:47
quote:
OK, but doesn't a temp table always go to te,pdb?



Yes, as does a table variable.

-Chad

http://www.clrsoft.com

Software built for the Common Language Runtime.
Go to Top of Page

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2004-12-10 : 17:06:04
Now now gentlemen... you don't really even need either in this case

pthbbbbt!

Corey
Go to Top of Page
    Next Page

- Advertisement -