2

I have a query that I can't get to work in DB2/AS400.

select integer(score(ADRLIN1, :param1)*100) as RELEVANCEADR, 
ADRLIN1, PSTCOD from MYSCHEMA.MYTABLE 
where contains(ADRLIN1, :param2) = 1
and pstcod like :param3 
order by RELEVANCEADR desc

When I try to run the query above, and entering values in my parameters, I get [SQL0418] Use of parameter marker not valid.

This isn't a big issue. A google search told me to CAST the parameters prior to using them. So then I change the query to this:

select integer(score(ADRLIN1, CAST(:param1 AS CHAR))*100) as RELEVANCEADR, 
ADRLIN1, PSTCOD from MYSCHEMA.MYTABLE  
where contains(ADRLIN1, CAST(:param2 AS CHAR)) = 1
and pstcod like :param3 order by RELEVANCEADR desc

With the following values:

  • param1 --> 19 EDGEWOOD BLVD
  • param2 --> 19 EDGEWOOD BLVD
  • param3 --> %68046%

And I get an empty resultset. However, if I actually fill in the query with literals, the query works.

select integer(score(ADRLIN1, '19 EDGEWOOD BLVD')*100) as RELEVANCEADR, 
ADRLIN1, PSTCOD from MYSCHEMA.MYTABLE
where contains(ADRLIN1, '19 EDGEWOOD BLVD') = 1
and pstcod like '%68046%' 
order by RELEVANCEADR desc

The query above returns a valid record.

My question is, how can I get the score and contains functions to work with passed in parameters instead of using hard-coded strings?

5
  • You have param1 and param2 in your code. Are you passing the same string for both? Commented Feb 22, 2017 at 17:34
  • @GordonLinoff Yes, even if I replace the param2 to be param1 to ensure the same string is passed, the resultset is empty. Commented Feb 22, 2017 at 17:35
  • 4
    CHAR is equivalent to CHAR(1), so all your input values are truncated. Use the actual data type when casting. Commented Feb 22, 2017 at 17:48
  • Thanks! That worked. I changed CHAR to CHAR(50) and it worked! Commented Feb 22, 2017 at 17:51
  • 1
    One other thing that could bite you if param3 is defined as CHAR vs. VARCHAR is that a CHAR' variable is always the defined length, andc pads on the left with blanks. So if you put %68046%` in a CHAR(10) field, it is as if you used '%68046% ' and will only find rows where pstcod contains 68046 and has three blanks at the end. Commented Feb 22, 2017 at 23:21

1 Answer 1

1

As @Mustaccio pointed out in the comments, casting as a CHAR is the equivalent to CHAR(1). I corrected this to declare an actual length and the query is working.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.