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 > Using the LIKE clause in an Embeded SQL

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-07-04, 11:28
Thomas_thomas Thomas_thomas is offline
Registered User
 
Join Date: Feb 2004
Posts: 2
Using the LIKE clause in an Embeded SQL

How to use the like clause in an embeded SQL?
When we accsess the data using simple SQL, the following query will work.

SELECT NAME FROM TABLE WHERE NAME LIKE 'TH%'

will give THOMAS. The same query wont work when used in a COBOL program.
Reply With Quote
  #2 (permalink)  
Old 02-07-04, 12:46
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
01 user-input-name pic x(20)
01 name pic x(20).

move user-input-name to name.
examine name replacing first ' ' by '%'.

SELECT NAME FROM TABLE WHERE NAME LIKE :name
__________________
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 02-09-04, 04:19
Walter Janissen Walter Janissen is offline
Registered User
 
Join Date: Nov 2003
Location: Germany
Posts: 62
Hi

I am no COBOL expert, but I think, that Marcus_A is not correct. You have to change all trailing blanks by '%', because if you only append one '%' behind the last character, DB2 will look, in your example for a name beginning with 'TH', followed by anything, but with 17 blanks at the end (assuming your host-variable is CHAR(20)).
Reply With Quote
  #4 (permalink)  
Old 02-09-04, 09:21
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
Walter, you are correct that you are not a COBOL expert. The example I gave works fine on OS/390 COBOL with DB2 for OS/390. Trailing blanks are ignored in a LIKE predicate in this example.

I will not guarantee that the EXAMINE statement syntax is exactly how it should be used for all circumstances, but it is close enough.
__________________
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
  #5 (permalink)  
Old 02-09-04, 09:48
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
Correction.

I just looked in the version 7 manuals, and it looks like it has changed since I have used it last. Therefore Walter is correct and I was wrong.

I actually have some older COBOL programs that only use a single % with trailing blanks that worked correctly when they were written, but that was in DB2 version 3.
__________________
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
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