473,883 Members | 2,395 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

bencoding
22 New Member
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_v alue 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_va lue = @report_field_v alue

Thx
Ben
Mar 20 '08 #1
4 2083
ck9663
2,878 Recognized Expert Specialist
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_v alue 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_va lue = @report_field_v alue

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
bencoding
22 New Member
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_v alue = 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 Recognized Expert Specialist
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
bencoding
22 New Member
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
1581
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
1406
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 folder however i am unable to save the user name to the table. i am able to save the folder name. what should i do? Dim vtr As Variant
16
1898
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 in c++/java !? Tnx, Raxit
1
4371
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 "Oracle Discoverer for Dummies ...", or some other good tutorial for a newbie like me.
2
1258
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
2158
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 articles using the
12
2020
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 = str.substr(3,2); strAA = str.substr(6,2); strhh = str.substr(9,2);
1
1682
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
9953
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10768
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10422
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9591
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7984
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7137
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5808
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4623
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3242
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.