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 > How to escape percent character in db2cmd?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-28-10, 04:11
grofaty grofaty is offline
Registered User
 
Join Date: Jan 2003
Posts: 1,570
How to escape percent character in db2cmd?

Hi,
I use db2cmd in DB2 v9.5 FP2a on Windows.

I did the following:
1. Open DB2 Command Window (db2cmd).
2. Set LANG variable with command: set LANG=en
3. Echo variable to see if it is set: echo %LANG%
Echo command should display "en".
4. Connect to database: db2 "CONNECT TO mydatabase"
5. Create table: db2 "CREATE TABLE ADMIN.MYTABLE (COL1 CHAR(10))"
6. Insert data: db2 "INSERT INTO ADMIN.MYTABLE VALUES ('LANG')"
7. Make select: db2 "SELECT * FROM ADMIN.MYTABLE WHERE COL1 LIKE '%LANG%'
Note: You see %LANG% is a variable in Windows command prompt, so instead of %LANG% value 'en' gets inserted.

8. Work-around on cmd I have deleted variable with command: set LANG=
9. If I re-execute select from step 7 and I get data from database.

The problem is I need LANG variable to be set to en, to make it possible to some of the program's batch (and some other programs) work correctly.

Question: How to escape %LANG% variable?
Thanks
Reply With Quote
  #2 (permalink)  
Old 07-28-10, 09:30
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
I think you need to double up the "%" sign to tell the Windows command shell to ignore it:

db2 "SELECT * FROM ADMIN.MYTABLE WHERE COL1 LIKE '%%LANG%%'"
Reply With Quote
  #3 (permalink)  
Old 07-29-10, 01:22
grofaty grofaty is offline
Registered User
 
Join Date: Jan 2003
Posts: 1,570
Hi,
this helps if command is run from batch file for example to write to file:
db2 "SELECT * FROM ADMIN.MYTABLE WHERE COL1 LIKE '%%LANG%%'"
and then save it with myfile.bat and run it from command prompt.

But if I execute above command directly from command prompt then above double percent command does not work.

Any other idea?
Regards
Reply With Quote
  #4 (permalink)  
Old 07-29-10, 07:59
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Try
db2 "SELECT * FROM ADMIN.MYTABLE WHERE COL1 LIKE '^%LANG^%'"
Reply With Quote
  #5 (permalink)  
Old 07-30-10, 01:12
grofaty grofaty is offline
Registered User
 
Join Date: Jan 2003
Posts: 1,570
n_i, I have tried from command prompt and select returns empty result, the same if run from bat file.

As I see the only working solution without any hassle is to create ascii file and write:
SELECT * FROM ADMIN.MYTABLE WHERE COL1 LIKE '%LANG%';
save the file e.g. myfile.sql and run it using command:
db2 -tf myfile.sql

It is really interesting how Windows XP are primitive using command prompt comparing to some Linux distribution.
Thanks
Reply With Quote
  #6 (permalink)  
Old 07-30-10, 08:29
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
It works if you don't quote the SQL statement:
Code:
D:\>db2 -v select count(1) from syscat.tables where tabname like '^%PATH^%'
select count(1) from syscat.tables where tabname like '%PATH%'

1
-----------
          3

  1 record(s) selected.
Reply With Quote
  #7 (permalink)  
Old 08-03-10, 07:39
grofaty grofaty is offline
Registered User
 
Join Date: Jan 2003
Posts: 1,570
n_i, thanks a lot. It is working fine.
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