[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

informix sql select statement manipulate string argument

Posted on 2010-09-15
6
Medium Priority
?
1,770 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 60

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
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 
LVL 60

Accepted Solution

by:
Kevin Cross earned 2000 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 60

Expert Comment

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

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

656 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