Converting number to date data types

ABAP seems to disregard leading zeros when implicit number conversions or even when N to D conversions.

Let's say that while creating a new customer-exit variable, you need to combine a full 8 character date from standalone values for year, month and day i.e. '20151201'

Depending on how you declare variables, result may or may not be just as expected. This works OK if you have two digit day or month number as 20171231. However, problem arise if your day or month number has only one number, so a leading zero must be used.
For example, lets check inline declaration:


DATA(lv_day)   =  01.

It does not matter how many leading zeroes you add, ABAP will always disregard those and result will be not as expected:




In that case, it is important to always specifically define length of each of the variables. We can even come back to old-fashion declaration to give it more clarity:


lv_day   TYPE NUM02,
lv_month TYPE NUM02,
lv_year  TYPE NUMC4.

Or if we want inline declaration, a little more busy looking code will do the same:


DATA(lv_day)   = CONV NUM02( 01 ).
DATA(lv_month) = CONV NUM02( 01 ).
DATA(LV_YEAR)  = CONV NUMC4( 2017 ).

This way, leading zeros will always be in place.


Comments

Popular posts from this blog

"Working days" and factory calendar in ABAP

Debugging planning functions in SAP BI-IP