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,
Related
**** 04:04:32 TPT10508: RDBMS error 3857: Cannot use value (or macro parameter) to match '<column_name>'.
facing this issues while
executing tpt to load data from csv to Teradata DB. Could you please
help me in resolving this issue.
simple cast logic is used to convert data type from Varchar to decimal and timestamp(0).
and if i hardcode the value like (cast(20.0) as decimal(12,2) ) its working fine.
Failure case cast(column_nm) as decimal(12,2) .
This works:
SELECT TASKT_ID FROM DATA . TASKT WHERE TASK_WEB_IDENTIFIER = CAST ( HEXTORAW ( '0213725501A421D384233E5001' ) AS CHAR ( 26 ) ) ;
Since that work I put it into the procedure:
BEGIN
DECLARE GET_TASKT_ID_BY_TASK_WEB_IDENTIFIER_C1 CURSOR WITH RETURN FOR
SELECT TASKT_ID FROM DATA . TASKT WHERE TASK_WEB_IDENTIFIER = CAST ( HEXTORAW ( P_WEB_IDENTIFIER ) AS CHAR ( 26 ) ) ;
OPEN GET_TASKT_ID_BY_TASK_WEB_IDENTIFIER_C1 ;
END
The procedure has the one parameter P_WEB_IDENTIFIER which is a CHAR(26) for bit data with the CCSID 65535
However, when I now call it with the string like so:
call PROGRAM . GET_TASKT_ID_BY_TASK_WEB_IDENTIFIER ('0213725501A421D384233E5001');
I get back that the argument for VARBINARY_FUNCTION is invalid by length or data type.
Also, this works:
call PROGRAM . GET_TASKT_ID_BY_TASK_WEB_IDENTIFIER (CAST('0213725501A421D384233E5001' as char(26)));
What Can I do to make sure that string converts with only the string being passed?
What are you using to call the stored procedure?
What version and release of Db2 for i?
In the Run SQL Scripts component of IBM ACS or the older Access for Windows, string literals in your statements are treated as varchar.
Thus the CAST('0213725501A421D384233E5001' as char(26)) makes sense. What doesn't is the error message. Normally, you'd get a procedure not found error as the Db is looking for a procedure named PROGRAM.GET_TASKT_ID_BY_TASK_WEB_IDENTIFIER that takes a varchar parameter and the only thing that exists is a procedure that takes a char(26).
IBM's tools have gotten better at implicitly converting when needed. But I usually go with an explicit conversion when testing manually (as you've done here). Or I just make the parms varchar to start. And convert to character within the procedure if needed.
The char/varchar difference doesn't usually matter to the client code, as it can be specific in it's type definitions. It's only a factor for interactive tools like ACS that are executing dynamic statements.
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.
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!
By some reason my db table have some invalid data. But i cant say what is that invalid data.
I am using DB2 . I get message " Invalid data in field Use Hex mode to change. " when i try to select the values.I cant change it in hex mode also
another ui am using is zend studio quantum db . there when i select the rows the output will be like "zodiac[][][]"
the end characters are invalid which shows like boxes . I fix the rows which ever i know , by updating each row with correct value.
But is there any way to search for invalid character ? The question is little weird . i know.The character in hex mode also like a block.so i dont know how to search and find and fix.
How can i find this invisible bug ?