473,394 Members | 1,840 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Like statement

Jim
I need to build a bunch of dynamic like statements '% ' or '% %'.

heres an example of one that im working on just to test..

declare @check varchar(50)
set @check = 'Bad'
declare @select varchar(50)
set @select = 'select * from art where style like %' + @check + ' and
inventoryid =13'

exec (@select)
how can I make this work for either like statements?

thanks

-Jim
Jul 20 '05 #1
2 3267

"Jim" <ji********@motorola.com> wrote in message
news:72**************************@posting.google.c om...
I need to build a bunch of dynamic like statements '% ' or '% %'.

heres an example of one that im working on just to test..

declare @check varchar(50)
set @check = 'Bad'
declare @select varchar(50)
set @select = 'select * from art where style like %' + @check + ' and
inventoryid =13'

exec (@select)
how can I make this work for either like statements?

thanks

-Jim


You don't need dynamic SQL here:

select *
from art
where style like '%' + @check
and inventoryid = 13

One possible solution is to use an additional variable to indicate whether
or not to use both % delimiters:

declare @full bit
set @full = 1 -- 1 for both delimiters, 0 for one

select *
from art
where style like '%' + case @full when 1 then @check + '%' else @check end
and inventoryid = 13

If you have a lot of similar queries, you might want to look at using
full-text searching, which may be more efficient - MSSQL can't use an index
on the style column to help with the queries above.

Simon
Jul 20 '05 #2
On 31 May 2004 00:13:07 -0700, Jim wrote:
I need to build a bunch of dynamic like statements '% ' or '% %'.

heres an example of one that im working on just to test..

declare @check varchar(50)
set @check = 'Bad'
declare @select varchar(50)
set @select = 'select * from art where style like %' + @check + ' and
inventoryid =13'

exec (@select)
how can I make this work for either like statements?

thanks

-Jim


Hi Jim,

The query you build misses some quotes. There are two ways to solve this.

1. Without dynamic SQL (preferred, unless you have a very good reason to
use dynamic SQL):

declare @check varchar(50)
set @check = 'Bad'
select * from art where style like '%' + @check + and inventoryid =13
2. With dynamic SQL:

declare @check varchar(50)
set @check = 'Bad'
declare @select varchar(50)
set @select = 'select * from art where style like ''%' + @check + ''' and
inventoryid =13'

exec (@select)
Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: David Lozzi | last post by:
OK, I know how to use to pull up all records that contains the keyword, but what about not containing the keyword? I've tried NOT LIKE but i get Arguments are of the wrong type, are out of...
1
by: ITHELP | last post by:
I am using a form to find some data from a table. In the table are fields such as Client name, job number, and job description. I built a query using a SQL statement through the 'Record source' for...
4
by: Tom Walker | last post by:
I cannot get the WHERE statement to work correctly unless I use a literal with the LIKE. I want to use a working storage data name so that I can vary the WHERE statement. Example that works: ...
18
by: Bill Smith | last post by:
The initial row is inserted with the colPartNum column containing a valid LIKE pattern, such as (without the single quotes) 'AB%DE'. I want to update the column value with the results of a query...
9
by: Peter | last post by:
My problem is the last bit of coding below, the like statement does not work. what I have is a product options field and in it is stored characters i.e. "avcy" etc what the query does is...
5
by: Andreas Müller | last post by:
Hi, I was wondering, if there is something similar in VB.NET like the using statement in C#. What it does is to automatically call Dispose on the object decrared with in the statement when the...
26
by: brenocon | last post by:
Hi all -- Compared to the Python I know and love, Ruby isn't quite the same. However, it has at least one terrific feature: "blocks". Whereas in Python a "block" is just several lines of...
3
by: Clearview | last post by:
This is my first post to the forum and hoping I can get help as I have spent a way too much time on this problem, and thinking it is probably something really simple to resolve. I am using Access...
6
by: vasudevram | last post by:
Hi group, Question: Do eval() and exec not accept a function definition? (like 'def foo: pass) ? I wrote a function to generate other functions using something like eval("def foo: ....") but...
2
sharijl
by: sharijl | last post by:
Can someone explain the correct way to query SQL with a like or statement. so I want to SELECT * from table where column like 'Something%' or like 'somethingelse%' I tried it and get syntax...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.