SAP SQL TIMESTAMP FUNC



Get Example source ABAP code based on a different SAP table
  


• TSTMP_IS_VALID ABAP_OSQL_FUNC
• TSTMP_CURRENT_UTCTIMESTAMP ABAP_OSQL_FUNC
• TSTMP_SECONDS_BETWEEN ABAP_OSQL_FUNC
• TSTMP_ADD_SECONDS ABAP_OSQL_FUNC
• UTCL_ADD_SECONDS ABAP_OSQL_FUNC
• UTCL_SECONDS_BETWEEN ABAP_OSQL_FUNC
• UTCL_CURRENT ABAP_OSQL_FUNC

ABAP_SQLFUNC - Time Stamp Functions

ABAP_SYNTAX_FORMS

GenericTime Stamp Functions
1 ... IS_VALID( date$|time$|utclong )
$| EXTRACT_YEAR( date$|utclong )
$| EXTRACT_MONTH( date$|utclong )
$| EXTRACT_DAY( date$|utclong )
$| EXTRACT_HOUR( time$|utclong )
$| EXTRACT_MINUTE( time$|utclong )
$| EXTRACT_SECOND( time$|utclong )
$| DAYNAME( date$|utclong )
$| MONTHNAME( date$|utclong )
$| WEEKDAY( date$|utclong )
$| DAYS_BETWEEN( ${date1$|utclong1$},${date2$|utclong2$} )
$| ADD_DAYS( ${date$|utclong$},days )
$| ADD_MONTHS( ${date$|utclong$},months ) ...

Functionsfor UTCLONG
2 ... UTCL_CURRENT( )
$| UTCL_ADD_SECONDS( utclong,seconds )
$| UTCL_SECONDS_BETWEEN( utclong1,utclong2 ) ...

Functionsfor TIMESTAMP
3 ... TSTMP_IS_VALID( tstmp )
$| TSTMP_CURRENT_UTCTIMESTAMP( )
$| TSTMP_SECONDS_BETWEEN( tstmp1 = tstmp1,
tstmp2 = tstmp2$[,
on_error = on_error$] )
$| TSTMP_ADD_SECONDS( tstmp = tstmp,
seconds = seconds$[,
on_error = on_error$] ) ...

What does it do?
These SQL functions perform operations on time stamps. The first set covers generic functions, the second set covers functions depending on the built-in data type UTCLONG, and the third set covers functions depending on the data element TIMESTAMP.
The arguments of the functions are specified as a comma-separated list in parentheses. A blank must be placed after the opening parenthesis and in front of the closing parenthesis. Optional parameters can be assigned actual parameters when called. Non-optional parameters must be assigned actual parameters.
SQL expressions of matching data types can be specified as actual parameters. Only enumerated constants of specific classes can be passed to the parameter on_error for the TIMESTAMP functions TSTMP_SECONDS_BETWEEN and TSTMP_ADD_SECONDS. If an actual parameter contains the null value , every function except IS_VALID and TSTMP_IS_VALID returns a null value.

Generic Time Stamp Functions

ABAP_SYNTAX
... IS_VALID( date$|time$|utclong )
$| EXTRACT_YEAR( date$|utclong )
$| EXTRACT_MONTH( date$|utclong )
$| EXTRACT_DAY( date$|utclong )
$| EXTRACT_HOUR( time$|utclong )
$| EXTRACT_MINUTE( time$|utclong )
$| EXTRACT_SECOND( time$|utclong )
$| DAYNAME( date$|utclong )
$| MONTHNAME( date$|utclong )
$| WEEKDAY( date$|utclong )
$| DAYS_BETWEEN( ${date1$|utclong1$},${date2$|utclong2$} )
$| ADD_DAYS( ${date$|utclong$},days )
$| ADD_MONTHS( ${date$|utclong$},months ) ...

What does it do?
These SQL functions perform operations with arguments of the built-in data types DATN, DATS, TIMN, TIMS, and UTCLONG. The generic functions are documented in date functions and time functions.



Latest notes:

NON_V5_HINTS
All generic functions enforce ABAP_STRICT_783 strict mode from ABAP_RELEASE ABAP_783 / .
ABAP_HINT_END

Functions for UTCLONG

ABAP_SYNTAX
... UTCL_CURRENT( )
$| UTCL_ADD_SECONDS( utclong,seconds )
$| UTCL_SECONDS_BETWEEN( utclong1,utclong2 ) ...

ABAP_VARIANTS:
1 ... UTCL_CURRENT( )
2 ... UTCL_ADD_SECONDS( utclong,seconds )
3 ... UTCL_SECONDS_BETWEEN( utclong1,utclong2 )

What does it do?
These SQL functions perform operations with arguments of the built-in data type UTCLONG.



Latest notes:

NON_V5_HINTS
The use of the functions UTCL_CURRENT( ), UTCL_ADD_SECONDS , and UTCL_SECONDS_BETWEEN enforces the ABAP_STRICT_778 strict mode from ABAP_RELEASE ABAP_778 / .
ABAP_HINT_END

ABAP_VARIANT_1 ... UTCL_CURRENT( )

What does it do?
This function generates a UTC time stamp from the system time and the system date of AS ABAP in accordance with POSIX. The return value has the built-in dictionary type UTCLONG.
All columns of a query have the same time stamp.

ABAP_EXAMPLE_VX5
Selects column CARRID from table SCARR and returns a time stamp for each row. The time stamp is generated only once for the query and, therefore, all rows have the same time stamp.
ABEXA 01426
ABAP_EXAMPLE_END

ABAP_VARIANT_2 ... UTCL_ADD_SECONDS( utclong,seconds )

What does it do?
The function UTCL_ADD_SECONDS adds seconds seconds to a time stamp utclong. It has two positional parameters. The actual parameter for the formal parameter utclong must have the built-in dictionary type UTCLONG and contain a valid time stamp in the format YYYYMMDDHHMMSSMMMUUUN. An invalid time stamp produces the initial value. The actual parameter for the formal parameter seconds must have either the built-in data type INT4 or the data type DEC with length 21 and 7 decimal places. Any negative values are subtracted. If the result is invalid, a catchable exception of class CX_SY_OPEN_SQL_DB is raised.
The result is of type UTCLONG.

ABAP_VARIANT_3 ... UTCL_SECONDS_BETWEEN( utclong1,utclong2 )

What does it do?
The function UTCL_SECONDS_BETWEEN calculates the difference between two specified time stamps utcl1 and utcl2 in seconds. It has two positional parameters. The actual parameters for the formal parameters utcl1 and utcl2 must have the built-in dictionary type UTCLONG and contain a valid time stamp in the format YYYYMMDDHHMMSSMMMUUUN. Any invalid time stamps produce an error. If utcl2 is greater than utcl1, the result is positive. If the values are identical, the result is 0. In all other cases, the result is negative.
The result is of type DEC with length 21 and 7 decimal places.

Functions for TIMESTAMP

ABAP_SYNTAX
... TSTMP_IS_VALID( tstmp )
$| TSTMP_CURRENT_UTCTIMESTAMP( )
$| TSTMP_SECONDS_BETWEEN( tstmp1 = tstmp1,
tstmp2 = tstmp2$[,
on_error = on_error$] )
$| TSTMP_ADD_SECONDS( tstmp = tstmp,
seconds = seconds$[,
on_error = on_error$] ) ...

ABAP_VARIANTS:
1 ... TSTMP_IS_VALID( tstmp )
2 ... TSTMP_CURRENT_UTCTIMESTAMP( )
3 ... TSTMP_SECONDS_BETWEEN( ... )
4 ... TSTMP_ADD_SECONDS( ... )

What does it do?
These SQL functions perform operations with arguments of the data element TIMESTAMP.



Latest notes:

NON_V5_HINTS
The use of the functions TSTMP_IS_VALID, TSTMP_CURRENT_UTCTIMESTAMP( ), TSTMP_SECONDS_BETWEEN, and TSTMP_ADD_SECONDS enforces the ABAP_STRICT_771 strict mode from ABAP_RELEASE ABAP_771 / .
ABAP_HINT_END

ABAP_VARIANT_1 ... TSTMP_IS_VALID( tstmp )

What does it do?
The function TSTMP_IS_VALID determines whether an argument tstmp contains a valid time stamp in the format YYYYMMDDHHMMSS. It has one positional parameter. The actual parameter must have the built-in data type DEC with length 15 and no decimal places. The result has the data type INT4. A valid time stamp produces the value 1 and all other input values (including the null value) produce the value 0.

ABAP_VARIANT_2 ... TSTMP_CURRENT_UTCTIMESTAMP( )

What does it do?
The function TSTMP_CURRENT_UTCTIMESTAMP returns a UTC time stamp in accordance with the POSIX standard. The result has the data type DEC with length 15 and no decimal places.



Latest notes:

The UTC time stamp is created from the clock on the database server. The system date and the system time in AS ABAP from which its time stamp is created using GET TIME STAMP are synchronized with the database server clock.
If the function TSTMP_CURRENT_UTCTIMESTAMP is used more than once within an AB-SQL statement, it cannot be guaranteed that every call within a database access produces the same result. Hence, later calls can produce later time stamps.
NON_V5_HINTS
ABAP_HINT_END

ABAP_VARIANT_3 ... TSTMP_SECONDS_BETWEEN( ... )

What does it do?
The function TSTMP_SECONDS_BETWEEN calculates the difference between two specified time stamps, tstmp1 and tstmp2 in seconds. All parameters are keyword parameters. The actual parameters for the formal parameters tstmp1 and tstmp2 must have the built-in data type DEC with length 15 and no decimal places and contain valid time stamps in the format YYYYMMDDHHMMSS. Any invalid time stamps produce an error. If tstmp2 is greater than tstmp1, the result is positive. In the reverse case, it is negative.
The optional parameter on_error affects how errors are handled. The parameter for on_error must be an enumerated object with the enumerated type ON_ERROR from the class SQL_TSTMP_SECONDS_BETWEEN and the following enumerated constants can be passed:
SQL_TSTMP_SECONDS_BETWEEN=>FAIL: An error raises an exception (default).
SQL_TSTMP_SECONDS_BETWEEN=>SET_TO_NULL: An error returns the null value.

ABAP_VARIANT_4 ... TSTMP_ADD_SECONDS( ... )

What does it do?
The function TSTMP_ADD_SECONDS adds seconds seconds to a time stamp tstmp. All parameters are keyword parameters. The actual parameter for the formal parameter tstmp must have the built-in data type DEC with length 15 and no decimal places and contain a valid time stamp in the format YYYYMMDDHHMMSS. An invalid time stamp produces an error. The actual parameter for the formal parameter seconds must also have the built-in data type DEC with length 15 and no decimal places. Negative values are subtracted. If the result is invalid, an error occurs.
The optional parameter on_error affects how errors are handled. The parameter for on_error must be an enumerated object with the enumerated type ON_ERROR from the class SQL_TSTMP_ADD_SECONDS and the following enumerated constants can be passed:
SQL_TSTMP_ADD_SECONDS=>FAIL: An error raises an exception (default).
SQL_TSTMP_ADD_SECONDS=>SET_TO_NULL: An error returns the null value.

ABAP_EXAMPLE_VX5
Applies the time stamp functions to columns of the DDIC database table DEMO_EXPRESSIONS. The class CL_DEMO_SQL_TIMESTAMP_FUNC executes this access to the table and displays the result. The column NUM1 of the DDIC database table is given a value that is added to a time stamp in the column TIMESTAMP1 as seconds. The difference is calculated between this sum and a time stamp retrieved on the database by the function TSTMP_CURRENT_UTCTIMESTAMP. A delay wait can be integrated between the time stamp in the ABAP program and the time stamp created on the database. The number of seconds specified as type-compliant literal in the variable pack is added to the time stamp utcl, which has the type UTCLONG. Finally, the difference in seconds between two time stamps of type UTCLONG is calculated. Since utcl1 is greater than utcl2, the result is negative.
ABEXA 01286
ABAP_EXAMPLE_END