Problem solve Get help with specific problems with your technologies, process and projects.

Counting characters in COBOL

How do I define the length of the query? Is there any predefined function that calculates the length of the string in COBOL?

I am using dynamic embedded SQL with my input in JCL to prepare a plan. I am supposed to use the varchar data type definition in COBOL. So I need to calculate the length of the query, which I am giving as my input. For example, if my query is
select ename,eno,esal,edept from emp where edept='d001'

There is a LENGTH function in COBOL, but it will not be helpful in solving your problem. All LENGTH will do is return the maximum size of a character string -- and you should already know that.

The problem boils down to one of parsing and counting based on your particular parameters. For example, in the SQL your provided above, the answer I'm expecting you want to receive is 55, because that is the total number of characters starting with the "s" in select and ending with the closing single quotation. So you could write your own counting routine that just counts the characters until two spaces are found in succession. Of course, that might not work for you because you can write your SQL statement many different ways, including with many embedded spaces. For example:

select ename, eno, esal, edept  from emp where edept='d001'

I put a space after each comma, but also an extra space after "edept" but before the "from" clause. I am guessing that you would want the function to return 59 in this case, but if you just counted until reaching two spaces you'd get 30 -- definitely the wrong answer.

I suppose you could code a routine to count backward through the text string and stop as soon as you get a non-space. Then you'd add one (for the non-space) and subtract the number from the maximum length.

Otherwise, unless you can put some rules around the SQL you are formulating, I don't know of a reasonable way to go about this task.

Good luck.

Dig Deeper on IBM system z and mainframe systems