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

Need to find the column name.


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

New User


Joined: 11 Oct 2008
Posts: 74
Location: Chicago, IL

PostPosted: Thu Jan 20, 2011 5:08 pm
Reply with quote

Hello,
I am running the following query

Select *
From Tab1
Where
Col1="XYZ" And
(Col2 like :ws-find or
Col3 like :ws-find or
Col4 like :ws-find or
Col5 like :ws-find or
Col6 like :ws-find or
Col7 like :ws-find)

I need to find the column name from which condition it is picking the data..
That is like which column is having ws-find.
COL2/COL3/COL4/COL5/COL6/COL7.

Also the length of ws-find is not fixed.
Its PIC X(10) but data may be of 3/4/5... character.

Thanks in Advance.
Back to top
View user's profile Send private message
Kjeld

Active User


Joined: 15 Dec 2009
Posts: 365
Location: Denmark

PostPosted: Thu Jan 20, 2011 8:25 pm
Reply with quote

I am thinking of something like this (not tested, just an idea):
Code:

SELECT 'COL2',COL1,COL2,.. COLn
FROM TAB1
WHERE COL1 = 'XYZ' AND COL2 LIKE :WS-FIND
UNION
SELECT 'COL3',COL1,COL2,.. COLn
FROM TAB1
WHERE COL1 = 'XYZ' AND COL3 LIKE :WS-FIND
.
.
UNION
SELECT 'COL7',COL1,COL2,.. COLn
FROM TAB1
WHERE COL1 = 'XYZ' AND COL7 LIKE :WS-FIND


You can then test on the first column which query returned your row, if any. You might get more than one row back if more than one column satisfied the query.

I think a solution with some logic in the column list could do the trick, but I will let you do your own lookup for that in the SQL manual, and then come back if you have questions.
Back to top
View user's profile Send private message
GuyC

Senior Member


Joined: 11 Aug 2009
Posts: 1281
Location: Belgium

PostPosted: Fri Jan 21, 2011 3:52 pm
Reply with quote

Something like this
Code:
select
col1, case when col1 like ? then 'TRUE' else 'FALSE' end as Col1_cond,
col2, case when col2 like ? then 'TRUE' else 'FALSE' end as Col2_cond,
col3, case when col3 like ? then 'TRUE' else 'FALSE' end as Col3_cond,
col4, case when col4 like ? then 'TRUE' else 'FALSE' end as Col4_cond,
from table1
where ...
would avoid the possible n index-/tablescans of previous solution
and only returns each row only once.
Back to top
View user's profile Send private message
Kjeld

Active User


Joined: 15 Dec 2009
Posts: 365
Location: Denmark

PostPosted: Fri Jan 21, 2011 4:15 pm
Reply with quote

Yes, exactly wthat I was thinking of, but I would have to look the syntax up in the manual, not having done much work on this kind of statements.

And then I thought, better let the OP look it up first. But there it is, on a silver plate!
36_16_15.gif
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 Replacing 'YYMMDD' with date, varying... SYNCSORT 3
No new posts To find whether record count are true... DFSORT/ICETOOL 6
No new posts How to load to DB2 with column level ... DB2 6
No new posts RC query -Time column CA Products 3
No new posts Find the size of a PS file before rea... COBOL Programming 13
Search our Forums:

Back to Top