Solved

informix sql select statement manipulate string argument

Posted on 2010-09-15
6
1,701 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Database restore 8 67
how to remove non-duplicated row 5 25
MSSQL 2014 Query Synthax 8 38
SQL Query to display duplicates ? 6 23
SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
This article explains all about SQL Server Piecemeal Restore with examples in step by step manner.
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…

708 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

17 Experts available now in Live!

Get 1:1 Help Now