Firebird SQL SUBSTRING : "expression evaluation not supported - sql

I'm trying to run the following query on a database to replace the leading characters of certain rows.
UPDATE table SET path = :newpath || SUBSTRING(path FROM :pathlen)
WHERE path STARTING WITH :oldpath
So for parameter :newpath = foo, :oldpath = bar and :pathlen = 4, I want this
bar\wibble
to be changed to ...
foo\wibble
However, I get the error "expression evaluation not supported" and I'm not sure why. Replacing :pathlen with a literal 4 works correctly, so it's definitely the SUBSTRING causing the problem.

Related

Stripping out non-digits - SQL0171 Argument of function TRANSLATE not valid

I'm trying to extract the first ten digit numbers of a phone number, ending up with a 10-digit (or less) number.
I need to use whitelisting, not blacklisting, due to special characters having been used. e.g. "á(123) 555-4567 Toll Free:á(891) 0" must become 1235554567.
I'm trying to use https://stackoverflow.com/a/37685384.
However, when I try to use this:
TRANSLATE(SFCONTACT.PHONE,'',TRANSLATE(SFCONTACT.PHONE,'','1234567890',''),'') as clean
I get
Message: [SQL0171] Argument 04 of function TRANSLATE not valid.
One of the comments said that using spaces instead of empty strings removed that. However, for me, trying:
TRANSLATE(SFCONTACT.PHONE,' ',TRANSLATE(SFCONTACT.PHONE,' ','1234567890',' '),' ') as clean
gives:
Message: [SQL0171] Argument 03 of function TRANSLATE not valid.
How can I accomplish this?
Running an AS400 DB2, IBM version V7R1M0
EDIT (Not sure if this should be a separate Question or not)
I tried this as suggested:
SUBSTR(REGEXP_REPLACE(PHONE, '[\D]', ''),1,10) AS MAINPHONE,
And at first it seemed to work; I was able to create a view, BBICNTMIG
However, when I try to insert into a table using that view:
INSERT INTO AMMLIBC.BBICONTACT
(COMPANY,CUSNO,SHIPTO,HONORIFICFK,FIRSTNAME,LASTNAME,EMAIL,MAINPHONE,TYPEFK,PROSPECTFK,CREATEDBY,CREATEDAT)
SELECT COMPANY,CUSNO,SHIPTO,HONORIFICFK,FIRSTNAME,LASTNAME,EMAIL,MAINPHONE,TYPEFK,PROSPECTFK,CREATEDBY,CREATEDAT
FROM AMMLIBC.BBICNTMIG
it gives:
Message: [SQL0420] Character in CAST argument not valid. Cause . . . . . : A character in the argument for the CAST function was not correct. Recovery . . . : Change the result data type to one that recognizes the characters in the CAST argument, or change the argument to contain a valid representation of a value for the result data type. Try the request again.
If I remove the phone numbers from the insert (taking default value of null instead), then the INSERT succeeds, so I know it's the phone number causing this.
The column type in the destination table is NUMERIC(10,0). I tried using this, but no change:
CAST(SUBSTR(REGEXP_REPLACE(PHONE, '[\D]', ''),1,10) AS NUMERIC(10,0)) AS MAINPHONE,
Further info:
I tried casting to char before casting to numeric. No change.
I tried adding a where clause (both "mainphone is null" and "mainphone is not null" do this) and the error message changes to:
Message: [SQL0802] Data conversion or data mapping error. Cause . . . . . : Error type 6 has occurred. [...] 6 -- Numeric data that is not valid.
The TRANSLATE function doesn't work correctly for non-ascii characters.
Use the following instead:
SELECT substr(
-- xmlcast(xmlquery('fn:replace($s, "[^\d]", "")' passing PHONE as "s") as varchar(4000)) -- DB2 for LUW
regexp_replace(PHONE, '[^\d]', '') -- DB2 for LUW 11.1 & DB2 for IBM i
, 1, 10)
FROM TABLE(VALUES
'á(123) 555-4567 Toll Free:á(891) 0'
, 'á(123) 555-'
) SFCONTACT(PHONE);
Platform & version of Db2 is important...
Db2 for i 7.2, for instance, gives me
Cause . . . . . : Parameter 3 specified in function TRANSLATE is not valid for use for reason code 1. The reason codes and their meanings follow:
1 -- Parameter must be a string constant.
2 -- Parameter must be an integer constant.
3 -- Parameter must be a numeric constant.
4 -- Parameter's length is too long.
5 -- Parameter's value is out of range.
6 -- Parameter must be a valid CCSID.
7 -- Parameter cannot be a parameter marker.
8 -- Parameter's data type is not supported by the built-in function.
9 -- Parameter cannot reference a column with an active column mask since the function is not secured.
Recovery . . . : Refer to the DB2 for IBM i SQL Reference topic collection in the Database category in the IBM i Information Center book, http://www.ibm.com/systems/i/infocenter/ for more information on functions. Correct the parameter specified for the function. Try the request again.
Using "FETCH FIRST _ ROWS ONLY" and binary search, I found the problem.
The phone number it was trying to parse was "PLEASE VERIFY CONTACT AND EMAIL". Which, stripping out digits, turns into empty string... which does not convert well into NUMERIC(10,0).
So, this fixed the problem:
CASE
WHEN TRIM(REGEXP_REPLACE(PHONE, '[\D]', '')) = '' THEN NULL
ELSE CAST(SUBSTR(REGEXP_REPLACE(PHONE, '[\D]', ''),1,10) AS NUMERIC(10,0))
END AS MAINPHONE,

Format decimal value in database in where clause

I want to get data from database but the format of values that are compared do not tally.
The numbers in database are saved in weird format - sometimes are rounded and some of those are decimals (10, 14, 15, 12.44, 16.10, etc.)
In array I have values rounded on 2 decimals though.
I want to get data from database like so:
$foundPayments = $this>paymentsRepo>newQuery()>whereRaw("CONVERT(DECIMAL(10,2),payments.amount) = :payments", ['payments' => $payments]);
Althought, I end up with an error like this: Syntax error: 7 ERROR: syntax error at or near ","
I am not sure if the code works in laravel 5.1 with sending params like I do but thats not really what I do care for in that - the same error apperars even when I do not use variable.
EDIT:
I am using Postgres.
I think that the syntax that you are using for the convert function is wrong. You should first pass the value then the type to which you are converting to.
But please try the cast function, I think that it should give you the expected result.
$foundPayments = $this->paymentsRepo->newQuery()
->whereRaw("CAST(payments.amount as DECIMAL(10,2)) = :payments",
['payments' => $payments]);

SSRS Report builder LENGTH expression & size specific field

I'm trying to build a bank file using SSRS report builder (3.0). I'm running into two issues:
I'm trying to get a length expression to work on a check number field but LEN(Field) doesn't work (returns 4 as the value regardless of the actual length of the field).
And LENGTH(Field) gives me an error:
The Value expression for the textrun 'Textbox15.Paragraphs[0]. TextRuns[0\' contains an error: [BC30451] Name 'LENGTH' is not declared*
The only reason why I'm even trying to get #1 to work is because I need to have one of the fields on the bank file have a constant length. Regardless of the check number, I need to make sure this field is always at 14 characters with leading zeros. I thought the best way to do this is to do a switch statement and add the number of appropriate zeros in depending on the size of the check number field.
Thanks for the help.
Edit: using a SQL server DB
For the length issue:
There are two ways to get string length
Using the LEN function
= LEN(Fields!myfield.Value)
Using the length property
= Fields!myfield.Value.Length
If your field is not a string, try converting first by using the Cstr function
= LEN( Cstr(Fields!myfield.Value) )
= Cstr(Fields!myfield.Value).Length
For the formatting issue:
For numeric fields set the cell format expression to have as many zeros as required eg. for 14 digit numbers
= "00000000000000"
I don't know on which database you are working if you are using sql server then try LEN
function and LENGHT in oracle.
I think you first convert it into integer if it's character and then try len function.

Data output syntax in Webi XI3.1

I am trying to duplicate a detail object from Desktop to its Webi equivalent.
I am familiar with the syntax differences, i.e. using semicolons instead of commas and using [] rather than <> to enclose references to other dimensions/measures/detail objects.
Given the working formula from the Deski report:
=ToDate(7+"/1/"+<Current FY>-1 ,"mm/dd/yyyy")
I tried to convert this using the syntax I know from Webi:
=ToDate(7+"/1/"+[Current FY]-1 ; "mm/dd/yyyy")
I faced the error message
The expression or sub-expression at position 8 in the '-' function uses an invalid data type
I am guessing this has something to do with trying to convert a date datatype into an integer in order to subtract "1." However, I do not know what kind of function this requires.
Thanks in advance!

Python select exists SQLite3 with variable

I am to be unable to get the following code to work. I know how to use python variables in queries, but somehow I can't get this right. The query works fine when I hard code the 'icaocode' variable in the query, but not if I try to use a variable. What is wrong with this code?
icaocode = input()
c.execute("SELECT EXISTS(SELECT 1 FROM airports WHERE ICAO = ?)", (icaocode))
if c.fetchone():
print("Found!")
else:
print("Not found...")
Received error:
sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 4 supplied.
In Python, wrapping an expression in parentheses does not make any difference, (icaocode) is exactly the same as icaocode.
The execute method expects some kind of list of parameters, so it sees the string as a sequence of four characters.
To tell Python that you want a tuple with a single element, you have to add a comma:
c.execute("... WHERE ICAO = ?", (icaocode,))