Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1779
  • Last Modified:

informix sql select statement manipulate string argument

Working with a proprietary reporting tool connected to an ERP system.
The reporting tool allows for a user prompt (called a user field in this case with the name myvariable)

We want the user to be able to type in the following into the user field prompt:

16,17,18,20,21  

The reporting tool uses the user field myvariable as follows
SELECT * FROM mytable WHERE fielda IN(@myvariable)

The reporting tool produces the following sql which does not work because
of missing single quotes.  
SELECT * FROM mytable WHERE fielda IN('16,17,18,20,21')


The SQL needs to be this in order to work
SELECT * FROM mytable WHERE fielda IN('16','17','18','20','21')

Is there a sql string manipulation function(s) that can translate or transform the user field value (without quotes) in to the final SQL with quotes around each value?
0
KeithMcElroy
Asked:
KeithMcElroy
  • 3
  • 3
1 Solution
 
Kevin CrossChief Technology OfficerCommented:
KeithMcElroy,

You can try something like this:
+concatenate ',' to end of variable -- so that all your values are immediately followed by a comma or whatever delimiter
+concatenate ',' to your field value also
+use instr to find if the field value with delimiter is found
+we use delimiter at the end of field value to eliminate false matches like 16 = 1 because 1 is found instr, but 16, <> 1,

I am not an INFORMIX person by any stretch, but since you haven't gotten any responses and this seems like a SQL syntax issue I figure I would help.  I only mention in case I goofed on converting my thought to informix code.  Hopefully the explanation of the technique helps you resolve any type-o's.

Kevin
SELECT * 
FROM mytable 
WHERE instr(@myvariable || ',', fielda || ',') > 0
;

Open in new window

0
 
KeithMcElroyAuthor Commented:
Thank you for responding.
I did not find instr in Informix's native functions.
I wonder if I could do something with lengh and replace where I make a replace that
increments the length and thus detects the

WHERE Length(replace(@myvariable,fielda,"XXX")  > Length(@myvariable)

Any thoughts appreciated.
And maybe InStr does exist, but so far have not found.
0
 
KeithMcElroyAuthor Commented:
I still see an issue. What you brought up about the 1 and 16 false positives.
Hmm.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Kevin CrossChief Technology OfficerCommented:
I thought InStr was one of the built-in functions, but maybe that is in a newer version.  Informix is definitely not my strong suit.

If the LENGTH() and REPLACE() functions are there, you could use similar approach and:
(1)concatenate a comma to the end of @myvariable
(2)contentate a comma to the end of fielda
(3)replace the value of (2) with empty string in the value of (1)
(4)if this replaced length is less the length of (1) originally, then you had a match

Something like this:
WHERE LENGTH( REPLACE( @myvariable || ',' , fielda || ',' , '' ) ) < LENGTH( @myvariable || ',' )

Hope that helps!
0
 
KeithMcElroyAuthor Commented:
many thanks!
0
 
Kevin CrossChief Technology OfficerCommented:
Glad to have helped!
Best regards and happy coding,
Kevin
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now