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 > Data Access, Manipulation & Batch Languages > ANSI SQL > SQL LIKE clause..help in comparing tables

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-04-03, 14:29
vivek_vdc vivek_vdc is offline
Registered User
 
Join Date: Sep 2003
Posts: 176
SQL LIKE clause..help in comparing tables

SQL experts:

I am comparing columns of two tables with a LIKE clause. I am not getting the desired result. Please help.
Here is the example.

Table A
Name........ID
B C A..........1
D E F...........2
A...............3


Table B

Name1
A

I need all records of Table A that contain alphabet A from Table B. With the query -

select * from tablea ta , tableb tb
where
ta.name like '%' +tb.name1+'%'

Only record 3 (A....3) is returned. I also need record 1 as it contains alphabet A(B C A). Note that the individual alphabets are separated by spaces.

Let me know. Thanks.

Last edited by vivek_vdc; 12-04-03 at 14:31.
Reply With Quote
  #2 (permalink)  
Old 12-04-03, 15:58
milindoke milindoke is offline
Registered User
 
Join Date: Apr 2003
Location: Atlanta
Posts: 8
i tried out with the following

TAB1
======
Id, Col1
----------
1, A
2, B C
3, A B
4, D E
5, F A G

TAB2
======
Col1
-----
A

this is the result
SQL> SELECT * FROM TAB1, TAB2
2 WHERE TAB1.col1 LIKE '%'||TAB2.col1||'%'
3 /

ID COL1 COL1
---------- ---------- ----------
1 A A
3 A B A
5 F A G A

Elapsed: 00:00:00.00
SQL>

looks like its working fine
Reply With Quote
  #3 (permalink)  
Old 12-04-03, 17:52
vivek_vdc vivek_vdc is offline
Registered User
 
Join Date: Sep 2003
Posts: 176
This is not working. It gives me incorrect syntax near |. I am executing the query in SQL Server Query Analyzer.


Quote:
Originally posted by milindoke
i tried out with the following

TAB1
======
Id, Col1
----------
1, A
2, B C
3, A B
4, D E
5, F A G

TAB2
======
Col1
-----
A

this is the result
SQL> SELECT * FROM TAB1, TAB2
2 WHERE TAB1.col1 LIKE '%'||TAB2.col1||'%'
3 /

ID COL1 COL1
---------- ---------- ----------
1 A A
3 A B A
5 F A G A

Elapsed: 00:00:00.00
SQL>

looks like its working fine
Reply With Quote
  #4 (permalink)  
Old 12-04-03, 18:05
milindoke milindoke is offline
Registered User
 
Join Date: Apr 2003
Location: Atlanta
Posts: 8
oops!

this works on Oracle. about SQL Server, no idea.

but the logic should be the same
Reply With Quote
  #5 (permalink)  
Old 12-04-03, 19:39
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
sql server string concatenation is non-standard, it uses the plus sign instead of double pipes

select * from tab1, tab2
where tab1.col1 like '%'+tab2.col1+'%'
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
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