SAP Tutorials Blog


 


"Since the content is character-like, you can use string processing functionalities
"to access date values. This also applies to time fields of type t.
"Some examples:
"Extracting date values using the string function substring.
DATA some_date TYPE d VALUE '20240102'.
DATA(year_sub) = substring( val = some_date off = 0 len = 4 ). "2024
DATA(month_sub) = substring( val = some_date off = 4 len = 2 ). "01
DATA(day_sub) = substring( val = some_date off = 6 len = 2 ). "02

"Replacing using the string function replace
DATA(year_repl)  = replace( val = some_date off = 0 len = 4 with = `2025` ). "20250102

"Offset and length specifications
"Read access
DATA(off_len_spec_year) = some_date(4). "2024
DATA(off_len_spec_year_2) = some_date+0(4). "2024
DATA(off_len_spec_month) = some_date+4(2). "01
DATA(off_len_spec_day) = some_date+6(2). "02
"Write access
some_date+4(2) = '10'. "20241002

"Excursion: Comparison rules for character-like date types
"The following example specifies an internal table that has a component
"of type d. Using a ranges table and SELECT statements different table
"entries are retrieved.
"Note the comparison rule for type d (and type t): Content is compared
"from left to right, and the first different character determines which
"operand is greater.

TYPES: BEGIN OF str_w_date,
          num  TYPE i,
          date TYPE d,
        END OF str_w_date,
        tab_type_w_date TYPE TABLE OF str_w_date WITH EMPTY KEY.

DATA(date_tab) = VALUE tab_type_w_date(
    ( num = 1 date = '20241017' )
    ( num = 2 date = '20241030' )
    ( num = 3 date = '20241101' )
    ( num = 4 date = '20241105' )
    ( num = 5 date = '20241112' )
    ( num = 6 date = '20241201' )
    ( num = 7 date = '20241215' )
    ( num = 8 date = '20241227' )
    ( num = 9 date = '20241231' )
    ( num = 10 date = '20250101' )
    ( num = 11 date = '20250110' )
    ( num = 12 date = '20250203' ) ).

"Declaring a ranges table
DATA rangestab TYPE RANGE OF d.

"Populating a ranges table using VALUE
"Include sign, including all December dates in the result set and all dates
"earlier than November
rangestab = VALUE #( sign   = 'I'
                     option = 'BT' ( low = '20241201' high = '20241231' )
                     option = 'LT' ( low = '20241101' ) ).

"Using a SELECT statement and the IN addition to retrieve internal table
"content based on the ranges table specifications
SELECT num FROM @date_tab AS dtab
    WHERE date IN @rangestab
    INTO TABLE @DATA(date_result1).
"The result includes: 1, 2, 6, 7, 8, 9

"Exclude sign, excluding all December dates in the result set
rangestab = VALUE #( sign   = 'E'
                     option = 'BT' ( low = '20241201' high = '20241231' ) ).

SELECT num FROM @date_tab AS dtab
    WHERE date IN @rangestab
    INTO TABLE @DATA(date_result2).
"The result includes: 1, 2, 3, 4, 5, 10, 11, 12

Serkan AKKAVAK Computer Engineer BSc Head of SAP & Software Department Contact : serkurumsal@yandex.com