IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

CASE in SQL query with WHERE clause


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
mfguy01

New User


Joined: 06 Sep 2010
Posts: 30
Location: India

PostPosted: Wed Jul 31, 2013 3:11 pm
Reply with quote

Hi All,

I need help in a Embedded sql query where variable used in WHERE clause may contain either SPACES or some value.
In general, if all values are given,query will look like:-
EXEC SQL
SELECT COUNT(*) INTO :WS-ABC
FROM <TABLE T1>
WHERE A = :WS-A
AND B = :WS-B
AND C IN (SELECT DISTINCT(D) FROM T2)
.....
END-EXEC.

Here WS-A & WS-B may be SPACES or any values.
As per requirement, if particular value for WS-A & WS-B is given then that will be considered.
If no value given for WS-A & WS-B, then query should run for all values of both.

I was thinking to use CASE in where clause as:
WHERE
CASE WHEN WS-A <> SPACES
THEN A = :WS-A
ELSE 1=1
AND
CASE WHEN WS-B <> SPACES
THEN B = :WS-B
ELSE 1=1
....

1. I am not sure that CASE can be used with WHERE claues.
2. Any other solution can be there for this problem.

Please help.
Thanks in Advance.
Back to top
View user's profile Send private message
Nic Clouston

Global Moderator


Joined: 10 May 2007
Posts: 2455
Location: Hampshire, UK

PostPosted: Wed Jul 31, 2013 3:37 pm
Reply with quote

Why not do your decision making in your program which can do it quicker and submit the appropriate query so that DB2 can do only what is necessary - retrieve the data - and free up its resource that much faster?
Back to top
View user's profile Send private message
sushanth bobby

Senior Member


Joined: 29 Jul 2008
Posts: 1020
Location: India

PostPosted: Wed Jul 31, 2013 3:42 pm
Reply with quote

Hey mfguy,

Case cannot be used like that, use programming logic, write 2 static queries and use if condition logic to decide.

Regards,
sushanth
Back to top
View user's profile Send private message
mfguy01

New User


Joined: 06 Sep 2010
Posts: 30
Location: India

PostPosted: Wed Jul 31, 2013 4:14 pm
Reply with quote

Hi Sushanth,

I came to use of CASE this time only and that also google search for my problem.
I am still cluseless, what needsto be done.
Pease help.
Thanks.
Back to top
View user's profile Send private message
Nic Clouston

Global Moderator


Joined: 10 May 2007
Posts: 2455
Location: Hampshire, UK

PostPosted: Wed Jul 31, 2013 4:35 pm
Reply with quote

I do not understand why you cannot understand Sushanth's reply.
You CANNOT use CASE as you were suggesting.
You need to include logic in your program:
write 2 sql queries
if one condition exec one query
else exec the other query.
Back to top
View user's profile Send private message
mfguy01

New User


Joined: 06 Sep 2010
Posts: 30
Location: India

PostPosted: Wed Jul 31, 2013 4:44 pm
Reply with quote

Hi Nic,

It is program to prepare report using data from many tables.
The query I shown as example, is same for input values but count of such queries is already more than 100 so writing 2 queries each for such scenerios will take the count 200+. It is easy in writing and I can do that but considering 'too much time' consumption in this, I was looking for short path and smart logic.

Thanks.
Back to top
View user's profile Send private message
Akatsukami

Global Moderator


Joined: 03 Oct 2009
Posts: 1788
Location: Bloomington, IL

PostPosted: Wed Jul 31, 2013 4:49 pm
Reply with quote

mfguy01 wrote:
Hi Nic,
I was looking for short path and smart logic.

Too bad, so sad. As Sushanth and Mr. Clouston have told you, there is none.
Back to top
View user's profile Send private message
mfguy01

New User


Joined: 06 Sep 2010
Posts: 30
Location: India

PostPosted: Wed Jul 31, 2013 5:05 pm
Reply with quote

Thanks you all for your valuable time.
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts COBOL -Linkage Section-Case Sensitive COBOL Programming 1
No new posts RC query -Time column CA Products 3
No new posts Dynamically pass table name to a sele... DB2 2
No new posts Query on edit primary command CLIST & REXX 5
No new posts Query on edit primary command CLIST & REXX 1
Search our Forums:

Back to Top