Results 1 to 7 of 7
  1. #1
    Join Date
    Jan 2003
    Posts
    1,605

    Unanswered: 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

  2. #2
    Join Date
    Jun 2003
    Location
    Toronto, Canada
    Posts
    5,516
    Provided Answers: 1
    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%%'"

  3. #3
    Join Date
    Jan 2003
    Posts
    1,605
    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

  4. #4
    Join Date
    Jun 2003
    Location
    Toronto, Canada
    Posts
    5,516
    Provided Answers: 1
    Try
    db2 "SELECT * FROM ADMIN.MYTABLE WHERE COL1 LIKE '^%LANG^%'"

  5. #5
    Join Date
    Jan 2003
    Posts
    1,605
    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

  6. #6
    Join Date
    Jun 2003
    Location
    Toronto, Canada
    Posts
    5,516
    Provided Answers: 1
    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.

  7. #7
    Join Date
    Jan 2003
    Posts
    1,605
    n_i, thanks a lot. It is working fine.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •