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 > Field name in LIKE statement

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-14-06, 01:00
dasmidbforum dasmidbforum is offline
Registered User
 
Join Date: Sep 2006
Posts: 1
Field name in LIKE statement

Hi,

I have come across an SQL statement today that I actually thought was impossible. I always thought that a LIKE statement must use a literal value such as field1 like '%ABC'. But today I came across a subselect that used a like statement

select * from TABLE_A
where exists (select 'X' from TABLE_B where TABLE_B.f1 like '%'+TABLE_A.f2+'%')

As I said I didn't think you could use a field in a like statement. This works in SQL Server,,, could people tell me if this would function for other databases.

Thanks
Dave
Reply With Quote
  #2 (permalink)  
Old 09-14-06, 01:07
Littlefoot Littlefoot is offline
Lost Boy
 
Join Date: Jan 2004
Location: Croatia, Europe
Posts: 3,629
Yes; no problem in Oracle.
Code:
SQL> SELECT * FROM EMP e
  2  WHERE EXISTS (SELECT NULL FROM DEPT d
  3                WHERE d.deptno LIKE '%' || e.sal || '%');

     EMPNO ENAME      JOB              MGR HIREDATE        SAL     DEPTNO
---------- ---------- --------- ---------- -------- ---------- ----------
      7839 KING       PRESIDENT            17.11.81         50         10

SQL>
Reply With Quote
  #3 (permalink)  
Old 09-14-06, 05:40
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
also no problem in mysql --
Code:
 where TABLE_B.f1 like concat('%', TABLE_A.f2, '%')
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 09-14-06, 06:36
Tyveleyn Tyveleyn is offline
Registered User
 
Join Date: Aug 2006
Location: The Netherlands
Posts: 248
In Informix it can be done the same way as previously described for Oracle... Looks like standard SQL to me.
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