473,405 Members | 2,379 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,405 software developers and data experts.

use IF in WHERE clause?

Hi,
I want to buld te where clause of a select statement dynamically. But SQL2005 won't let me do this ("An object or column name is missing or empty"etc). What am I doing wrong here?

Thanks!

Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.         d.naam,
  3.         e.naam,
  4.         h.ts_week,
  5.         h.ts_jaar,
  6.         l.nummer
  7. FROM
  8.         departments d
  9.         JOIN emp_dep ON d.id=emp_dep.dep_id
  10.         JOIN employees e ON emp_dep.emp_id=e.id
  11.         JOIN hours h ON e.id=h.employee_id
  12.         JOIN locations l ON h.locatie_id=l.id
  13. WHERE
  14.         1=1
  15.         IF @employee_name !=""
  16.             AND e.naam=@employee_name
  17.         IF @department_name!="" 
  18.             AND d.naam=@department_name
  19.         IF @timesheet_week !=""
  20.             AND h.ts_week=@timesheet_week
  21.         IF @timesheet_year !=""
  22.             AND h.ts_jaar=@timesheet_year
  23.         IF @location_nummer !=""
  24.             AND l.nummer=@location_nummer
Oct 26 '06 #1
5 33641
scripto
143 100+
Use the Case statement in the SELECT clause that's how it's done.

for example:

select case @Employee when 'SAM' THEN 1
when 'SUE' THEN 2
else '' end as EmployeeName, date, class
from mytable
Oct 26 '06 #2
Thanks! I found out how to.
Oct 26 '06 #3
Once you post a problem on a tech forum, it's very valuable to other people if you take the time to post the solution, even if you found it yourself. I see so many threads like this that end with "Never mind, I figured it out." This just creates blind alleys for people who are having the same problem.
Oct 19 '07 #4
iburyak
1,017 Expert 512MB
Try this:

Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.         d.naam,
  3.         e.naam,
  4.         h.ts_week,
  5.         h.ts_jaar,
  6.         l.nummer
  7. FROM
  8.         departments d
  9.         JOIN emp_dep ON d.id=emp_dep.dep_id
  10.         JOIN employees e ON emp_dep.emp_id=e.id
  11.         JOIN hours h ON e.id=h.employee_id
  12.         JOIN locations l ON h.locatie_id=l.id
  13. WHERE
  14.        e.naam like '%' + @employee_name
  15.        AND d.naam like % + @department_name
  16.        AND h.ts_week like % + @timesheet_week
  17.        AND h.ts_jaar like '%' + @timesheet_year
  18.        AND l.nummer like '%' + @location_nummer
Let's examine this:
e.naam like '%' + @employee_name

In this case if @employee_name = ""
e.naam like '%' + ""
the same as

e.naam like '%' - means everything

If @employee_name = "poolman"
e.naam like '%' + "poolman"
the same as
e.naam like '%poolname"
So if name is passed it will be found and if nothig passed all namew will be returned.

Good Luck.
Oct 19 '07 #5
Just want to say Serutan, I agree .. I stumbled across this thread looking for answers. And like so many other threads that just fizzle out with "S'ok, I figured it out"... this one left me frustrated. I had a similar problem and for others with the same problem as this.. the solution is to use single quotes - not double quotes. In the xample above..

IF @employee_name !="" ... should be ... IF @employee_name !=''

In my case it was declaring tsql variables .. like

@Postcode varchar(10) = "" ... when it should be ... @Postcode varchar(10) = '',

Hope this helps other people as its not an obvious one to spot (I wasn't even writing an insert query so SQL error message really didn't help me!)

Cheers
Feb 25 '08 #6

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

Similar topics

5
by: malcolm | last post by:
Example, suppose you have these 2 tables (NOTE: My example is totally different, but I'm simply trying to setup the a simpler version, so excuse the bad design; not the point here) CarsSold {...
2
by: aj70000 | last post by:
This is my query select ano,max(date),a_subject from MY_TAB where table_name='xyz' and ano=877 group by a_subject,ano order by a_subject ANO max(Date) A_Subject 877 2005-01-20...
7
by: JJ_377 | last post by:
Can someone tell me why SQL seems to ignore my order by clause? I tried to run through the debugger, but the debugger stops at the select statement line and then returns the result set; so, I have...
27
by: Chris, Master of All Things Insignificant | last post by:
I have come to greatly respect both Herfried & Cor's reponses and since the two conflicted, I wanted to get some clarification. My orginal post: Herfried, maybe your example here can get you to...
3
by: Sean Shanny | last post by:
To all, We are running postgresql 7.4.1 on an G5 with dual procs, OSX 10.3.3 server, 8GB mem, attached to a fully configured 3.5TB XRaid box via fibre channel. I think we have run into this...
26
by: GreatAlterEgo | last post by:
Hi, This is my query which is embedded in a COBOL program. EXEC SQL SELECT DATE, AGE, DURATION, AMT INTO :LDATE, :L.AGE, :L.DURATION, :L.AMT FROM TAB1 WHERE CODE = :KEY.CODE AND...
25
by: metaperl.etc | last post by:
A very old thread: http://groups.google.com/group/comp.lang.python/browse_frm/thread/2c5022e2b7f05525/1542d2041257c47e?lnk=gst&q=for+else&rnum=9#1542d2041257c47e discusses the optional "else:"...
2
by: Jim.Mueksch | last post by:
I am having a problem with using calculated values in a WHERE clause. My query is below. DB2 gives me this error message: Error: SQL0206N "APPRAISAL_LESS_PRICE" is not valid in the context where...
5
by: pwiegers | last post by:
Hi, I'm trying to use the result of a conditional statement in a where clause, but i'm getting 1)nowhere 2) desperate :-) The query is simple: -------- SELECT idUser,...
6
by: jackal_on_work | last post by:
Hi Faculties, I have two queries which give me the same output. -- Query 1 SELECT prod.name, cat.name FROM products prod INNER JOIN categories cat ON prod.category_id = cat.id WHERE cat.id...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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
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
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
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,...
0
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...

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.