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 > Question about LIKE operator

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-10-10, 00:05
kokokoy kokokoy is offline
Registered User
 
Join Date: May 2010
Posts: 1
Question about LIKE operator

I have a problem in my project.

I have to retrive all data which start with 11. (for example 11,112,1156)
but the provided data type of this column is INTEGER.

I tried to use LIKE but it didn't work.

Can anyone suggest me how to solve this problem?
Reply With Quote
  #2 (permalink)  
Old 05-10-10, 01:49
umayer umayer is offline
Registered User
 
Join Date: Dec 2005
Posts: 273
Try:

... WHERE VARCHAR(your_column) LIKE '11%'
Reply With Quote
  #3 (permalink)  
Old 05-10-10, 08:53
dav1mo dav1mo is offline
Registered User
 
Join Date: Dec 2007
Location: Richmond, VA
Posts: 782
what about leading 0's when converting datatype? you'll need to strip them out as well.
Dave
Reply With Quote
  #4 (permalink)  
Old 05-10-10, 12:45
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Quote:
Originally Posted by dav1mo View Post
what about leading 0's when converting datatype? you'll need to strip them out as well.
Dave
You don't need to worry about leading 0's if you are using the VARCHAR function.

Lenny
Reply With Quote
  #5 (permalink)  
Old 05-10-10, 13:34
wilsonfv wilsonfv is offline
Registered User
 
Join Date: Apr 2009
Posts: 42
Quote:
Originally Posted by Lenny77 View Post
You don't need to worry about leading 0's if you are using the VARCHAR function.

Lenny
As far as I know, there is not existing a VARCHAR function that accept integer type.
Reply With Quote
  #6 (permalink)  
Old 05-10-10, 14:04
Stealth_DBA Stealth_DBA is offline
Registered User
 
Join Date: May 2009
Posts: 472
wilsonfv, it depends and the version of DB2.

DB2 LUW V8.2 and 9.5, VARCHAR function does not accept integer as a parameter.

DB2 LUW V9.7, DB2 z/OS V8, and V9, VARCHAR funcion does accept integer as a parameter.
Reply With Quote
  #7 (permalink)  
Old 05-10-10, 16:28
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
CHAR(integer-expression) is accepted more widely,
including DB2 LUW V8.2 to 9.7, DB2 z/OS V7 to V9, DB2 iSeries V5R2 to V6R1.

CHAR(integer-expression) would be padded with trailing blanks.
But, it would be no matter using in

... WHERE CHAR(your_column) LIKE '11%'

Last edited by tonkuma; 05-10-10 at 16:33.
Reply With Quote
  #8 (permalink)  
Old 05-10-10, 18:19
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Question Performace

Quote:
Originally Posted by tonkuma View Post
CHAR(integer-expression) is accepted more widely,
including DB2 LUW V8.2 to 9.7, DB2 z/OS V7 to V9, DB2 iSeries V5R2 to V6R1.

CHAR(integer-expression) would be padded with trailing blanks.
But, it would be no matter using in

... WHERE CHAR(your_column) LIKE '11%'
Agree with you, tonkuma ! We can use Char as well as Varchar.

But if your_column is first column of index using function can affect performance.

Lenny
Reply With Quote
  #9 (permalink)  
Old 05-10-10, 18:33
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Exclamation Performance

Instead of LIKE expression you can use the query with much better performance:

Code:
with power(pwr) as
(select 1 from sysibm.sysdummy1
 union all
select pwr + 1
  from power
where pwr <= 7) 
select * from 
your_table join power
On 
your_column between 11 * power(10, pwr) and 12 * power(10, pwr + 1) - 1
or your_column  = 11
Lenny
Reply With Quote
  #10 (permalink)  
Old 05-10-10, 22:55
DB2Plus DB2Plus is offline
Registered User
 
Join Date: Jul 2009
Posts: 150
Exclamation Correction to Lenny's query

Quote:
Originally Posted by Lenny77 View Post
Instead of LIKE expression you can use the query with much better performance:

Code:
with power(pwr) as
(select 1 from sysibm.sysdummy1
 union all
select pwr + 1
  from power
where pwr <= 7) 
select * from 
your_table join power
On 
your_column between 11 * power(10, pwr) and 12 * power(10, pwr + 1) - 1
or your_column  = 11
Lenny
Lenny, your query consists the small mistake. Let me correct it:

Code:
with power(pwr) as
(select 0 from sysibm.sysdummy1
 union all
select pwr + 1
  from power
where pwr <= 7
)
select * from 
your_table join power
On 
your_column between 11 * power(10, pwr) and 12 * power(10, pwr) - 1
Thanks, Kara.
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