473,394 Members | 1,870 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.

Newbie to T-SQL (and can't get my if..else to work in WHERE clause)

bencoding
Hello, I'm new to this board and new to T-SQL,

I have a project at my work that is requiring me to modify an existing stored procedure to add more functionality.

How can I use a CASE or IF...ELSE (preferably a single IF) statement for this scenario?

if @report_field_value is equal to 0 then leave that where clause out completely else if it is a number greater than 0 then use the where clause of WHERE report_field_value = @report_field_value

Thx
Ben
Mar 20 '08 #1
4 2065
ck9663
2,878 Expert 2GB
Hello, I'm new to this board and new to T-SQL,

I have a project at my work that is requiring me to modify an existing stored procedure to add more functionality.

How can I use a CASE or IF...ELSE (preferably a single IF) statement for this scenario?

if @report_field_value is equal to 0 then leave that where clause out completely else if it is a number greater than 0 then use the where clause of WHERE report_field_value = @report_field_value

Thx
Ben
Try this:


Expand|Select|Wrap|Line Numbers
  1. WHERE report_field_value = case when @report_field_value = 0 then report_field_value else  @report_field_value end
  2.  
-- CK
Mar 20 '08 #2
Try this:


Expand|Select|Wrap|Line Numbers
  1. WHERE report_field_value = case when @report_field_value = 0 then report_field_value else  @report_field_value end
  2.  
-- CK

yes, I have seen where you can add the CASE statement right after the "=, equal" operator but what I really need is to leave the WHERE clause out all together if @report_field_value = 0.

But actually I think what you provided will still work I didn't think about that, thanks.
Mar 20 '08 #3
ck9663
2,878 Expert 2GB
Option 1: Since the where condition is always true for all records, it's as if there's not where at all.

Option 2: Create a dynamic sql.

-- CK
Mar 20 '08 #4
Option 1: Since the where condition is always true for all records, it's as if there's not where at all.

Option 2: Create a dynamic sql.

-- CK

Thanks for the alternative option, I'm not a big fan of dynamic sql though, I have heard it can slow down some larger sp's. Not to mention not as easy to follow or debug. Option 1 seems to work perfectly for me. Thanks, Ben
Mar 21 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Elger | last post by:
Dear Members, A newbie question How do I convert this XML example into HTML (using XSLT)? <DOCUMENT> <PARA> This <BOLD>is</BOLD> a <BOLD>test</BOLD> </PARA> </DOCUMENT>
3
by: lrheeza | last post by:
Hello, i am a newbie at ms access, please help this is a code i created to be able to get the multiple users i have selected in the form. i was able to select the users in the form an select...
16
by: Raxit | last post by:
Hi, i was reading/learning some hello world program in python. I think its very simillar to Java/C++/C#. What's different (except syntax) ? what can i do easily with python which is not easy...
1
by: Ole Hansen | last post by:
Hi ! I am trying to learn how to use Oracle Discoverer. I have worked with databases, like Oracle for several years, but i am new to Discoverer. Does anybody know a place to download an...
2
by: r_ahimsa_m | last post by:
Could you recommend me some free JavaScript validator? I was using JSlint but it reports nonsense errors. Please help. Thanks. /RAM/
5
by: SharkD | last post by:
Hi! I'm a total newbie when it comes to ASP (or any type of server-side programming). I want to start a project that does the following: 1. query Wikipedia for information regarding...
12
by: Atropo | last post by:
Hi all. Having several strings how do i combine them to construct a command; lets say to run the date command. string str = "14/10/08 19:06:09"; strDD = str.substr(0,2); strMM =...
1
by: KevinCarb | last post by:
Hi, I would like to get my site to go to www.mydomain.com when user type domain.com (for ssl reasons). Any help is much appreciated - TOTAL NEWBIE. Regards, Kevin
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...

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.