Convert string column to decimal number within ABAP class SELECT - abap

in my SAP CRM table, ratings per lawyer are stored as strings, and I need to calculate an average per lawyer:
ID_LAW
rating_LAW
1
'2.6'
1
'2.2'
2
'4.3'
When writing the ABAP class, I am writing a SELECT statement, but I have not found a way to convert the string column to a decimal number within the SELECT so I can calculate the average rating right away
This is what I have so far:
class ZCL_LAWYER_RATINGS_TEST definition
public
final
create public .
public section.
TYPES: BEGIN OF t_lawyer,
id TYPE c LENGTH 10,
rating TYPE string,
END OF t_lawyer,
tt_lawyer TYPE TABLE OF t_lawyer.
DATA: lawyers TYPE tt_lawyer.
protected section.
private section.
METHODS: get_lawyers IMPORTING rating_threshold TYPE string.
ENDCLASS.
CLASS ZCL_LAWYER_RATINGS_TEST IMPLEMENTATION.
METHOD get_lawyers.
SELECT ID_LAW as rating, CONV #(rating_LAW) AS rating
INTO TABLE #lawyers
FROM LAWYERS_STAT
WHERE rating >= #rating_threshold
GROUP BY ID_LAW.
ENDMETHOD.
ENDCLASS.
The usage of CONV is giving me trouble: CONV #(rating_LAW) AS rating
Any suggestion how to achieve the conversion?

ABAP SQL supports CAST to convert values from columns of type CHAR or SSTRING into numeric types.
You need to provide the numbers as a valid number format e.g. quotes are invalid characters so you must remove them using SQL string functions.
The possibilities depend on the ABAP version you use.
NB:
It's forbidden to mix pure ABAP (CONV)and pure SQL (rating_law) all together, the database cannot understand what CONV means.
Having SELECT inside or outside a class doesn't change anything.

Related

the USE OF CAST FUNCTION IN THE FOLLOWING QUERY

the thing is to replace whatever observation that hold the number 999.9 as NULL
but i dont know the utility of CAST here as i know it helps change the data type but WDSP in the description is STRING
IF(
wdsp="999.9",
NULL,
CAST(wdsp AS Float64)) AS wind_speed,
nothing yet the cast should it come before IF or within IF
The CAST function in being used to convert the wdsp column to a number (specifically FLOAT64) instead of returning it as a STRING (which you mention is the declared type). This is likely so that whatever is issuing the query can get the aliased column wind_speed as a numerical type that can be processed as a number and not a string.

CASTING to NUMERIC in SQL

I am trying to understand the ARPU calculation in SQL from the following code, however I don't understand why the author has used NUMERIC with revenue in the 2nd query? Won't revenue (meal_price * order quantity) be numeric anyway?
The issue is probably the following. NUMERIC is a specific data type. However, it is not clear that meal_price and order_quantity are specifically NUMERIC -- and not some other type such as INT.
Many databases do integer division for INT, so 1 / 2 is 0 rather than 0.5.
The conversion to NUMERIC is a simple way to avoid integer division.
Of course if a and b are numeric types , a * b will be numeric type
But there are many different numeric types, see
https://www.postgresql.org/docs/13/datatype-numeric.html
NUMERIC is a KEYWORK to specify numeric type of arbitrary précision, see previous link, it's often used to do exact calculations (accouinting) that cannoy be done in foating type.
In your case the author choosed to define the type he wants to use and not let the system/db choose for him. (try to figure out if a and b are integer what shoult be the type of the result 2 * 4 / 3 ?). It's a good practice.

how to convert int to decimal in SQL

I am working on an to solve a problem that I have a textbox that accepts any type of data but I want to cast the data to decimal(12,9) or any data type can accept this number as an example 42.56926437219384
here is the problem the user can enter any kind of data like characters or integer
1st case I want to handle if the data entered as integer:
DECLARE #num DECIMAL(12,9) = 444444444444444
SELECT CONVERT(DECIMAL(12,9),#num)
I think if it is characters will handle it in the solution and assign validation for the textbox.
how can I handle the integer part?
When you specify it as DECIMAL(12,9) this means your number can have up to 12 digits (excluding . ) and out of that 9 digits will be after decimal part. Which means the maximum value that you can store here is 999.999999999.
in your sample scenario above, the number has got 15 integer parts which are too high than the scope of your variable, as you can do either of the following
Change the size of variable from DECIMAL(12,9) to higher integer precision like DECIMAL(25,9)
Add a validation on your application to restrict the number of characters that the user can enter

AS400 thousand delimiter on numeric field

Is there a way to get a number formatted with a comma for thousand in numbers?
According to IBM documentation, this is the syntax:
DECIMAL(:newsalary, 9, 2, ',')
newsalary is the string (field)
9 is the precision
2 is the scale
, is the delimiter.
I tried:
SELECT DECIMAL ( T1.FIELD1 , 15 , 2 , "," ) AS TOTAL FROM TABLE T1
When trying it, I am getting the following error:
Message: [SQL0171] Argument 4 of function DECIMAL not valid.
DECIMAL converts from string type to a numeric type.
Numeric types don't have separators; only character representations of numbers have separators.
What tool are you using STRSQL, Run SQL Scripts or something else? Once you convert the string to a number, the tool should add the language appropriate separators when it displays the numeric data. For example, in STRSQL:
select decimal('12345.67', 12,2) as mynum
from sysibm.sysdummy1
Returns:
MYNUM
12,345.67
Using SQL to format strings is usually a bad idea. That should be left to whatever is consuming the data.
But if you really, really, really want to do it. You should create a user defined function (UDF) that does it for you. Here's an article, Make SQL Edit the Way You Want It To that includes source for for an EDITDEC function written in ILE RPG along with the SQL function definition you need to use it in an SQL statement.

MS Access SQL, Rnd Function without numeric field

Is it possible in a Microsoft Access query to use the Rnd() Function without specifying a numeric column (e.g. when my Primary Key contains Alpha characters) and still generate a different random number for each row?
You could use the 1st character;
rnd(asc(left([field],1)))
which should give a different result for each row, even if the char is the same
It's simpler than I originally thought, this function can be used in exactly the same context as Rnd()
Public Function Rand(FieldName) As Single
Rand = Rnd(1)
End Function
Then in SQL used as:
SELECT ID, Rand([ID]) FROM Table
By passing it a fieldname, we force the function to execute for each row in the query (eventhough we ignore the actual field in the function) and then evaluating the function to 1 will always return a different random number in the same way Rnd() would if executed in VB.