This document describes how to extract Year and Month details from Timestamp fields in an analytic view.
I saw a question posted here http://scn.sap.com/thread/3520511 asking for the same and tried it out but saw that the standard YEAR() function that I used to use for extracting year from timestamp details was not working when I was defining my new calculated column.
It appears that only the functions provided there can be used. So this document desctibes how to extract Year and Month from a timestamp using the limited capability of Analytic view’s calculation functions.
So in my scenario here, I created a simple analytical view with 3 fields. The important field here is TIMESTAMP which holds the value where I need to get year and month from.
A quick data preview shows how data looks like for this view.
So first thing I do here is to convert this timestamp value to a string.
To do this, I define a new column TS_STRING and use the string() function as shown below:
Save activate and do a data preview. You will get the below output.
Notice that the new field shows the timestamp value converted to a string type.
What this allows us to do now is use the existing string functions to extract year and month from this field. Notice that the year can
be derived from the first 4 characters of this field and the month can be extracted from the 6th and 7th character.
Use the leftstr() function to extract the first 4 string literals from TS_STRING. Define as below:
Now, use the midstr() function to extract month from the 6th character to the next 2 characters, i.e 6th and 7th character
Now you get the required output.
Again, you can avoid creating a separate TS_STRING and combine the string conversion and substring functions in the YEAR and MONTH
field definitions itself.
I just created a separate field for more clarity.
Do let me know if there are better ways to do this.
Cheers!!!
Shyam Uthaman