If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Selecting values based on other column

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-24-09, 09:01
laknar laknar is offline
Registered User
 
Join Date: Jul 2008
Posts: 80
Selecting values based on other column

Code:
--------------------------------------------------
-- Create Table TEST
--------------------------------------------------
Create table TEST (
    COLUMN1                            VARCHAR(50)                     ,
    COLUMN2                            VARCHAR(50)                     
     ) 
in USERSPACE1   ;


insert into TEST values ('A','A,B');
insert into TEST values ('B','B,C');

select * from where Column1 in(''''||REPLACE(column2,',',''',''')||'''');

how to select records based on column2

note:- Column2 contains list of values seperated by comma

but its giving the below error.

SQL0401N The data types of the operands for the operation "IN" are not compatible. SQLSTATE=42818
Reply With Quote
  #2 (permalink)  
Old 03-24-09, 09:57
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Try the LOCATE function.

Andy
Reply With Quote
  #3 (permalink)  
Old 03-24-09, 22:57
laknar laknar is offline
Registered User
 
Join Date: Jul 2008
Posts: 80
column1 column2
a a,b,c,d,e
b f,g,h,i,j,k

i used locate and it worked for two values

select * from test where Column1 in(LEFT(COLUMN2,LOCATE(',',COLUMN2)-1),RIGHT(COLUMN2,LOCATE(',',COLUMN2)-1))

how can i select when column2 contains multiple values seperated by comma

really dont know how many values seperated by comma.

i have to give during runtime.
Reply With Quote
  #4 (permalink)  
Old 03-25-09, 01:13
nick.ncs nick.ncs is offline
Registered User
 
Join Date: May 2007
Location: somewhere in dbforums
Posts: 221
Quote:
Originally Posted by laknar
i used locate and it worked for two values
That is because LOCATE function returns the location of the first occurrence of the matching string

Quote:
Originally Posted by laknar
how can i select when column2 contains multiple values seperated by comma
In such a scenario what should be the output if there are multiple occurrences. An example would help us in a long way.
__________________
IBM Certified Database Associate, DB2 9 for LUW
Reply With Quote
  #5 (permalink)  
Old 03-25-09, 03:21
umayer umayer is offline
Registered User
 
Join Date: Dec 2005
Posts: 273
I think, ARWinner meant something like:

SELECT * FROM test
WHERE LOCATE (',' CONCAT COL1 CONCAT ',' , ',' CONCAT COL2 CONCAT ',') > 0
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On