Search code examples
sqldb2cobolmainframe

how to accept part of variable(string) as dynamic value in cobol?


I have a field in working storage section WS_CONTRACT_NUM , this has few hard-coded values of leght 15 characters.But first 5 characters could be anything, my question is how can i declare this variable in that case.

WORKING STORAGE
 01 WS_CONTRACT_NUM PIC X(15) VALUE '?????9999999999' --- > instead of '?' what can i give so that dynamic value can be substituted in my query.

And i am performing select query like this :

EXEC SQL
  SELECT * FROM TABLE WHERE CONTRACT_NUM = :WS_CONTRACT_NUM
END-EXEC

Thanks in advance!


Solution

  • You're looking for SQL LIKE, see IBM docs, where

    The underscore character (_) represents any single character. The percent sign (%) represents a string of zero or more characters.

    To use this in COBOL you'd either

    MOVE '_____9999999999' TO WS_CONTRACT_NUM 
    EXEC SQL
      SELECT * FROM TABLE WHERE CONTRACT_NUM LIKE :WS_CONTRACT_NUM
    END-EXEC
    

    or (less likely that you want to do it)

    EXEC SQL
      SELECT * FROM TABLE WHERE CONTRACT_NUM LIKE '%9999999999'
    END-EXEC
    

    or maybe (I have no option to tests this, please report back)

    MOVE '%9999999999' TO WS_CONTRACT_NUM
    *> this may search for the trailing spaces,
    *> if it does define a smaller variable
    EXEC SQL
      SELECT * FROM TABLE WHERE CONTRACT_NUM LIKE :WS_CONTRACT_NUM
    END-EXEC