CX_SY_CONVERSION_NO_NUMBER during a calculation - abap

I am trying following code from the SAP Press book ABAP Basics. I am using exactly the same code from the the book and unable to understand why it is failing. While I am executing the program I am getting following Runtime Error. I have tried to debug the code and found that my code is failing in ELSE section at l_total_amount calculation. Could please let me know what I am doing wrong?
An exception has occurred which is explained in more detail below. The exception, which is assigned to class 'CX_SY_CONVERSION_NO_NUMBER' was not caught .
code fails at this line: l_total_amount = <l_credit_amount> + l_vat_amount.
REPORT ZGULLA_SALES_ORDER_DYNAMIC.
PARAMETERS:
*Article Data
p_ano(10) TYPE n OBLIGATORY,
p_aname(40) TYPE c OBLIGATORY,
p_aprice TYPE p DECIMALS 2 OBLIGATORY,
p_curr TYPE currencysap OBLIGATORY DEFAULT 'EUR',
p_aquant TYPE i OBLIGATORY DEFAULT 1,
*Tax
p_tax TYPE p DECIMALS 2 DEFAULT '16' OBLIGATORY,
*Terms of payment
p_cash TYPE c RADIOBUTTON GROUP 0001 DEFAULT 'X',
p_credit TYPE c RADIOBUTTON GROUP 0001,
p_months TYPE i OBLIGATORY DEFAULT '24'.
CONSTANTS:
*Interest per year in percent
con_annual_interest TYPE p DECIMALS 2 VALUE '6.75'.
DATA:
*Temporary data
l_net_amount TYPE p DECIMALS 2,
l_tax_factor TYPE f,
*l_credit_amount TYPE p DECIMALS 2,
*l_monthly_interest_factor TYPE f,
*Result data
* l_monthly_vat_amount TYPE p DECIMALS 2,
* l_monthly_amount TYPE p DECIMALS 2,
l_vat_amount TYPE p DECIMALS 2,
l_total_amount LIKE l_net_amount,
* * Dynamic variables
l_rda_credit_amount TYPE REF TO currency,
l_rda_monthly_interest_factor TYPE REF TO f,
l_rda_monthly_vat_amount TYPE REF TO currency,
l_rda_monthly_amount TYPE REF TO currency.
FIELD-SYMBOLS:
* Access to reference variables
<l_credit_amount> TYPE any,
<l_monthly_interest_factor> TYPE f,
<l_monthly_vat_amount> TYPE any,
<l_monthly_amount> TYPE currency.
* Temporary calculations
l_net_amount = p_aprice * p_aquant.
l_tax_factor = p_tax / 100.
* Write Article information to screen
WRITE: /, / 'Article information',
/ 'Article number: ', 30 p_ano,
/ 'Article name: ', 30 p_aname,
/ 'Article net price: ', 30 p_aprice, p_curr,
/ 'Quantity: ', 30 p_aquant.
* Write conditions to screen
WRITE: /, / 'Conditions',
/ 'Tax rate: ', 30 p_tax,
/ 'Quantity: ', 30 p_aquant.
WRITE: /, / 'Result'.
IF p_cash = 'X'.
* Calculate cash results
l_vat_amount = l_net_amount * l_tax_factor.
l_total_amount = l_net_amount + l_vat_amount.
* Write results to screen
WRITE: / 'Total VAT amount: ', 30 l_vat_amount, p_curr,
/ 'Total amount: ', 30 l_total_amount, p_curr.
ELSE.
* Calculate interest Results
CREATE DATA l_rda_monthly_interest_factor.
ASSIGN l_rda_monthly_interest_factor->* to <l_monthly_interest_factor>.
CREATE DATA l_rda_credit_amount.
ASSIGN l_rda_credit_amount->* to <l_credit_amount>.
CREATE DATA l_rda_monthly_vat_amount.
ASSIGN l_rda_monthly_vat_amount->* to <l_monthly_vat_amount>.
CREATE DATA l_rda_monthly_amount.
ASSIGN l_rda_monthly_amount->* to <l_monthly_amount>.
<l_monthly_interest_factor> = con_annual_interest / 100 / 12.
<l_credit_amount> = l_net_amount + l_net_amount * <l_monthly_interest_factor>
* p_months.
l_vat_amount = <l_credit_amount> * l_tax_factor.
l_total_amount = <l_credit_amount> + l_vat_amount. "<=============== FAILS
<l_monthly_vat_amount> = l_vat_amount / p_months.
<l_monthly_amount> = l_total_amount / p_months.
* Write results to screen
WRITE: / 'Month: ', 30 p_months,
/ 'Monthly VAT amount: ', 30 <l_monthly_vat_amount>, p_curr,
/ 'Monthly amount: ', 30 <l_monthly_amount>, p_curr,
/ '(VAT amount: ', 30 l_vat_amount, p_curr, ')',
/ '(Total amount: ', 30 l_total_amount, p_curr, ')'.
ENDIF.

You are using text field CURRENCY for amounts. Therefore you get a short dump. Just change the type to a value type and everything should be ok. I have changed it to DEC23_2 and everything ran smoothly.
The CURRENCY type is used to hold information about the currency like EUR, USD, GBP, etc. and is a text type.

I guess issue is because of floating point range at Line no 114.
Just convert to packed decimal.By adding the below code
DATA : lv_monthly_interest TYPE p DECIMALS 2.
MOVE <l_monthly_interest_factor> TO lv_monthly_interest.
<l_credit_amount> = l_net_amount + l_net_amount * lv_monthly_interest * p_months.
Hope this helps!!!

Related

What is reason why my second list for my ALV report not showing any info?

I am trying to create a classical list. Everything until At line-selection works. The problem that I have is with At line-selection. First, I execute the program and it shows me the parameter s_matnr, I then enter the
values (e.g. 500-100 - 500-400) and it gets me to the first list. After that, I choose one field by checking and I double-click on it. It then shows me another window and when I try to get to the second list that I've created it just doesn't output any info on the screen.
What I have to do is to get two tables, ekko and ekpo, and to display the info in a list. For that, I have tried to create a inner join between the two tables and then loop through them and write the info. Also, I have to calculate the total quantity of each Purchase order.
Could you help me out? Thank you!
The list has to look like this :
My lines of code :
TOP-OF-PAGE.
WRITE: / 'User:',sy-uname, 29 'Programe Name: ', sy-cprog,
/ 'Date: ', sy-datum.
ULINE.
TABLES mara.
TYPES : BEGIN OF ty_mara,
chck TYPE char1,
matnr TYPE mara-matnr,
maktx TYPE makt-maktx,
ntgew TYPE mara-ntgew,
gewei TYPE mara-gewei,
spart TYPE mara-spart,
name TYPE string,
END OF ty_mara,
tt_mara TYPE STANDARD TABLE OF ty_mara.
TYPES: BEGIN OF ty_marc,
matnr TYPE marc-matnr,
werks TYPE marc-werks,
END OF ty_marc.
TYPES:BEGIN OF ty_ab,
ebeln TYPE ekpo-ebeln,
lifnr TYPE ekko-lifnr,
bedat TYPE ekko-bedat,
ekorg TYPE ekko-ekorg,
ekgrp TYPE ekko-ekgrp,
bukrs TYPE ekko-bukrs,
ebelp TYPE ekpo-ebelp,
aedat TYPE ekpo-aedat,
menge TYPE ekpo-menge,
meins TYPE ekpo-meins,
netpr TYPE ekpo-netpr,
END OF ty_ab.
DATA:
gt_ab TYPE TABLE OF ty_ab,
gs_ab TYPE ty_ab.
TYPES: BEGIN OF ty_name,
cbx TYPE c,
cmatnr TYPE mara-matnr,
END OF ty_name.
DATA:
gv_var1 TYPE string VALUE 'Purchase order',
gv_var2 TYPE string VALUE 'Vendor',
gv_var3 TYPE string VALUE 'Document Date',
gv_var4 TYPE string VALUE 'Purchasing Org.',
gv_var5 TYPE string VALUE 'Purchasing Group',
gv_var6 TYPE string VALUE 'Company Code',
gv_quantity TYPE ekpo-menge VALUE 0.
DATA:
gv_v1 TYPE string VALUE 'Item',
gv_v2 TYPE string VALUE 'Delivery Date',
gv_v3 TYPE string VALUE 'PO Quantity',
gv_v4 TYPE string VALUE 'Unit',
gv_v5 TYPE string VALUE 'Net Price'.
TYPES: BEGIN OF ty_ekko,
ebeln TYPE ekko-ebeln,
lifnr TYPE ekko-lifnr,
bedat TYPE ekko-bedat,
ekorg TYPE ekko-ekorg,
ekgrp TYPE ekko-ekgrp,
bukrs TYPE ekko-bukrs,
END OF ty_ekko.
TYPES:BEGIN OF ty_ekpo,
ebeln TYPE ekpo-ebeln,
ebelp TYPE ekpo-ebelp,
aedat TYPE ekpo-aedat,
menge TYPE ekpo-menge,
meins TYPE ekpo-meins,
netpr TYPE ekpo-netpr,
END OF ty_ekpo.
DATA: gt_mara TYPE TABLE OF ty_mara,
gs_mara TYPE ty_mara,
gt_marc TYPE TABLE OF ty_marc,
gs_marc TYPE ty_marc,
gt_popup TYPE ty_marc,
gv_string TYPE string,
gv_line TYPE i,
gs_name TYPE ty_name,
gt_name TYPE TABLE OF ty_name,
gt_ekko TYPE TABLE OF ty_ekko,
gs_ekko TYPE ty_ekko,
gt_ekpo TYPE TABLE OF ty_ekpo,
gs_ekpo TYPE ty_ekpo.
SELECT-OPTIONS s_matnr FOR mara-matnr.
START-OF-SELECTION.
SET PF-STATUS 'DIALOG'.
SELECT a~matnr, a~ntgew, a~gewei, a~spart, b~maktx
FROM mara AS a
INNER JOIN makt AS b
ON a~matnr = b~matnr
INTO CORRESPONDING FIELDS OF TABLE #gt_mara
WHERE a~matnr IN #s_matnr
AND b~spras = 'E'.
WRITE: AT 3 'Material', AT 30 'Material Description', AT 60 'Net Weight',
AT 80 'Unit', AT 85 'Division'.
LOOP AT gt_mara INTO gs_mara.
IF gs_mara-ntgew < 10.
WRITE: / gs_name-cbx AS CHECKBOX,
gs_mara-matnr COLOR 4,
gs_mara-maktx COLOR 4,
gs_mara-ntgew COLOR 4,
gs_mara-gewei COLOR 4,
gs_mara-spart COLOR 4.
ELSE.
WRITE: / gs_name-cbx AS CHECKBOX,
gs_mara-matnr COLOR COL_NEGATIVE,
gs_mara-maktx COLOR COL_NEGATIVE,
gs_mara-ntgew COLOR COL_NEGATIVE,
gs_mara-gewei COLOR COL_NEGATIVE,
gs_mara-spart COLOR COL_NEGATIVE.
ENDIF.
ENDLOOP.
AT USER-COMMAND.
CASE sy-ucomm.
WHEN'DISPLAY'.
SET PF-STATUS 'POP'.
SET TITLEBAR 'Titlu'.
WINDOW STARTING AT 5 3 ENDING AT 40 10.
CLEAR: gs_mara,gv_string.
DO .
READ LINE sy-index FIELD VALUE gs_name-cbx INTO gs_name-cbx.
IF sy-subrc = 0.
IF gs_name-cbx = 'X'.
READ LINE sy-index FIELD VALUE gs_mara-matnr INTO gs_name-cmatnr.
SELECT marc~matnr marc~werks
INTO CORRESPONDING FIELDS OF TABLE gt_marc
FROM marc
WHERE marc~matnr LIKE gs_name-cmatnr.
IF sy-subrc = 0.
LOOP AT gt_marc INTO gs_marc.
WRITE: / gs_marc-matnr, gs_marc-werks.
ENDLOOP.
ELSE .
MESSAGE e208(00) WITH 'No records found!'.
ENDIF.
ENDIF.
ELSE.
EXIT.
ENDIF.
ENDDO.
WRITE:/ '' .
ENDCASE.
AT LINE-SELECTION.
GET CURSOR LINE gv_line.
READ LINE gv_line FIELD VALUE gs_mara-matnr INTO gs_name-cmatnr.
SELECT
c~lifnr
c~bedat
c~ekorg
c~ekgrp
c~bukrs
c~ebeln
d~ebelp
d~aedat
d~menge
d~meins
d~netpr
FROM ekko AS c
INNER JOIN ekpo AS d
ON c~ebeln = d~ebeln
INTO CORRESPONDING FIELDS OF TABLE gt_ab
WHERE d~matnr = gs_name-cmatnr .
LOOP AT gt_ab INTO gs_ab.
IF sy-subrc = 0.
gv_quantity = 0.
ULINE.
WRITE:/
gs_ekko-ebeln UNDER gv_var1 COLOR 6,
gs_ekko-lifnr UNDER gv_var2 COLOR 6,
gs_ekko-bedat UNDER gv_var3 COLOR 6,
gs_ekko-ekorg UNDER gv_var4 COLOR 6,
gs_ekko-ekgrp UNDER gv_var5 COLOR 6,
gs_ekko-bukrs UNDER gv_var6 COLOR 6.
WRITE:/
gs_ekpo-ebelp UNDER gv_v1 ,
gs_ekpo-aedat UNDER gv_v2 ,
gs_ekpo-menge UNDER gv_v3 ,
gs_ekpo-meins UNDER gv_v4 ,
gs_ekpo-netpr UNDER gv_v5 .
gv_quantity = gv_quantity + gs_ekpo-menge.
WRITE:/ 'Total Quantity=' UNDER gv_v3 COLOR 3, gv_quantity COLOR 3.
ELSE.
MESSAGE e208(00) WITH 'Nu a fost gasit!'.
ENDIF.
ENDLOOP.
I guess you need to debug your code to check what's going on, what the exact issue is.
Here is a Minimal Reproducible Example, which demonstrates that there's basically no issue from the different list levels:
REPORT.
TYPES ty_matnr TYPE c LENGTH 10.
DATA matnr TYPE ty_matnr.
SELECT-OPTIONS s_matnr FOR matnr.
TOP-OF-PAGE.
WRITE: / 'User:',sy-uname, 29 'Programe Name: ', sy-cprog,
/ 'Date: ', sy-datum.
ULINE.
START-OF-SELECTION.
WRITE / 'Enter DISPLAY in the Command Field and press Enter'.
AT USER-COMMAND.
CASE sy-ucomm.
WHEN'DISPLAY'.
WINDOW STARTING AT 5 3 ENDING AT 40 10.
matnr = '500-100'.
WRITE / matnr.
matnr = '500-400'.
WRITE / matnr.
matnr = '700-220'.
WRITE / matnr.
ENDCASE.
AT LINE-SELECTION.
DATA gv_line TYPE i.
GET CURSOR LINE gv_line.
DATA matnr2 TYPE ty_matnr.
READ LINE gv_line FIELD VALUE matnr INTO matnr2.
WRITE : / 'Second list. Selected line is', matnr2.

Long arithmetic with SQL SELECT statement

The basic idea is I have 3 separate SELECT statements that output a number. I need a percentage from those numbers. So the problem would look like this:
(a.count_one + b.count_two) -
(b.count_two + c.count_three) / (a.count_one + b.Count_two) * 100
When I do the code found below I only get the output of the first (a.count_one + b.count_two). BUT if I comment out the third portion which is / (a.count_one + b.count_two) I successfully get the correct solution from (a.count_one + b.count_two) – (b.count_two + c.count_three).
So it appears, doing math of two statements is great, but when I throw in a third statement it pukes and only wants to show the solution of the first problem (a.count_one + b.count_two), but does not even try to calculate the solution for (a.count_one + b.count_two) – (b.count_two + c.count_three) anymore. I'm a bit stumped why.
Select
(a.Count_one + b.Count_two) -
(b.Count_two + c.Count_three) / (a.Count_one + b.Count_two) * 100
as 'Final_Percentage'
from
(
select COUNT(v_Summary.ResourceID) AS Count_one
From [DB1].[dbo].[v_Summary]
) a,
(
select count([CN]) as Count_two
From [DB2].[dbo].[Computers]
WHERE cn NOT IN (SELECT name0 FROM [DB1].[dbo].[v_system] where v_system.Client0 = '1')
) b,
(
select COUNT(v_Summary.ResourceID) AS Count_three
From [DB1].[dbo].[v_Summary]
Where Description like '%/Fail'
) c
And to give additional information. The math problem with numbers:
(54558 + 373) – (373 + 117) / (54558 + 373) * 100
Or further solved:
(54931) - (490) / 55304 * 100 = 98.44%
COUNT function returns int. When you divide two int values the result is again int in SQL Server. So, 490 / 55304 is 0. Then 0 * 100 is 0 again.
Cast the values to a suitable type, like float.
By the way,
(54931) - (490) / 55304 * 100
is not equal to ~98.44, it is equal to ~54930.11399
You need parentheses to get the result you expect:
((54931) - (490)) / 55304 * 100
is equal to ~98.43953421
So, the final query should look like this:
Select
((a.Count_one + b.Count_two) - (b.Count_two + c.Count_three))
/ (a.Count_one + b.Count_two) * 100
as 'Final_Percentage'
from
(
select CAST(COUNT(v_Summary.ResourceID) AS float) AS Count_one
From [DB1].[dbo].[v_Summary]
) a,
(
select CAST(count([CN]) AS float) as Count_two
From [DB2].[dbo].[Computers]
WHERE cn NOT IN (SELECT name0 FROM [DB1].[dbo].[v_system] where v_system.Client0 = '1')
) b,
(
select CAST(COUNT(v_Summary.ResourceID) AS float) AS Count_three
From [DB1].[dbo].[v_Summary]
Where Description like '%/Fail'
) c
You should also get the right result by saying
Select
100.*(a.Count_one - c.Count_three) / (a.Count_one + b.Count_two)
as 'Final_Percentage'
from ... -- unchanged subqueries, as supplied in your question
By moving the *100. right to the front you implicitly convert the first product to float and the type then stays that way for the rest of the calculation. Also, you might have noticed, I simplified the formula, since b.Count_two gets subtracted from itself, so you can leave it out altogether. I have not tested this but I am pretty sure it will work.

Free space on SQL Server 2014 Express

Since the Express edition is limited to 10 GB (couldn't figure out if it was 'per database' or as a whole since the documentation is confusing,well at least for me), I am wondering is there a way to check this space availability i.e how much have I used so far ? I used some suggestions from the internet but somehow not all of my databases show up.
The 10 GB is for data file size. You can get percentage used of each file like this:
;WITH x AS
(
SELECT name, [file] = physical_name, size = size * 8,
su_bytes = FILEPROPERTY(name, 'SpaceUsed') * 8
FROM sys.database_files
)
SELECT name, [file], size, SpaceUsed = su_bytes,
[SpaceUsed%] = CONVERT(DECIMAL(5,2), su_bytes*100.0/size)
FROM x;
As follows from the manual on sys.database_files, the size column holds the size expressed in 8K pages, hence multiplying the value by 8 gives you the size in kilobytes. Same applies to the result of FILEPROPERTY(..., 'SpaceUsed'). To obtain the amounts in other units, change the calculations accordingly.
You're right ... Did some googling and found a much better solution:
SELECT name AS 'File Name' , physical_name AS 'Physical Name', size/128 AS 'Total Size in MB',
size/128.0 - CAST(FILEPROPERTY(name, 'SpaceUsed') AS int)/128.0 AS 'Available Space In MB', *
FROM sys.database_files;
even better :
SELECT DB_NAME(A.DATABASE_ID) AS DBNAME,A.NAME AS FILENAME ,
SIZE/128.0 AS CURRENTSIZE_MB,
B.RECOVERY_MODEL_DESC,A.TYPE_DESC ,
CASE WHEN IS_PERCENT_GROWTH = 0
THEN LTRIM(STR(A.GROWTH * 8.0 / 1024,10,1)) + ' MB, '
ELSE 'BY ' + CAST(A.GROWTH AS VARCHAR) + ' PERCENT, 'END +
CASE WHEN MAX_SIZE = -1 THEN 'UNRESTRICTED GROWTH'
ELSE 'RESTRICTED GROWTH TO ' +LTRIM(STR(MAX_SIZE * 8.0 / 1024,10,1)) + ' MB'
END AS AUTOGROW,A.PHYSICAL_NAME
FROM SYS.MASTER_FILES A JOIN SYS.DATABASES B
ON A.DATABASE_ID =B.DATABASE_ID
--WHERE A.PHYSICAL_NAME LIKE 'E%'
-- AND A.FILE_ID =2
ORDER BY A.SIZE/128.0 DESC

Latitude and Longitude conversion error

I'm trying to execute a specific query in my database, but occurs the following error
Table San_Filial
Filial_Id Name lat lon
2 A -19.926131 -43.924373
3 B -19.952192 -43.938789
4 C -19.939626 -43.924541
5 D -19.95529 -43.92953
6 E -19.9099 -43.93124
7 F -19.926191 -43.946067
9 G -19.97125 -43.96622
14 H -19.89038 -43.921734
17 I -19.88838 -43.93059
19 J -19.94305 -43.94093
Query
SELECT *
FROM San_Filial
WHERE San_Filial.Credenciada_Id IN (2,3,4,5,6,7,9,14,17)
AND ACOS(COS(RADIANS(ltrim(San_Filial.lat)))
* COS(RADIANS(convert(float, -19.926131)))
* COS(RADIANS(ltrim(San_Filial.lon))
- RADIANS(convert(float, -43.924373)))
+ SIN(RADIANS(ltrim(San_Filial.lat)))
* SIN(RADIANS(convert(float, -19.926131)))) * 6380 < 5.0
Error
Mesage 8114, Level 16, State 5, Line 1
Error converting data type varchar to float.
Someone can help me ?
Start by seeing if you have any data that is not in a correct format:
select *
from san_filia1
wHERE San_Filial.Credenciada_Id IN (2,3,4,5,6,7,9,14,17) and
(isnumeric(lat) = 0 or isnumeric(long) = 0)
From this, you'll see what is causing the problem and then you can fix it.
The correct fix is probably along these lines:
select *
from (select sf.*,
(case when isnumeric(lat) then cast(lat as float) end) as latf,
(case when isnumeric(long) then cast(long as float) end) as longf
from san_filial sf
) sf
where . . . -- use Latf and Longf instead of lat and long
You need to do the conversion inside a case statement to guarantee that it works as expected. SQL does not guarantee the ordering of statements and a filter might be applied after the calculation.

default value for column in SQL view

I have an SQL view vith many columns.
ID (land parcel ID) ex : 7465-85-7468
Building (ID of a specific building if needed) ex : 102
Room (ID of a specific room if needed) ex : 0023
I have an ID for each row, but for most of the rows, there is not Building ID or Room ID
I'd like to fill the blank rows with '000' for building, and '0000' for Room.
I could Concat these fields in a fourth field (full ID)
It tried to get a default value with "ISNULL", but it didn't work.
For Now, I have somethins like this
ID / Building / Room / Full ID
7848-05-6956 / / / 7848-05-6956--
6985-26-7485 / 102 / 0000 / 6985-26-7485-102-0000
7236-12-0145 / / 0223 / 7236-12-0145--0223
I'd need this
ID / Building / Room / Full ID
7848-05-6956 / 000 / 0000 / 7848-05-6956-000-0000
6985-26-7485 / 102 / 0000 / 6985-26-7485-102-0000
7236-12-0145 / 000 / 0223 / 7236-12-0145-000-0223
If the field is not nullable, the data may be stored as empty strings rather than NULL's.
Try doing something like the following:
SELECT x.ID, x.Building, x.Room, ID + '-' + Building + '-' + Room AS [Full ID]
FROM (
SELECT
ID
, CASE WHEN ISNULL(Building, '') = '' THEN '000' ELSE Building END AS Building
, CASE WHEN ISNULL(Room, '') = '' THEN '0000' ELSE Room END AS Room
FROM MyTable ) x
This will handle both NULL and empty string and it will allow you to build Full ID without executing the CASE statements twice.