473,507 Members | 2,447 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

CASE: cannot test for empty string

8 New Member
Hello,
In a SELECT statement I want to add SQL to the WHERE clause depending of the value of a variable. If it is empty, do nothing. Else attach the value to the where clause.
have this
...
Expand|Select|Wrap|Line Numbers
  1. WHERE
  2.     1=1
  3.     SELECT
  4.         CASE @employee_name
  5.             WHEN !='' THEN 'AND e.naam = @employee_name'
  6.     END
  7. RETURN
  8.  
SQL 2005 won't let me do this. I get an "Incorrect syntax near !" Also operators <> and NOT don't work.If I test for equality (WHEN ='' THEN ...) then I get no errors. How can I do this in SQL 2005?
Oct 30 '06 #1
4 17706
scripto
143 New Member
Hello,
In a SELECT statement I want to add SQL to the WHERE clause depending of the value of a variable. If it is empty, do nothing. Else attach the value to the where clause.
have this
...
Expand|Select|Wrap|Line Numbers
  1. WHERE
  2.     1=1
  3.     SELECT
  4.         CASE @employee_name
  5.             WHEN !='' THEN 'AND e.naam = @employee_name'
  6.     END
  7. RETURN
  8.  
SQL 2005 won't let me do this. I get an "Incorrect syntax near !" Also operators <> and NOT don't work.If I test for equality (WHEN ='' THEN ...) then I get no errors. How can I do this in SQL 2005?
case @employee_name
when = '' Then ''
else 'AND e.naam = @employee_name'
end
Oct 30 '06 #2
NeoPa
32,557 Recognized Expert Moderator MVP
Does that mean the SELECT CASE construct only works with the ELSE clause? It must be the full construct with no optional parts?
Oct 30 '06 #3
poolman
8 New Member
case @employee_name
when = '' Then ''
else 'AND e.naam = @employee_name'
end
This is what I tried also. It gives me all rows in the table-not the one for which "AND e.naam = @employee_name". The VS2005 output window shows the parameter has a value:
Expand|Select|Wrap|Line Numbers
  1. Running [dbo].[spgetdepartmenttimesheets] ( @employeeNaam = Janssen ).
Somehow the additional WHERE clause I want to add via the CASE statement is ignored. Isn't there a way to view the generated SQL that's used to query the database so I can check what's actually being executed?
Oct 31 '06 #4
scripto
143 New Member
Hello,
In a SELECT statement I want to add SQL to the WHERE clause depending of the value of a variable. If it is empty, do nothing. Else attach the value to the where clause.
have this
...
Expand|Select|Wrap|Line Numbers
  1. WHERE
  2.     1=1
  3.     SELECT
  4.         CASE @employee_name
  5.             WHEN !='' THEN 'AND e.naam = @employee_name'
  6.     END
  7. RETURN
  8.  
SQL 2005 won't let me do this. I get an "Incorrect syntax near !" Also operators <> and NOT don't work.If I test for equality (WHEN ='' THEN ...) then I get no errors. How can I do this in SQL 2005?
Usually Case statments are in the select clause not the WHERE clause, so what you can do is place a condition like

if @employee_name <> ''
select .... where .... AND e.naam = @employee_name'
else
select .... where .....
Oct 31 '06 #5

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

Similar topics

4
2281
by: Cyrus D. | last post by:
Hi guys, What's the best way to test for an empty form value ? I am doing it like this now: $test = $_POST; if(strlen($test) < 1) // it is empty ! Maybe I can just go:
17
14596
by: Newbie | last post by:
Dear friends, I am having a hard time understanding how to use a SELECT CASE in ASP. I have used it in VB but never in ASP scripting. Scenerio: I have 2 textboxes on a form that I have to...
8
10198
by: Mike S. Nowostawsky | last post by:
I tried using the "toUpperCase()" property to change the value of an array entity to uppercase BUT it tells me that the property is invalid. It seems that an array is not considered an object when...
12
5150
by: Phoe6 | last post by:
The Program Fragment is this: int choice; /* Users Input Command */ .. .. .. printf("Enter a command: "); fflush(stdin); choice = getc(stdin); printf("\n");
15
3485
by: Benny Raymond | last post by:
I'm confused as to how fallthrough is limited in switch. For example the following works: string switch_test = "a"; switch (switch_test) { case "a": case "b": case "c": doSomething(a);
7
3168
by: Jim Carlock | last post by:
Looking for suggestions on how to handle bad words that might get passed in through $_GET variables. My first thoughts included using str_replace() to strip out such content, but then one ends...
1
4110
by: Kevin Blount | last post by:
I have a test script right now that I'm fighting with. The idea is to "simply" have an aspx page with 3 panels, to show 3 "different" forms and then a 4th panel to show the results of processing...
2
6071
by: Kevin Blount | last post by:
I have a test script right now that I'm fighting with. The idea is to "simply" have an aspx page with 3 panels, to show 3 "different" forms and then a 4th panel to show the results of processing...
4
5539
by: gerardianlewis | last post by:
Any help appreciated. (VB.NET under XP and Vista, SP1 installed) My code, inherited from a VB6 version of an app that ran under W98, loads an image from a file into a PictureBox. The user may...
0
7223
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,...
0
7321
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
7377
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...
1
7034
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...
0
7488
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
5623
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,...
1
5045
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...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
412
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...

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.