Solved

informix sql select statement manipulate string argument

Posted on 2010-09-15
6
1,710 Views
Last Modified: 2013-12-24
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
Comment
Question by:KeithMcElroy
  • 3
  • 3
6 Comments
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 33697830
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
 

Author Comment

by:KeithMcElroy
ID: 33702270
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
 

Author Comment

by:KeithMcElroy
ID: 33702305
I still see an issue. What you brought up about the 1 and 16 false positives.
Hmm.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 33702731
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
 

Author Closing Comment

by:KeithMcElroy
ID: 33703010
many thanks!
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 33703168
Glad to have helped!
Best regards and happy coding,
Kevin
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Left Join with Tuple returning more rows 10 89
Pivot not using aggregate yield error 3 41
mysql joining from the same table 6 29
append to an ms access field 6 16
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now