Portal | Manuals | References | Downloads | Info | Programs | JCLs | Mainframe wiki | Quick Ref
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Profile Log in to check your private messages Log in
 
Need to find the column name.

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
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    Post subject: Need to find the column name.
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    Post subject:
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    Post subject:
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    Post subject:
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    IBMMAINFRAMES.com Support Forums -> DB2 All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts column with count of rows within dist... ronald wouterson DB2 4 Sun Sep 17, 2017 9:48 pm
No new posts Convert rows and column into JSON for... Dinesh Manivannan DB2 2 Sun Sep 03, 2017 6:50 pm
No new posts Select numeric portion from CHAR data... balaji81_k DB2 6 Sat Aug 19, 2017 1:51 am
No new posts Command to find TMEMBER name for a TPIPE Siva NKK Kothamasu IMS DB/DC 0 Wed Aug 16, 2017 6:08 pm
No new posts Loading data to table gives wrong for... Raghu navaikulam DB2 19 Thu Jul 13, 2017 2:11 pm

Facebook
Back to Top
 
Job Vacancies | Forum Rules | Bookmarks | Subscriptions | FAQ | Polls | Contact Us