CASE Statment for USQL through ODBC in Query - sql

I am trying to write a CASE statement for a query, and it seems USQL doesn't actually have the Syntax for a CASE.
Just trying to find out if anyone knows of the correct fn to make a case statement.
Thanks

According to the documentation that I found (http://pz.southware.com/nlhtml/USQL_User_Guide.pdf) there is no reference to a CASE statement, so you're probably correct in saying that it doesn't support that.
There is the isnull function that you could use if your CASE statement condition was checking for a null value. You could maybe simulate a case by having a nested query or two, or maybe by splitting your query into multiple parts (one for each CASE condition) and then unioning them together. Not exactly elegant though...

Related

And/or SQL statement

I'm struggling to get a SQL statement to run.
I need to have an and / or statement which gives me:
Where Condition 1 is true
OR
Where both Condition 2 AND Condition 3 are true. (not only one of them)
Appreciate some ideas :)
You can split them with parenthesis, you also only need to define 'WHERE' once. Example:
WHERE
{condition_1} or ({condition_2} and {condition_3})
Edit: You don't technically require parenthesis due to AND having a higher precedence than OR, but it makes it much easier to read and see at a glance exactly what you're trying to do.

Confused on what to use, If Then or Case Else Statement

I am trying to determine if I should use a CASE Statement or an IF THEN statement to get my results.
I want a SQL statement to run when a certain condition exists, but am not certain on how to check for the condition. Here is what I am working on
IF EXISTS(SELECT source FROM map WHERE rev_num =(SELECT MAX(rev_num) from MAP <-- at this point it would return either an A or B -->
What ever the answer is i then need to run a set of SQL's. So for A it would do this set of statements and for B it would do another.
CASE is used within a SQL statement. IF/THEN can be used to choose which query to execute.
Based on your somewhat vague example it seems like you want to execute different queries based on some condition. In that case, an IF/THEN seems more appropriate.
If, however, the majority of each query is identical and you're just changing part of the query then you may be able to use CASE to reduce the amount of duplicate code.

Syntax for comparison expression in Simple case expression in Oracle DB SQL

I am trying to make a parser for Oracle SQL select statement which include CASE statement
http://docs.oracle.com/cd/B19306_01/server.102/b14200/img/case_expression.gif
And the oracle reference listed that the simple case expression can be expressed in
http://docs.oracle.com/cd/B19306_01/server.102/b14200/img/simple_case_expression.gif
But i cannot find the comparison expression everywhere in the reference, does anyone have a clue where is it and how it looks like?
I believe it's the same as the condition defined here:
http://docs.oracle.com/cd/E11882_01/server.112/e26088/conditions001.htm#SQLRF52101
which means it can be several kinds of things, requiring all of chapter 7 of the manual to explain them. And most of them in turn can include arbitrary expressions (that's all of chapter 6, plus chapter 5 for function calls) and even subqueries (so all of chapter 9 too). For eaxmple this is a "simple case statement" in action:
SELECT
CASE
WHEN (SELECT /* anything that can go in a select statement might be here */) > 0
THEN 'Y'
ELSE 'N'
END
FROM DUAL
And in the nested query you can have all kinds of hard-to-parse things. Basically if you want to parse a CASE expression the way Oracle would parse it, you're in trouble. There's no way to avoid the need to parse the rest of Oracle SQL.
If you limit yourself to standard SQL (and maybe ban subqueries) you have a better chance of finishing the project.
OK, first answer was exactly the opposite of the truth. comparison_expr is an expr that will be compared to the expr that comes right after the CASE.

SQL equivalent of 'this' ABL statement

Someone might understand this ABL statement...
FOR EACH <table> BREAK BY <field as breakgroup>:
....
IF FIRST-OF(<breakgroup>) THEN DO:
....
END.
....
IF LAST-OF(<breakgroup>) THEN DO:
....
END.
....
END.
Above code basically loops through individual records in a table and then runs some codes (....) during specific parts of the execution. Interesting to note is the FIRST-OF and LAST-OF statement where the <breakgroup> value changes and is useful for data aggregation.
Does someone know how to implement/replicate the same logic using SQL perhaps with some VBA as well?
Firstly, it is worth noting that SQL, contrary to the OpenEdge ABL, is not a 4GL. It's not supposed to 'execute code' per se, so you will have to use another programming language to execute SQL statements against a Database, retrieve the selected records and then manipulate them with whatever language you might be using.
To get the equivalent of an ABL FIRST-OF or LAST-OF statement, the SQL FIRST() and LAST() function will come in handy (First() and Last() explain those.)
Basically, that means that you will have to do at least three different SQL Select statements I think, on to select all the table entries you want, one to select the first one of a given 'group' (a group, after all, is just a subselection of entries that fulfill certain criteria) and one to select the last one of such a group. For Example
SELECT LAST(CustomerType) WHERE CustomerType = "ValuedCustomer"
Now, if you want to use VBA as your programming language, you can execute SQL statements for instance with a command like this:
DoCmd.RunSQL "SELECT LAST(CustomerType) WHERE CustomerType = 'ValuedCustomer'"
Here is the MSDN entry for that command!
Hope that helped!

SQL CASE statements on Informix - Can you set more than one field in the END section of a case block?

Using IBM Informix Dynamic Server Version 10.00.FC9
I'm looking to set multiple field values with one CASE block. Is this possible? Do I have to re-evaluate the same conditions for each field set?
I was thinking of something along these lines:
SELECT CASE WHEN p.id = 9238 THEN ('string',3) END (varchar_field, int_field);
Where the THEN section would define an 'array' of fields similar to the syntax of
INSERT INTO table (field1,field2) values (value1,value2)
Also, can it be done with a CASE block of an UPDATE statement?
UPDATE TABLE SET (field1,field2) = CASE WHEN p.id=9238 THEN (value1,value2) END;
Normally, I'd ask for the version of Informix that you're using, but it probably doesn't matter much this time. The simple answer is 'No'.
A more complex answer might discuss using a row type constructor, but that probably isn't what you want on the output. And, given the foregoing, then the UPDATE isn't going to work (and would require an extra level of parentheses if it was going to).
No, a CASE statement resolves to an expression (see IBM Informix Guide to SQL: Syntax CASE Expressions) and can be used in places where an expression is permitted. An expression is a single value.
from http://en.wikipedia.org/wiki/Expression_%28programming%29
An expression in a programming
language is a combination of explicit
values, constants, variables,
operators, and functions that are
interpreted according to the
particular rules of precedence and of
association for a particular
programming language, which computes
and then produces (returns, in a
stateful environment) another value.
Found an easy way to do it located here:
how to have listview row colour to change based on data in the row
Solution was just adding the case statement to my sql statement. Just maid my life much easier.