Cast as Int only when character is a number on Firebird 2.5 - sql-order-by

I have a query that I use on a MySQL database that orders the result, casting a char database field to integer when possible (when the char string is numeric), so for example the ORDER BY clause that I use on MySQL is:
ORDER BY
CASE
WHEN CONVERT(charfield, SIGNED INTEGER) IS NOT NULL THEN
CAST(charfield AS SIGNED INTEGER)
ELSE 9999999999 END
Where charfield is a database field character(25).
How can I translate this ORDER BY clause for Firebird 2.5?

You could use SIMILAR TO operator, ie
ORDER BY
CASE
WHEN charfield SIMILAR TO '[0-9]+' THEN CAST(charfield AS INTEGER)
ELSE 9999999
END
If the field has leading or trailing spaces then you have to use TRIM() function to get rid of those before test, ie
WHEN TRIM(charfield) SIMILAR TO ...
And to allow negative numbers you have to modify the pattern to include -, ie the clause would became
ORDER BY
CASE
WHEN TRIM(charfield) SIMILAR TO '\-?[0-9]+' ESCAPE '\' THEN CAST(charfield AS INTEGER)
ELSE 9999999
END

Related

SSRS takes out leading zero if the Customer Ref contains leading zero

I have the following situation of running SSRS report with Report Builder 3.0 (SQL Server 2012).
The data field CustomerRef contains Customer Reference No which may have Cust1234 or 00001234. I want to retain the Cust1234 whereas to trim out the leading zero of 00001234 with below expression.
=IIF(Fields!CustomerRef.Value.Contains("Cust"), Fields!CustomerRef.Value, CStr(Cint(Fields!CustomerRef.Value)))
As a result, Customer Ref No with 00001234 can be changed to 1234. However, all other Custxxxx returns #Error. How do I solve this?
This is not tested but try this
=IIF(Fields!CustomerRef.Value.Contains("Cust")
, Fields!CustomerRef.Value
, CStr(Cint(
IIF(Fields!CustomerRef.Value.Contains("Cust")
,0
,Fields!CustomerRef.Value)
)
)
)
The idea here is that is the field does contain "Cust" then the CINT function sees 0 as the operand rather than the CUst1234 which will fail, even though that but of code will never get executed.
Another option (again untested) is the simpler
=IIF(Fields!CustomerRef.Value.Contains("Cust")
, Fields!CustomerRef.Value
, CStr(VAL(Fields!CustomerRef.Value))
)
As VAL() will try to turn a string into a value by extracting only the numeric parts of the string, it does not fail when presented with a string as an argument.

ADO Recordset filter comparing two fields

How can I filter a recordset by comparing two fields?
For a given ADO Recordset with n fields (Field1, Field2,...,Fieldn)
I used to filter a field against a value:
rs.Filter = "Field1 = 'something'"
But what I need to do is something like this:
rs.Filter = "Field1 = Field2"
Is that possible?
The criteria string is made up of clauses in the form FieldName-Operator-Value
Value is the value with which you will compare the field values (for example, 'Smith', #8/24/95#, 12.345, or $50.00). Use single quotes with strings and pound signs (#) with dates. For numbers, you can use decimal points, dollar signs, and scientific notation. If Operator is LIKE, Value can use wildcards. Only the asterisk (*) and percent sign (%) wild cards are allowed, and they must be the last character in the string. Value cannot be null.
This suggests that comparing fields to each other is not supported. Value must be a literal.

ORA-01722 invalid number on using the expression in select and while joining

CASE WHEN (r.code_value4 = 0 AND LENGTH(ltrim(rtrim(xx.AFFILIATE_CODE))) > 0) AND
r_intercompany.code_value1 is not null AND
ltrim(rtrim(xx.AFFILIATE_CODE)) <> (CASE WHEN xx.COMPANY_CODE_JE_EXCEPTION_FLAG = 1
THEN r.code_value3 ELSE r.code_value1 END)
THEN r_intercompany.code_value1
ELSE NVL(r_mga_acct.code_value2, xx.ACCOUNT_NUMBER) END
I have view in which the above part of the sql is being used in select statement and as well as to compare with a field while joining. When I run the view after making this change, it is throwing the error ORA-01722 invalid number.
Please let me know on how to correct this.
Thanks
" it is throwing the error ORA-01722 invalid number."
It is likely somewhere you are comparing a numeric column with a string column . Oracle is implicitly casting the string to a number but the column contains values which cannot be converted, so it hurls.
"Please let me know on how to correct this."
Don't rely on implicit data conversion. Go through your code, check the data types of all the columns. Where you find a varchar2 column being compared to a number you need to cast the number to a string. For literals that means quoting them - '1' instead of 1 - and for columns that means wrapping them in to_char() calls.
My Suspicion is either r.code_value4 field or xx.COMPANY_CODE_JE_EXCEPTION_FLAG field is String type hence try the below
CASE WHEN (r.code_value4 = '0' AND LENGTH(ltrim(rtrim(xx.AFFILIATE_CODE))) > 0) AND
r_intercompany.code_value1 is not null AND
ltrim(rtrim(xx.AFFILIATE_CODE)) <> (CASE WHEN xx.COMPANY_CODE_JE_EXCEPTION_FLAG = '1'
THEN r.code_value3 ELSE r.code_value1 END)
THEN r_intercompany.code_value1
ELSE NVL(r_mga_acct.code_value2, xx.ACCOUNT_NUMBER) END

Cannot cast object '(10)' with class 'java.lang.String' to class 'java.lang.Integer'

I am using ireport 3.7.1. I have made a connection with my database.I have a procedure which when given an input in number ,it returns the word format of the number i.e if I give input 10,it will return ten. The problem is when I am executing the procedure in pl/sql developer,I am getting the proper output but when I am firing the same procedure in ireport it's giving me this exception
Cannot cast object '(10)' with class 'java.lang.String' to class 'java.lang.Integer' .
Casting straight from a String to an Integer is not possible. You'll want to use the function Integer.parseInt(stringNumber);
(10) isn't a properly formated integer. Not even for PL/SQL:
select '(10)' +0 from dual;
> ORA-01722: invalid number
I could only suggest you to trace back the point where those ( ) come from. And fix your code at that position instead. Just a wild guess, some number formats use parenthesis to represent negative numbers. Maybe this is your case?
That being said, if you still want to locally remove the parenthesis that have somehow lurked inside of your string:
String str = "(10)";
int value = Integer.parseInt(str.substring(1, str.length()-1));
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// *blindly* get away of first and last character
// assuming those are `(` and `)`
For something a little bit more robust, and assuming parenthesis denotes negative numbers, you should try some regex:
String str = "(10)";
str = str.replaceFirst("\\(([0-9]+)\\)", "-$1");
// ^^^ ^^^ ^
// replace integer between parenthesis by its negative value
// i.e.: "(10)" become "-10" (as a *string*)
int value = Integer.parseInt(str);

Oracle Pattern matching

In Oracle I want to check whether the string has "=' sign at the end. could you please let me know how to check it. If it has '=' sign at the end of string, I need to trailing that '=' sign.
for eg,
varStr VARCHAR2(20);
varStr = 'abcdef='; --needs to trailing '=' sign
I don't think you need "pattern matching" here. Just check if the last character is the =
where substr(varstr, -1, 1) = '='
substr when called with a negative position will work from the end of the string, so substr(varstr,-1,1) extracts the last character of the given string.
Use the REGEX_EXP function. I'm putting a sql command since you didn't specify on your question.:
select *
from someTable
where regexp_like( someField, '=$' );
The pattern $ means that the precedent character should be at the end of the string.
see it here on sql fiddle: http://sqlfiddle.com/#!4/d8afd/3
It seems that substr is the way to go, at lease with my sample data of about 400K address lines this returns 1043 entries that end in 'r' in an average of 0.2 seconds.
select count(*) from addrline where substr(text, -1, 1) = 'r';
On the other hand, the following returns the same results but takes 1.1 seconds.
select count(*) from addrline where regexp_like(text, 'r$' );

Resources