473,405 Members | 2,415 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,405 software developers and data experts.

Creating WHERE clauses based on IF (or CASE) STATEMENTS Transact-SQL

I am trying to create a stored procedure whose where clause is
dependent on a parameter.

If the parameter @myparam is null or '' then I want the where clause to
be one thing, else I want it to be a completely different thing. I can
do it easily using iif but obviously that is not an option here...

Here is what I have:

Select @ FROM my table WHERE

IF @myparam = '' or @myparam is null
[MyField] = like '%'
ELSE
charindex(@myparam,[MyField]) > 0)

I have also tried variations of this and tried it using CASE. I keep
getting syntax errors. Do I have to write multiple SELECT statements
for every possibility. If I can get this working the query will
actually be a bit more complex, although it will follow this basic
structure...in other words, I'd rather not have to write multiple
queries when the only thing that is changing is the WHERE portion...

Thanks!
Ryan

Nov 23 '05 #1
3 3579
How about this:

if @myparam = '' or @myparam is null
@myparam = '%'
else
@myparam = '%' + @myparam + '%'

select ...
from mytable
where MyField like @myparam

Nov 23 '05 #2
>How about this:

if @myparam = '' or @myparam is null
@myparam = '%'
else
@myparam = '%' + @myparam + '%'

select ...
from mytable
where MyField like @myparam


Actually, now that I've thought about it more, it would be silly (and
wasteful) to pattern match to everything. This might be better:

select ...
from mytable
where (@myparam = '' or @myparam is null
or charindex(@myparam,[MyField]) > 0))

Nov 23 '05 #3
I want you to know that you are my new hero :) Thank you SOOOOOO much.
I can now home for the night...

Nov 23 '05 #4

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

Similar topics

1
by: db2sysc | last post by:
All. We have LOT of variables declared in the Oracle package as ORACLE CONSTANTS like, v_test CONSTANT INTEGER=1; When converting to DB2, MTK changes each of these CONSTANTs into...
3
by: Fanofmsu | last post by:
Hi - from my limited expereince in DB2, it appears that you can't include a case statement in a group by clause - is this correct? For example, my SQL looked like this: SELECT CASE WHEN...
2
by: Stephen Zachmann | last post by:
Ok, I want to have a directory full of pdf files and a page that creates links to them dynamically so that if I add 10 docs or subtract 7 or whatever, they will be added or removed the next time...
5
by: alacrite | last post by:
If I have code like this: int main() { a=1; b=2; x=0; switch(a) {
0
by: java7man | last post by:
All - below is part of a construct I am using. I have some very complex data and processing to perfrom and I need a way to get beyond case limitations. Anyone know of other more robust constructs...
6
by: Fir5tSight | last post by:
Hi All, I have a "SELECT" statement in a stored procedure that looks like the follows: ...
1
by: poops2468 | last post by:
I have about 100 functions that each have a Select Case statement as their main driver. I need to retrieve the number of actual Case statements within each file. How can I do this? Thanks.
1
by: anishap | last post by:
Can anyone help me with the case statements? I have customized a new report for our application (vendor software). I want the end user to check 3 boxes to print (they can choose all or one or two)....
6
by: kronecker | last post by:
Is there any way to make case statements case independent? ie Case Is = "dim the light" or Case Is = "Dim the light" making them the same outcome without putting every possibility. eg ...
1
by: weefrenchie | last post by:
Hi, I have a SQL query that looks like this: , CASE WHEN object 1 LIKE '%first%' OR object 1 LIKE '%second%' OR object 2 LIKE '%first%' OR object 2 LIKE '%second%' THEN object 3...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.