Solved

informix sql select statement manipulate string argument

Posted on 2010-09-15
6
1,735 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 60

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 60

Expert Comment

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

Featured Post

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!

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
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…

732 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