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 > SQL Help

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-27-07, 14:40
Db2Rookie Db2Rookie is offline
Registered User
 
Join Date: Dec 2006
Posts: 3
SQL Help

I also have tables like I_TABLE_A, I_TABLE_B in a database. I need a SQL to get the list of tables starting with "I_"

Thanks a million in advance
Reply With Quote
  #2 (permalink)  
Old 01-27-07, 15:29
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
I know you probably are looking for the escape character, but this might also work:

Code:
select tabschema, tabname
from syscat.tables
where type = 'T'
and tabname between 'I_         ' and 'I_ZZZZZZZZZ'
If you are working on DB2 for z/OS (EBCDIC) then you might have to substitute 9's for the Z's
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #3 (permalink)  
Old 01-29-07, 01:23
vini_srcna vini_srcna is offline
Registered User
 
Join Date: May 2006
Posts: 82
How about this:

SELECT CREATOR,NAME FROM SYSIBM.SYSTABLES
WHERE TYPE = 'T' AND NAME LIKE 'I_%'
Reply With Quote
  #4 (permalink)  
Old 01-29-07, 08:33
umayer umayer is offline
Registered User
 
Join Date: Dec 2005
Posts: 273
Quote:
Originally Posted by vini_srcna
SELECT CREATOR,NAME FROM SYSIBM.SYSTABLES
WHERE TYPE = 'T' AND NAME LIKE 'I_%'
This query doesn't work, as the underscore is a *joker* for any single character. You have to escape it.

... WHERE TYPE = 'T' AND NAME LIKE 'I+_%' ESCAPE '+'

should work.
Reply With Quote
  #5 (permalink)  
Old 01-29-07, 09:53
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Yet another approach:
Code:
SELECT ...
FROM  ...
WHERE SUBSTR(tabname, 1, 2) = 'I_'
Notes:
  • this will not make use of an index unless you have a generated column based on the above SUBSTR expression
  • it will fail with an error if you have tables whose names are shorter than two characters; in that case, you will need a CASE expression - expressions can be evaluated in arbitrary order so that adding a simple "AND LENGTH(tabname) >= 2" will not work
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #6 (permalink)  
Old 01-30-07, 03:09
umayer umayer is offline
Registered User
 
Join Date: Dec 2005
Posts: 273
also possible ( but with poor performance )

... WHERE LOCATE('I_' , NAME ) = 1
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