Skip to content
NextLytics
Megamenü_2023_Über-uns

Shaping Business Intelligence

Whether clever add-on products for SAP BI, development of meaningful dashboards or implementation of AI-based applications - we shape the future of Business Intelligence together with you. 

Megamenü_2023_Über-uns_1

About us

As a partner with deep process know-how, knowledge of the latest SAP technologies as well as high social competence and many years of project experience, we shape the future of Business Intelligence in your company too.

Megamenü_2023_Methodik

Our Methodology

The mixture of classic waterfall model and agile methodology guarantees our projects a high level of efficiency and satisfaction on both sides. Learn more about our project approach.

Products
Megamenü_2023_NextTables

NextTables

Edit data in SAP BW out of the box: NextTables makes editing tables easier, faster and more intuitive, whether you use SAP BW on HANA, SAP S/4HANA or SAP BW 4/HANA.

Megamenü_2023_Connector

NextLytics Connectors

The increasing automation of processes requires the connectivity of IT systems. NextLytics Connectors allow you to connect your SAP ecosystem with various open-source technologies.

IT-Services
Megamenü_2023_Data-Science

Data Science & Engineering

Ready for the future? As a strong partner, we will support you in the design, implementation and optimization of your AI application.

Megamenü_2023_Planning

SAP Planning

We design new planning applications using SAP BPC Embedded, IP or SAC Planning which create added value for your company.

Megamenü_2023_Dashboarding

Dashboarding

We help you with our expertise to create meaningful dashboards based on Tableau, Power BI, SAP Analytics Cloud or SAP Lumira. 

Megamenü_2023_Data-Warehouse-1

SAP Data Warehouse

Are you planning a migration to SAP HANA? We show you the challenges and which advantages a migration provides.

Business Analytics
Megamenü_2023_Procurement

Procurement Analytics

Transparent and valid figures are important, especially in companies with a decentralized structure. SAP Procurement Analytics allows you to evaluate SAP ERP data in SAP BI.

Megamenü_2023_Reporting

SAP HR Reporting & Analytics

With our standard model for reporting from SAP HCM with SAP BW, you accelerate business activities and make data from various systems available centrally and validly.

Megamenü_2023_Dataquality

Data Quality Management

In times of Big Data and IoT, maintaining high data quality is of the utmost importance. With our Data Quality Management (DQM) solution, you always keep the overview.

Career
Megamenü_2023_Karriere-2b

Working at NextLytics

If you would like to work with pleasure and don't want to miss out on your professional and personal development, we are the right choice for you!

Megamenü_2023_Karriere-1

Senior

Time for a change? Take your next professional step and work with us to shape innovation and growth in an exciting business environment!

Megamenü_2023_Karriere-5

Junior

Enough of grey theory - time to get to know the colourful reality! Start your working life with us and enjoy your work with interesting projects.

Megamenü_2023_Karriere-4-1

Students

You don't just want to study theory, but also want to experience it in practice? Check out theory and practice with us and experience where the differences are made.

Megamenü_2023_Karriere-3

Jobs

You can find all open vacancies here. Look around and submit your application - we look forward to it! If there is no matching position, please send us your unsolicited application.

Blog
NextLytics Newsletter Teaser
Sign up now for our monthly newsletter!
Sign up for newsletter
 

SAP HANA TO DATE - Date functions in HANA SQLScript explained

In this article you will learn some helpful HANA SQLScript date functions that you can use in your transformation routines. These functions make your work much easier because they contain predefined logic that you do not have to program manually. Here I will use examples to explain how you can solve the frequently occurring requirements.

Convert date to SAP HANA DATE format with TO_DATE

The data type DATE consists of year, month and day information to represent a date value. The standard format of this data type is YYYY-MM-DD. YYYY stands for the year, MM for the month and DD for the day. The range is between 0001-01-01 and 9999-12-31.

With the SQLScript function TO_DATE you can convert a string representing a date into the data type DATE. 

The syntax of the function is:

TO_DATE ([String], [Format])

For example:

SELECT TO_DATE('2020-09-01', 'YYYY-MM-DD') "DATE Format" FROM DUMMY;

Please note that you need the ABAP data type DATS in the format YYYYMMDD for transformation routines. Therefore, if you have an ADSO as the target of the transformation, you must use the SAP HANA function TO_DATS instead.

Convert date to ABAP format with SAP HANA function TO_DATS

In contrast to the TO_DATE function, which converts a character string into the DATE format YYYY-MM-DD, the TO_DATS function converts a string into the DATS format YYYYMMDD. This format is required to write data into objects such as ADSOs in BW.

The syntax is very simple:

TO_DATS ([String])

For example:

SELECT TO_DATS('2020-09-01') "DATS Format" FROM DUMMY;

You can combine this function with other SAP HANA functions, such as ADD_DAYS:

OUTTAB = SELECT
TO_DATS(ADD_DAYS(DATE0, 25)) AS DATE0,
CURRENCY, RECORDMODE, AMOUNT, RECORD, SQL__PROCEDURE__SOURCE__RECORD
FROM :INTAB;

Add days with SAP HANA function ADD_DAYS

A very common requirement is to add days to a date. The ADD_DAYS function makes this task easier for you. This HANA function adds the specified number of days to the given date. Months and years are also automatically incremented.

The syntax of the function is:

ADD_DAYS ([Date], [Number of days])

Where Date indicates the date to be incremented and Number of Days indicates the number of days by which the date is incremented.

In the following example we add 25 days to the respective date as part of a transformation routine:

OUTTAB = SELECT
TO_DATS(ADD_DAYS(DATE0, 25)) AS DATE0,
CURRENCY, RECORDMODE, AMOUNT, RECORD, SQL__PROCEDURE__SOURCE__RECORD
FROM :INTAB;

SQL Script Function to_dats and add_days

Add months with the ADD_MONTHS function

In addition to days, you can also add months. For this purpose use the SAP HANA function ADD_MONTHS. This adds the specified number of months to the given date.

The function syntax is:

ADD_MONTHS ([Date], [Number of months])

Where Date indicates the date to be incremented and Number of Months indicates the number of months by which the date is increased.

In the following example we add a month to the date:

OUTTAB = SELECT
TO_DATS(ADD_MONTHS(DATE0, 1)) AS DATE0,
CURRENCY, RECORDMODE, AMOUNT, RECORD, SQL__PROCEDURE__SOURCE__RECORD
FROM :INTAB;

SQL Scipt Function add_months

This also works for years, as the next example shows:

OUTTAB = SELECT
TO_DATS(ADD_MONTHS(DATE0, 12)) AS DATE0,
CURRENCY, RECORDMODE, AMOUNT, RECORD, SQL__PROCEDURE__SOURCE__RECORD
FROM :INTAB;

SQL Script Function add_months -  add 12 months

The SAP HANA function ADD_YEARS works in the same way. It adds years to the specified date.

Add current date in SAP HANA via CURRENT_DATE and CURRENT_UTCDATE

Sometimes you need the current date as part of a transformation routine. For example, to determine a key date for the currency translation.

To get the current date, you can use the CURRENT_DATE function. It returns the current local system date.

Here is an example:

OUTTAB = SELECT
TO_DATS(CURRENT_DATE) AS DATE0,
CURRENCY, RECORDMODE, AMOUNT, RECORD, SQL__PROCEDURE__SOURCE__RECORD
FROM :INTAB;

If you work in an international environment, you should use the UTC date instead of the local date. Using local time and converting between local time zones would require additional handling in the code.

Use the SAP HANA function CURRENT_UTCDATE for this:

OUTTAB = SELECT
TO_DATS(CURRENT_UTCDATE) AS DATE0,
CURRENCY, RECORDMODE, AMOUNT, RECORD, SQL__PROCEDURE__SOURCE__RECORD
FROM :INTAB;

SQL Script Function current_date

Find out months and years via SAP HANA EXTRACT function

Another common requirement is the extraction of months and years from a date. With the EXTRACT function you can easily implement this requirement. You do not have to laboriously find out the individual pieces via SUBSTRING. The function finds out the value automatically.

The syntax of the function is:

EXTRACT ({YEAR | MONTH | DAY | HOUR | MINUTE | SECOND} FROM [Date])

For example, you can read the calendar year 0CALYEAR and the calendar month 0CALMONTH2 from the date 0DATE using the parameters YEAR and MONTH:

OUTTAB = SELECT
DATE0,
EXTRACT(MONTH FROM DATE0) AS CALMONTH2,
EXTRACT(YEAR FROM DATE0) AS CALYEAR,
CURRENCY, RECORDMODE, AMOUNT, RECORD, SQL__PROCEDURE__SOURCE__RECORD
FROM :INTAB;

SQL Script extract Function

For the same purpose you can use the SAP HANA functions MONTH and YEAR. The MONTH function returns the number of the month from the specified date. The MONTH function returns the year of a given date:


OUTTAB = SELECT
DATE0,
MONTH (DATE0) AS CALMONTH2,
YEAR (DATE0) AS CALYEAR,
CURRENCY, RECORDMODE, AMOUNT, RECORD, SQL__PROCEDURE__SOURCE__RECORD
FROM :INTAB;

SQL-Script functions month and year


Increase the performance of your BW with SQLScript

Neuer Call-to-Action


Calculating the number of days between two dates with SAP HANA functions DAYS_BETWEEN and WORKDAYS_BETWEEN

Frequently also the number of days and/or working-days must be computed. For example, in order to determine the delivery reliability of a supplier. For this purpose you can use the functions DAYS_BETWEEN and WORKDAYS_BETWEEN. The DAYS_BETWEEN function returns the number of days between two date values.

The syntax is:

DAYS_BETWEEN ([Start date], [End date])

For example:

OUTTAB = SELECT
DATE0, CALDAY, CURRENCY, RECORDMODE, AMOUNT,
DAYS_BETWEEN(DATE0, CALDAY) AS "/BIC/ZDRDBTWN",
RECORD, SQL__PROCEDURE__SOURCE__RECORD
FROM :INTAB;

SQL Script Function days_between

However, it makes more sense to use working days for the calculation of delivery reliability. In this case, the WORKDAYS_BETWEEN function can be used. This function allows the factory calendar to be used for the calculation. In BEx Queries, this was previously only possible to a limited extent.

The syntax of this function is:

WORKDAYS_BETWEEN ([Factory calendar], [Start date ], [End date], [Source schema])

The Factory Calendar parameter specifies the ID of a factory calendar in the factory calendar table TFACS. The factory calendar table TFACS must be available in the SAP HANA database to use the function. In SAP BW, SAP CRM and SAP ERP systems running on a SAP HANA database, the TFACS table is located in the ABAP schema SAP<SID>. For other SAP HANA databases the table TFACS can be replicated from a SAP Business Suite system.

Factory calendar selection

The result is returned as an INTEGER number.

If the [Start Date] is less than or equal to the [End Date], then the number of working days in the period starting with the [Start Date] and ending with the [End Date] is returned. The number of days returned includes the day of the [Start Date] but excludes the day of the [End Date].

If [Start Date] is later than [End Date], a negative number of working days will be returned in the period starting with [End Date] and ending with [Start Date]. The number of days returned includes the day of [End Date] but excludes the day of [Start Date].

Below is an example:

OUTTAB = SELECT
DATE0, CALDAY, CURRENCY, RECORDMODE, AMOUNT,
WORKDAYS_BETWEEN('10', DATE0, CALDAY) AS "/BIC/ZDRDBTWN",
RECORD, SQL__PROCEDURE__SOURCE__RECORD
FROM :INTAB;

SQL Script Function workdays_between

Providing data records with a time stamp - SAP HANA functions CURRENT_TIMESTAMP and CURRENT_UTCTIMESTAMP

Sometimes it is necessary to time stamp records. You can use the TIMESTAMP function for this purpose. This function returns the current timestamp of the local system.

However, the HANA internal format for the timestamp is YYYY-MM-DD HH24:MI:SS.FF7. FF.

HANA internal timestamp format

The format of the BW time stamp 0TCTTIMSTMP, on the other hand, is YYYYMMDDHHMMSS.

Data type of the time stamp

Therefore, you have to convert the type with the TO_NVARCHAR function:

TO_NVARCHAR(CURRENT_TIMESTAMP, 'YYYYMMDDHHMMSS')

Here is an example of the transformation routine:

PROCEDURE FOR HDB LANGUAGE SQLSCRIPT OPTIONS READ-ONLY.
OUTTAB = SELECT
DATE0,
TO_NVARCHAR(CURRENT_TIMESTAMP, 'YYYYMMDDHHMMSS') AS TCTTIMSTMP,
CURRENCY, RECORDMODE, AMOUNT, RECORD, SQL__PROCEDURE__SOURCE__RECORD
FROM :INTAB;

TO_NVARCHAR  converted time stamp

If you work in an international environment, you should use UTC time instead of local timestamps. Using local time and converting between local time zones may require additional handling in the application code.

In this case, use the CURRENT_UTCTIMESTAMP function. This function selects the current UTC timestamp. For example:

OUTTAB   = SELECT
DATE0,
TO_NVARCHAR(CURRENT_UTCTIMESTAMP, 'YYYYMMDDHHMMSS')  AS TCTTIMSTMP,
CURRENCY, RECORDMODE, AMOUNT, RECORD, SQL__PROCEDURE__SOURCE__RECORD
FROM :INTAB;

SQL Script Function UTC_TIMESTAMP

SAP HANA TO DATE - Our Summary

As you can see, the switch from ABAP to SQLScript can be made easier thanks to predefined functions. These cover the most common requirements.

Would you like to learn more about SAP HANA SQLScript applications? Download our current whitepaper on this topic! If you would like to get support in defining the right HANA strategy for you, please do not hesitate to contact us. 

Learn more about  SAP HANA SQLScript

, ,

avatar

Chris

Chris Fidanidis has been working in the SAP BW environment since 2007. During these years he has implemented several planning projects and used various SAP tools such as SAP BSP, SAP BW-IP, SAP BPC, SAP BW Embedded BPC. He has gained experience primarily as a developer, architect, project manager and team leader. He enjoys playing basketball and barbecue whenever possible.

Got a question about this blog?
Ask Chris

Blog - NextLytics AG 

Welcome to our blog. In this section we regularly report on news and background information on topics such as SAP Business Intelligence (BI), SAP Dashboarding with Lumira Designer or SAP Analytics Cloud, Machine Learning with SAP BW, Data Science and Planning with SAP Business Planning and Consolidation (BPC), SAP Integrated Planning (IP) and SAC Planning and much more.

Subscribe to our newsletter

Related Posts

Recent Posts