473,569 Members | 2,604 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

if else statement inside a where clause

13 New Member
Good day!!

I was wondering if it is possible to put an if else statement inside a where clause of an sql statement... you see i would like my update statement to do this:


update trial_clear set num = @count2 /* @count2 is a integer passed*/
where
if (select top 1 def from trial_clear where num is NULL) is NULL
def is NULL
else
def = @def /*given value*/

please do help me... i would appreciate it much!!
Oct 19 '07 #1
5 8176
iburyak
1,017 Recognized Expert Top Contributor
Try this:

Expand|Select|Wrap|Line Numbers
  1. update trial_clear set num = @count2 /* @count2 is a integer passed*/
  2. where def =
  3. case when (select top 1 def from trial_clear where num is NULL) is NULL Then null
  4. else @def  end
Good Luck.
Oct 19 '07 #2
clear1140
13 New Member
Try this:

Expand|Select|Wrap|Line Numbers
  1. update trial_clear set num = @count2 /* @count2 is a integer passed*/
  2. where def =
  3. case when (select top 1 def from trial_clear where num is NULL) is NULL Then null
  4. else @def  end
Good Luck.



thanks for the code but still it doesn't work, i should have made my message clear... you see i've got this table..
--table---
trial_clear(num ,def,def2,def3)
--these are the existing values--
1st row (NULL,q,NULL,NU LL)
2nd row (NULL,w,w,NULL)
3rd row (NULL,e,e,r)
4th row (t,NULL,y)
5th row (q,w,NULL)

you see what i was supposed to do is update this table, assigning the num value with int.. starting form 1 to so on...the table i presented is just a prototype of the real table

while making my procedure i noticed that it will only work if i would say 'def is NULL' not def = NULL and that was why i was wondering if there was and if else statement inside the where clause
Oct 19 '07 #3
iburyak
1,017 Recognized Expert Top Contributor
Try this:

Expand|Select|Wrap|Line Numbers
  1. set rowcount 1
  2.  
  3. update trial_clear set num = @count2 /* @count2 is a integer passed*/
  4. where isnull(def, '') = ''
  5.  
  6.  
Oct 19 '07 #4
clear1140
13 New Member
Try this:

Expand|Select|Wrap|Line Numbers
  1. set rowcount 1
  2.  
  3. update trial_clear set num = @count2 /* @count2 is a integer passed*/
  4. where isnull(def, '') = ''
  5.  
  6.  

thanks it really helped!!

But i too got another solution. I simply put the resulting rows into a cursor and it works even better than my previous code. Thanks for reminding me that there is a better way... THANK YOU!!!
Oct 20 '07 #5
ck9663
2,878 Recognized Expert Specialist
thanks it really helped!!

But i too got another solution. I simply put the resulting rows into a cursor and it works even better than my previous code. Thanks for reminding me that there is a better way... THANK YOU!!!

cursor should be your last resort...
Oct 20 '07 #6

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

Similar topics

2
188923
by: Largo SQL Tools | last post by:
Can anyone tell me if it's possible to use a Case statement in a Where clause, and if so, the proper syntax? J.R. Largo SQL Tools The Finest Collection of SQL Tools Available http://www.largosqltools.com
9
10937
by: paulmac106 | last post by:
If you could help me with my syntax i would really appreciate it. I'm trying to change the where clause on the fly, but it's giving me a syntax error. Hopefully from what I have below you can tell what i'm after the first part should evaluate a boolean expression, then if true, search using one field, otherwise search using a different...
8
2592
by: frenzic | last post by:
Hi I am a DBA - just been in the job two weeks and I would be grateful for some help. I am trying to update a table with values from another table. I have spent two days on this and I think I may have blown a fuse in my brain! The 'from' table has 3 rows in it and the 'to' table has 3094.
2
4721
by: knmadhhu | last post by:
Hi Everybody I am writing this statement,but is it hanging! Can anybody correct me? Any help will be appreciated! select (cba.billing_name1||' '||cba.billing_name2) as cust_bill_name from USRP.cust_billing_acct cba,USRP.circuit ckt,USRP.customer cust where cba.MCN = case when ckt.CKT_MCN is NOT NULL THEN ckt.CKT_MCN ELSE ( select cust.MCN...
2
3811
by: edwinjs | last post by:
Hi I am not able to execute the following query. select emp_code from employees where emp_code in (case when emp_type = 1 then select emp_code from table1 else select emp_code from table2 ...
1
2725
by: ebo2006 | last post by:
There are two combo boxes in my form: CORP and CLASS. The WHERE clause of the SELECT statement in the Row Source Property of the CLASS combo box works only every time I start the form. It does not change the list options when I select another ID from CORP combo box. Why is this so? Here is my select statement SELECT ., . FROM Courses WHERE...
15
9378
by: shlo | last post by:
I was wondering if someone would be able to tell me what is wrong with my code here: SELECT COUNT(*) FROM CALL WHERE CALL.DESCRIPTION = CASE WHEN @Param1 = 'In' THEN 'In' WHEN @Param1 = 'Out' THEN 'Out' END
1
2573
by: djpaul | last post by:
Hello, I'm trying to select a dayvalue in the sql statement depending on the openingstime. So if the shop opens on 8:00 and the time is before that i want to show the times that it's open today. If the actual time is after closing time i want to show the openingstime for the next day. This is where i came so far...: SELECT customer_id,...
0
1640
by: yshali | last post by:
I am trying to handle the NULL value passed to a parameter. if param1 = 1,2,3 query returns where param in (1,2,3) if param = NULL query returns all values. I have a split function to implement this CSV passed parameter in a single string. Can anyone help please ?
0
7700
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...
0
7614
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8125
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...
1
7676
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5513
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...
0
5219
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...
0
3653
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...
1
2114
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
0
938
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...

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.