SSRS Report builder LENGTH expression & size specific field - sql

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.

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,

how to convert this String to Decimal

i have this String '5666,232343' and i want to convert it to Decimal, i use cast('5666,232343' as decimal(7,5)) but it returns NULL value.
Do you know why it doesn't work with CAST
Zorkolot is right. The current precision and scale that you've used is not sufficient for the value you've provided.
If you're using SQL Server 2012 or higher and you want to keep the comma in the value, then you can use the TRY_PARSE function and set a culture. It will return NULL if it encounters an error instead of not completing the statement and returning red text. This also allows you to add basic error handling to the statement, if you wanted, by getting failed conversions to return the value of zero. For example:
This is your original query (which is currently erroring) with my error handling fix:
select coalesce(try_parse('5666,232343' as decimal(7,5) using 'en-GB'),'0') as [DecimalValue]
This is the same thing as above but I've amended the decimal precision and scale so that the value is successfully converted:
select coalesce(try_parse('5666,232343' as decimal(16,6) using 'en-GB'),'0') as [DecimalValue]
This should prevent you having to perform a REPLACE either manually or by using the SQL function.
You need to cast to a decimal that can hold the value of 5666.232343.
DECIMAL(7,5) allows numbers in this format: ##.#####. The biggest number you can have then is 99.99999. You also need to take the comma out and replace it with a period:
SELECT CAST('5666.232343' as decimal(16,6)) AS [DecimalValue]
The problem is probably the comma. In some databases, some of the functions are not as internationally-sensitive as (I think) they should be. So try:
cast(replace('5666,232343', ',', '.') as decimal(7, 5))

Is format ####0.000000 different to 0.000000?

I am working on some legacy code at the moment and have come across the following:
FooString = String.Format("{0:####0.000000}", FooDouble)
My question is, is the format string here, ####0.000000 any different from simply 0.000000?
I'm trying to generalize the return type of the function that sets FooDouble and so checking to make sure I don't break existing functionality hence trying to work out what the # add to it here.
I've run a couple tests in a toy program and couldn't see how the result was any different but maybe there's something I'm missing?
From MSDN
The "#" custom format specifier serves as a digit-placeholder symbol.
If the value that is being formatted has a digit in the position where
the "#" symbol appears in the format string, that digit is copied to
the result string. Otherwise, nothing is stored in that position in
the result string.
Note that this specifier never displays a zero that
is not a significant digit, even if zero is the only digit in the
string. It will display zero only if it is a significant digit in the
number that is being displayed.
Because you use one 0 before decimal separator 0.0 - both formats should return same result.

Filtering rows in Pentaho

I have a dataset with columns containing numbers. However, some of the rows in that column have missing data. Instead of numbers, a dash (-) is placed in the cell.
What I want to happen is to separate those rows with a dash and output them to a separate excel file. Those without the dash, should output to a csv file.
I tried the "filter rows" but it gives me an error:
Unexpected conversion error while converting value [constant String] to a Number
constant String : couldn't convert String to number
constant String : couldn't convert String to number : non-numeric character found at position 1 for value [-]
My condition is if
Column1 CONTAINS - (String)
You cant try to convert to number in the select step,and handler the error, if can not convert to number that mean that is (-)
You can convert missing value indicators (like a dash or any other string) to null in Text-File-Input - see field option "Null if". That way you still can use the metadata detection feature and will not trip over a dash arriving in a Number field.
With CSV-File-Input you should stick to the String datatype until a Null-If step has cleansed the values, so you can change the datatype to Number in a Select-Values step.
If you must preserve the dash character, don't use metadata detection (as it suggests datatype Number) or use more rows to sample (so a field with a dash is encountered) or just revert the datatype to String again before saving and running the transformation.
My solution lies on the first 'Replace in String'. I replaced the dash into something numeric and can easily be distinguished from the rest of the numbers (I used 9999) and carried on with the rest of my process.
In filter rows, I had no problems anymore with the data type because both my variables and condition contained numbers, therefore, it no longer had to convert anything.
After filter rows, I added the 'Null-if' to remove the random 9999 that I used
just to have something to replace the dash.
After that, the separation was made just as I hope it would.
Thanks to #marabu for the Null-if idea.

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!