Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

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, 15:29
vivek_vdc vivek_vdc is offline
Registered User
 
Join Date: Sep 2003
Posts: 174
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 15:31.
Reply With Quote
  #2 (permalink)  
Old 12-04-03, 16: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, 18:52
vivek_vdc vivek_vdc is offline
Registered User
 
Join Date: Sep 2003
Posts: 174
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, 19: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, 20:39
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 13,556
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

pre-order my book Simply SQL from Amazon
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

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