473,322 Members | 1,523 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,322 software developers and data experts.

Using IF inside SELECT ?

Is there possibility to use IF conditions inside SELECT statements?

For example, can i write something like this:
CREATE PROCEDURE [search]
(
@OPTION int,
@KEYWORD nvarchar(40)
)
AS
BEGIN
SELECT id FROM projects WHERE title LIKE @KEYWORD IF (@OPTION = 1)
THEN (OR description LIKE @KEYWORD)
END

or am i limited to this:
....
BEGIN
IF @OPTION = 1
SELECT id FROM projects WHERE title LIKE @KEYWORD OR description LIKE
@KEYWORD
ELSE
SELECT id FROM projects WHERE title LIKE @KEYWORD
END

Apr 19 '06 #1
3 12605
On 19 Apr 2006 07:07:47 -0700, Igor wrote:
Is there possibility to use IF conditions inside SELECT statements?

For example, can i write something like this:
CREATE PROCEDURE [search]
(
@OPTION int,
@KEYWORD nvarchar(40)
)
AS
BEGIN
SELECT id FROM projects WHERE title LIKE @KEYWORD IF (@OPTION = 1)
THEN (OR description LIKE @KEYWORD)
END

or am i limited to this:
...
BEGIN
IF @OPTION = 1
SELECT id FROM projects WHERE title LIKE @KEYWORD OR description LIKE
@KEYWORD
ELSE
SELECT id FROM projects WHERE title LIKE @KEYWORD
END


Hi Igor,

The latter will probably perform best. But if you prefer to have it in
one query, you can also use

SELECT id
FROM projects
WHERE title LIKE @keyword
AND ( @option <> 1 OR description LIKE @keyword )

For more complex cases, check out the CASE expression in Books Online.

--
Hugo Kornelis, SQL Server MVP
Apr 19 '06 #2
Am 19 Apr 2006 07:07:47 -0700 schrieb Igor:
BEGIN
IF @OPTION = 1
SELECT id FROM projects WHERE title LIKE @KEYWORD OR description LIKE
@KEYWORD
ELSE
SELECT id FROM projects WHERE title LIKE @KEYWORD
END


can be done this way:

SELECT id FROM projects WHERE title LIKE @KEYWORD OR
(@OPTION = 1 AND description LIKE @KEYWORD)

bye, Helmut
Apr 19 '06 #3
On Wed, 19 Apr 2006 16:33:26 +0200, Hugo Kornelis wrote:
SELECT id
FROM projects
WHERE title LIKE @keyword
AND ( @option <> 1 OR description LIKE @keyword )


Oops, I misread the question. Use Helmut's version instead.

--
Hugo Kornelis, SQL Server MVP
Apr 19 '06 #4

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

Similar topics

3
by: r rk | last post by:
I am trying to write a utility/query to get a report from a table. Below is the some values in the table: table name: dba_daily_resource_usage_v1...
3
by: darrel | last post by:
In my XSLT, I'm trying to write out some dynamic URLs. I can't put an actual xsl:value-of field inside a URL: <a href="<xsl:value-of select="linkUrl"/>"> But I can do this: <a...
0
by: Horia Tudosie | last post by:
Using Visual Studio 2003 This is to report a series of bugs regarding the FlagsAttribute and (independently) the usage of interfaces in Web applications. Let’s declare xColors type like: ...
1
by: Max Evans | last post by:
I have a XML file, which contains itemid-elements, e.g.: <itemid>3</itemid> <itemid>12</itemid> Now I want to convert these IDs to the corresponding name via XSLT. I thought I could do it this...
3
by: sewerized | last post by:
Hello all, I'm having a problem with a user preferences form I'm creating. I want to use dynamic dropdown SELECT boxes for this so my page doesn't get cluttered with 100000 links. Since I...
1
by: Nitinkcv | last post by:
Hi, i have my button actually inside a repeater and it validates info from textboxes and ddl inside the repeater.. The prob is that i have "SELECT VALUE" chosen as the default value in my ddl. so...
13
by: cj | last post by:
I'm looking at using Using to try an plug a suspected memory leak. I've already started using dispose for everything that implements it but no luck. I have several questions about Using Can I...
4
by: dougans | last post by:
Hey there, Hope someone can help me, completely stuck with immigrating from php4 to OOP based php5. == index.php -- include(database.inc.php);
0
by: lamolap | last post by:
i have 1 gridview , a dropdownlist inside a gridview and a commandfield of (edit, update and cancel) my gidview looks like this Edit Surname Initials ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.