Store a combination of texts and variables in a single variable - abap

I am doing a program in ABAP to display system date in differed formats like DDMMYYYY and YYYYDDMM.
The snippet is :
WRITE: / 'DATE IN DDYYYYMM FORMAT IS',SY-DATUM+6(2),'.',SY-DATUM+0(4),'.',SY-DATUM+4(2).
which produces this output :
DATE IN DDYYYYMM FORMAT IS 27 . 2007 . 07
Is it possible to store DATE IN DDYYYYMM FORMAT IS 27 . 2007 . 07 in a variable say c so that when I write the code WRITE : c. it outputs DATE IN DDYYYYMM FORMAT IS 27 . 2007 . 07?

You can use the character string operator && to join texts and variables :
DATA: date_text TYPE c LENGTH 10.
date_text = sy-datum+6(2) && '.' &&
sy-datum+0(4) && '.' &&
sy-datum+4(2).
WRITE: / 'DATE IN DDYYYYMM FORMAT IS',
date_text.
And the output will be:
DATE IN DDYYYYMM FORMAT IS 17.2019.07
PS: for WRITE, you can add NO-GAP to each element so that the extra space is removed:
WRITE: / 'DATE IN DDYYYYMM FORMAT IS',
sy-datum+6(2) NO-GAP,
'.' NO-GAP,
sy-datum+0(4) NO-GAP,
'.' NO-GAP,
sy-datum+4(2) NO-GAP.
Output will be like above.

A handy approach for stuff like this is using string templates:
DATA(lv_date) = |{ sy-datum+6(2) }.{ sy-datum+0(4) }.{ sy-datum+4(2) }|.
WRITE: / |DATE IN DDYYYYMM FORMAT IS { lv_date }|.

Related

How to convert interger numbers into brazilain price format

i have integer numbers like below on left side in a file for products and need to convert them into brazilaian format like on right side.
100 =>100,00
84 => 84,00
1011 => 1.011,00
Use number_format() -
echo number_format(100, 2, ',', '.');
echo number_format(84, 2, ',', '.');
echo number_format(1011, 2, ',', '.');
Output -
100,00
84,00
1.011,00
Docs

CX_SY_CONVERSION_NO_NUMBER during a calculation

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!!!

Format output in bteq in teradata

I am using below bteq to fetch data .
.Set Separator '|'
.SET TITLEDASHES OFF;
.SET NULL AS ''
.EXPORT REPORT FILE = /app2/test.txt
sel
emp_id,
float_perc,
CAST( 0 AS DECIMAL(18.2) ) AS var
from emp
I am getting below output:
5|.99|.00
4|.78|.00
But we want output in below format:
5|0.99|0.00
4|0.78|0.00
Can anyone please help on this.
Can we replace |. with |0. in unix (Sun OS) with sed or tee?
Can you use a FORMAT?
sel
emp_id,
float_perc (FORMAT '9.99'),
CAST( 0 AS DECIMAL(18.2) FORMAT '9.99') AS var
from emp

DB2: How to validate date in String format which is a varchar data type

I need to know wheather a data in a column of type varchar is in correct date format or not .
I have to do the same in DB2.
I have done this in java by using SimpleDateFormat() and Date.Parse() functions with the help of Exception handling .
I'm posting my java code to validate a string to date
private boolean isValidDate(String date) {
try{
DateFormat d = new SimpleDateFormat("yyyyMMdd");
d.setLenient(false);
d.parse(date);
return true;
}
catch(Exception e){
return false;
}
}
but now i have to do the same in DB2 data base.Can i use any functions or procederes in DB2
my data in table column is like this..
20140231
20000101
.
.
.
yyyyMMdd
and column type is varchar
My style of display the date:
try
{
Date dob = new SimpleDateFormat("yyyy-MMM-dd").parse(request.getParameter("date"));
user.setDate(date);
}
catch(ParseException e)
{
e.printStackTrace();
}
note: if you should give like as yyyy-MMM-dd or MM/dd/yyyy
your programs as:
private boolean isValidDate(String date)
{
try
{
DateFormat d = new SimpleDateFormat("MMM/dd/yyyy").parse(request.getParameter("date"));
d.setLenient(false);
d.parse(date);
return true;
}
catch(Exception e)
{
return false;
}
}
Timestamp to varchar:
timestamp-expression
An expression that returns a value that must be a DATE or TIMESTAMP, or a valid string representation of a date or timestamp that is not a CLOB or DBCLOB.
If the argument is a string, the format-string argument must also be specified.
In a Unicode database, if a supplied argument is a graphic string representation of a data, time, or timestamp, it is first converted to a character string before evaluating the function.
If timestamp-expression is a DATE or a valid string representation of a date, it is first converted to a TIMESTAMP(0) value, assuming a time of exactly midnight (00.00.00).
For the valid formats of string representations of datetime values, see "String representations of datetime values" in "Datetime values".
format-string
The expression must return a value that is a built-in CHAR, VARCHAR, numeric, or datetime data type.
If the value is not a CHAR or VARCHAR data type, it is implicitly cast to VARCHAR before evaluating the function.
In a Unicode database, if the supplied argument is a GRAPHIC or VARGRAPHIC data type, it is first converted to VARCHAR before evaluating the function. The actual length must not be greater than 254 bytes (SQLSTATE 22007).
The value is a template for how timestamp-expression is to be formatted.
A valid format-string must contain a combination of the format elements listed below (SQLSTATE 22007).
Two format elements can optionally be separated by one or more of the following separator characters:
minus sign (-)
period (.)
slash (/)
comma (,)
apostrophe (')
semi-colon (;)
colon (:)
blank ( )
note:
click 1
click2
UPDATE:
sample code 1: Convert the current date to YYYYMM format
SELECT DATE_FORMAT(NOW(), '%Y%m');
o/p: # 201403
sample code 2: Convert the current date to YYYYMM format
SELECT VARCHAR_FORMAT(CURRENT_DATE, 'YYYYMM') FROM jmail;
o/p # 201403
sample:
No | MySQL | DB2 |SampleOutput
1 | DATE_FORMAT(NOW(), '%Y-%m-%d) | VARCHAR_FORMAT(CURRENT_DATE,'YYYY-MM-DD')| 2013-02-14
2 | DATE_FORMAT(NOW(), '%d/%m/%y')| VARCHAR_FORMAT(CURRENT_DATE,'DD/MM/RR') | 14/02/13
It's not clear what "flavor" of DB2 is needed. With DB2 for i, I'd probably create a function to do the test and return an indication of success or failure. Here's an example that works for me:
DROP SPECIFIC FUNCTION SQLEXAMPLE.CHKVCDATE ;
SET PATH "QSYS","QSYS2","SYSPROC","SYSIBMADM","SQLEXAMPLE" ;
CREATE FUNCTION SQLEXAMPLE.CHKVCDATE (
VCDATE VARCHAR(20) )
RETURNS INTEGER
LANGUAGE SQL
SPECIFIC SQLEXAMPLE.CHKVCDATE
DETERMINISTIC
CONTAINS SQL
RETURNS NULL ON NULL INPUT
NO EXTERNAL ACTION
NOT FENCED
SET OPTION ALWBLK = *ALLREAD ,
ALWCPYDTA = *OPTIMIZE ,
COMMIT = *CHG ,
CLOSQLCSR = *ENDMOD ,
DECRESULT = (31, 31, 00) ,
DFTRDBCOL = *NONE ,
DYNDFTCOL = *NO ,
DYNUSRPRF = *USER ,
SRTSEQ = *HEX
BEGIN ATOMIC
DECLARE CHKDATE DATE ;
DECLARE VALIDDATE INT ;
DECLARE NOT_VALID CONDITION FOR '22007' ;
DECLARE CONTINUE HANDLER FOR NOT_VALID
SET VALIDDATE = -1 ;
SET VALIDDATE = 0 ;
VALUES ( VCDATE ) INTO CHKDATE ;
RETURN VALIDDATE ;
END ;
COMMENT ON SPECIFIC FUNCTION SQLEXAMPLE.CHKVCDATE
IS 'Check VARCHAR for valid date' ;
Any properties or other details that don't fit your particular DB2 can be removed or changed. The size of the VCDATE VARCHAR parm might need adjustment, and the RETURN value can be whatever you need. The function might be useful in a WHERE clause.
I can invoke it like this:
select sqlexample.chkvcdate('2014-02-29'), sqlexample.chkvcdate('2014-02-28') from sysibm.sysdummy1
The first will return ( -1 ) for the invalid value, and the second will return ( 0 ) for valid.

Converting CYYMMDD to SAS Date in DB2 via SAS

I'm looking to convert a date that is in the format of CYYMMDD (where C is either 0 for 20th century or 1 for 21st century) to a standard SAS date. This code will be placed inside of a SAS query using 'proc sql' so that it can compare a SAS date against a date stored in DB2.
Example: Input data=1130101, Output='1Jan2013'd
Examples I've tried are:
(substr(t1.'EffectDate'n,4,2)|| '/' || substr(t1.'EffectDate'n,6,2) || '/' || cast(substr(t1.'EffectDate'n,1,3) AS INTEGER) + 1900)
That fails to the cast() function (appears it doesn't exist?)
Also tried:
convert(varchar(10), convert(datetime, right(t1.'EffectDate'n, 6), 12), 101)
But varchar(10) doesn't exist.
My query looks like this:
proc sql;
create table CLAIMS as select
t1.CID,
t1.MID,
t1.DOS
OTHER_TABLE.ChangeDate AS EffectDate
FROM
SOURCE.REJECTED t1
INNER JOIN
EGTASK.OTHER_TABLE
ON
t1.DOS >= *Converted_Date*
[... goes on a couple more lines...]
Where *Converted_Date* is what I need.
(However, I should clarify that this particular query/join doesn't necessarily need to be SQL)
To convert your variable from it's current coded format into a proper SAS date variable, you will need to turn it into a character string and then read the result using the INPUT function. For example:
data _null_;
do EffectDate = 1130101,0130101;
cEffectDate = put(EffectDate,z7.);
if substr(cEffectDate,1,1) = '0'
then SASEffectDate = input('19' || substr(cEffectDate,2),yymmdd8.);
else SASEffectDate = input('20' || substr(cEffectDate,2),yymmdd8.);
put EffectDate=
/ SASEffectDate=
/ ;
end;
format SASEffectDate yymmdd10.;
run;
This is just an illustration and a bit long-winded; it creates a new SAS variable named SASEffectDate to preserve the original variable. Once you have it as a SAS variable, you don't need to do anything else; the SAS Access product will know how to make the references to the external database.
Here is an example of doing something similar using PROC SQL:
data have; /* Just a dummy data set for illustration */
do EffectDate = 1130101,0130101;
i+1;
output;
end;
run;
proc sql;
create table want as
select t2.*
, case when t2.EffectDate < 999999 /* starts with 0 */
then input('19' || substr(put(EffectDate,z7.),2),yymmdd8.)
else input('20' || substr(put(EffectDate,z7.),2),yymmdd8.)
end as SASEffectDate format=yymmdd10.
from have t2
;
quit;