You are on page 1of 8

SQL Functions for Calculated Column in SAP

HANA
HANA 4,052 Views

inShare6

Hello Everyone,
In this article, I would like to explain the different SQL Functions available while creating
calculated column in SAP HANA which is available in Attribute View, Analytic View,
Calculation View and Decision Tables.

Conversion Functions

String Functions

Mathematical Functions

Date Functions

Miscellaneous Functions
Please go through the below article to know the process of creating Calculated Column in
SAP HANA.
Calculated Column in SAP HANA

Conversion Functions:
INT: This function is used to convert input value to integer
Syntax: int(argument)
Example: int(2),int(abcd)
FLOAT: This function is used to convert input value to float.
Syntax: float(argument)
Example: float(3.0)
DOUBLE: This function is used to convert input value to double.
Syntax: double(argument)
Example: double(3)
SDFLOAT: This function is used to convert input value to SDFLOAT.
Syntax: sdfloat(argument)
Example: sdfloat(3)

DECFLOAT: This function is used to convert input value to DECFLOAT.


Syntax: decfloat(argument)
Example: decfloat(3)
FIXED: This function converts the given input arguments to either 8, 12 or 16 bit length
fixed value.
Syntax: fixed(argument,integer,decimal_value)
Example: fixed(3.12,8,2)
STRING: This function is used to cover the input value to string.
Syntax: string(argument)
Example: string(456)
RAW: This function is used to convert input value to RAW data type.
Syntax: raw(3.0)
Example: raw(3.0)
DATE: Using this function we can convert the input values into date format. This function
accepts inputs in different formats.
Syntax: date(int)
date(string)
date(int,int)
date(int,int,int)
date(int,int,int,int,int,int)
Example: date(2009,01,01) 2009-01-01
date(2009,01,01,12,32,24) 2009-01-01 12:32:24
date(2009), date(2009)
Note: If we give multiple integer values as input then values will be considered as YEAR,
MONTH, DATE, HOUR, MINUTE and SECONDS in the sequence.
LONGDATE: This function is used to convert the input values to date format. This similar to
DATE function.
Syntax: longdate(int)
longdate(string)
longdate(int,int)
longdate(int,int,int)
longdate(int,int,int,int,int,int)
Example: longdate(2009,01,01) 2009-01-01
longdate(2009,01,01,12,32,24) 2009-01-01 12:32:24
longdate(2009), date(2009)
Note: If we give multiple integer values as input then values will be considered as YEAR,
MONTH, DATE, HOUR, MINUTE and SECONDS in the sequence.
TIME: This function is used to convert the input values to time format.
Syntax: time(int)

time (string)
time(int,int)
time(int,int,int)
time(int,int,int,int,int,int)
Example: time(2009,01,01) 2009-01-01
time(2009,01,01,12,32,24) 2009-01-01 12:32:24
time(2009), date(2009)
Note: If we give multiple integer values as input then values will be considered as YEAR,
MONTH, DATE, HOUR, MINUTE and SECONDS in the sequence.
Proceed to the next page to continue reading

SQL Functions for Calculated Column in SAP


HANA
HANA 4,052 Views

inShare6

String Functions:
STRLEN: This function returns the length of the given input string.
Syntax: strlen(string)
Example: strlen(abcd) 4
MIDSTR: This function is used to return the string from the input string based on the
specified number of characters.
Syntax: midstr(string,start_position,no_of_characters)
Example: midstr(Hello Everyone3,2) ll (start position is always counted from
1 (not from 0)).
LEFTSTR: This function return the number of characters from left side of the input string
based on the no of characters specified. If no of characters are higher than the length of the
input string, then entire string will be displayed as output.
Syntax: leftstr(string,integer)
Example: leftstr(Hello,3) Hel
Leftstr(Hello,7) Hello (integer value is higher than input string
length)
RIGHTSTR: This function return the number of characters from right side of the input string
based on the no of characters specified. If no of characters are higher than the length of the

input string, then entire string will be displayed as output.


Syntax: rightstr(string,integer)
Example: rightstr(Hello,3) llo
righttstr(Hello,7) Hello (integer value is higher than input string
length)
INSTR: This function returns the first occurrence of the specified string of the input string.
If the specified string is not found in the input string, then it results 0.
Syntax: instr(string,specified_string)
Example: instr(Hello_World,_) 6
Instr(Hello_Magic_World,_) 6 (the first occurrence of _ is still 6)
Instr(Hello World,_) 0 ( _ is not found in input string)
HEXTORAW: This function converts hexadecimal representation of bytes to string of bytes.
The hexadecimal string may contain 0-9, upper or lowercase a-f and no spaces between the
two digits of a byte; spaces between bytes are allowed.
Syntax: hextoraw(string)
Example: hextoraw(Hello)
RAWTOHEX: This function converts a string of bytes to its hexadecimal representation.
The output will contain only 0-9 and (upper case) A-F, no spaces and is twice as many bytes
as the original string.
Syntax: rawtohex(string)
Example: rawtohex(hello)
LTRIM: This function is used to remove white spaces on the left side of the input string. As
optional we can also specify removable character instead by white spaces. This functions
operates on raw bytes of the UTF8-string and has no knowledge of multi byte codes (you
may not specify multi byte whitespace characters).
Syntax: ltrim( string)
Ltrim(string,string)
Example: ltrim(

Hello) Hello

ltrim(_Hello,_) Hello
RTRIM: This function is used to remove white spaces on the right side of the input string.
As optional we can also specify removable character instead by white spaces. This functions
operates on raw bytes of the UTF8-string and has no knowledge of multi byte codes (you
may not specify multi byte whitespace characters).
Syntax: rtrim( string)
rtrim(string,string)
Example: rtrim(Hello

) Hello

rtrim(Hello_,_) Hello
TRIM: This functions removes white spaces from both sides of the input string. As optional
we can also specify removable character instead by white spaces.

Syntax: trim( string)


trim(string,string)
Example: trim(

Hello

) Hello

trim(_Hello_,_) Hello
LPAD: This function is used to add white spaces to the left side of the input string until the
string length reached to the specified number. We can also use specific string for padding
instead of white spaces. This function operated on UTF-8 bytes and has no knowledge of
unicode characters (neither for the whitespace string nor for length computation).
Syntax: lpad(string,integer)
lpad(string,integer,string)
Example: lpad(Hello,10)

Hello (five white spaces has been added to the

left side)
lpad(Hello,10,0) 00000Hello (Five zeros has been added to the
left side instead of white spaces)
RPAD: This function is used to add white spaces to the right side of the input string until
the string length reached to the specified number. We can also use specific string for
padding instead of white spaces. This function operated on UTF-8 bytes and has no
knowledge of unicode characters (neither for the whitespace string nor for length
computation).
Syntax: rpad(string,integer)
rpad(string,integer,string)
Example: rpad(Hello,10) Hello

(five white spaces has been added to the

right side)
rpad(Hello,10,0) Hello00000 (Five zeros has been added to the
right side instead of white spaces)
REPLACE: This function replaces every occurrence of specified string in the input string with
mentioned replacement string.
Syntax: replace(string,search_string,replace_string)
Example: replace(Hello,l,b) Hebbo (All the occurrences of l has been
replaced with b)
Mathematical Functions:
SIGN: This functions returns values -1,0 or 1 depending on the sign of the input value. This
can be used on the values with data types of all numeric, date and time.
Syntax: sign(double)
sign(time)
sign(time)
Example: sign(-678) -1 (the input value is negative)
sign(2009-01-01) 1 (the input value is positive)

ABS: This function returns the value if the input value is either zero or positive. If the input
value is negative then the result is again multiplied with negative. This can used for data
types of all numeric and time.
Syntax: abs(double)
abs(decfloat)
abs(time)
Example: abs(678) 678
abs(-678) 678
ROUND: This function does rounding of absolute values toward zero while the sign is
retained.
Syntax: round(double,integer)
Example: round(123.456, 0) = 123
round(123.456, 1) = 123.5
round(-123.456, 1) = -123.5
round(123.456, -1) = 120
ROUNDDOWN: This function rounds toward negative infinity making rounddown(-1.1, 0) =
-2.
Syntax: rounddown(double,integer)
Example: rounddown(123.456, -1) = 120
rounddown(-123.456, -1) = -130
Proceed to the next page to continue reading
Date Functions:
UTCTOLOCAL: This function converts the UTC time local time. We can also specify the input
time zone instead of UTC.
Syntax: utctolocal(date,timezone)
Example: utctolocal(2015-07-07 16:03:00,PST)
LOCALTOUTC: This function converts the local time to UTC time. We can also specify the
output time zone instead of UTC.
Syntax: localtoutc(date,timezone)
Example: localtoutc((2015-07-07 16:03:00,IST)
WEEKDAY: This function returns the weekday as number from the given date ranges from
0 to 6 (0 is Monday).
Syntax: weekday(date)
Example: weekday(2015-07-07) 1 (Tuesday)

NOW(): This function returns the current date and time (this is local time on the server
time zone).
Syntax: now()
Example: now() 2015-07-07 13:45:21 (local time)
DAYSBETWEEN: This function returns the number of days (integer) between date1 and
date2. The works as date2 date1.
Instead of rounding or checking for exactly 24 hours distance, this truncates
both date values today precision and subtract the resulting day numbers, meaning that if
arg2 is not the calendar day following arg1, daysbetween returns 1 regardless of the time
components of arg1 and arg2.
Syntax: daysbetween (date1, date2)
Example: daybetween(2015-07-07,2015-07-10) 3
daybetween(2015-07-07,2015-07-10) 3
SECONDSBETWEEN: This function returns the number of seconds the first to the second
arg, as a fixed point number. The returned value is positive if the first argument is less than
the second. The return values are fixed18.0 in both cases (note that it may prove more
useful to use fixed11.7 in case of longdate arguments).
Syntax: secondsbetween(time1,time2)
Example: secondsbetween(2015-07-07 13:50:50, 2015-07-07 13:55:50) 300
secondsbetween(2015-07-07 13:50:50, 2015-07-07 13:55:50) 300
COMPONENT: This function is used to extract individual components from input date or
time by specifying integer values range from 1 to 6 in the sequence YEAR, MONTH, DATE ,
HOUR, MINUTE and SECONDS. If the input column is not date or time then this returns 1 for
month and day and 0 for other components.
Syntax: component(date,integer)
Example: component(2015-07-06 12:34:56,1) 2015 (Year)
component(2015-07-06 12:34:56,2) 07 (Month)
component(2015-07-06 12:34:56,3) 06 (Day)
component(2015-07-06 12:34:56,4) 12 (Hour)
component(2015-07-06 12:34:56,5) 34 (Minutes)
component(2015-07-06 12:34:56,6) 56 (Seconds)
ADDSECONDS: This function returns a date plus a number of seconds. Fractional seconds
is used in case of longdate. Null handling is (in opposition to the default done with adds) to
return null if any argument is null.
Syntax: addseconds(date,integer)

ADDDAYS: This function returns a date plus a number of days. Null handling is (in
opposition to the default done with adds) to return null if any argument is null.
Syntax: adddays(date,integer)
Miscellaneous Functions:
IF: This function returns argument2 if the specified condition is true else it returns
argument 3. Currently, no shortcut evaluation is implemented, meaning that both arg2 and
arg3 are evaluated in any case. This means you cannot use if to avoid a divide by zero error
which has the side effect of terminating expression evaluation when it occurs.
Syntax: if(condition,argument2,argument3)
Example: if(QUANTITY>=1000,HIGH SALE,LOW SALE)
if(QUANTITY>=1000,HIGH SALE,if(QUANTITY>=500 AND
QUANTITY<= 1000,MEDIUM SALE,LOW SALE)) Nested if condition is also possible.
IN: This function returns 1 if the initial argument is equal to any of the remaining
arguments, else returns 0.
Syntax: in(arg1,.)
Example: in(EMP_ID,123,124) Results 1 for the records with EMP_ID 123,
124 and 0 for others.
CASE: This function returns value1 if argument1 equals to condition1, value2 if argument1
equals to condition2 etc, default if there is no match.
Syntax: case(agr1,default)
case(arg1,cnd1,val1,cnd2,val2..,default)
Example: case(EMP_ID,123,1,124,2,0) if the EMP_ID is 123 then 1, if
EMP_ID is 124 then 2 else 0.
ISNULL: This function returns 1 if the specified column has null value else 0.
Syntax: isnull(arg)
Example: isnull(EMP_ID)
Thank you for reading and hope this information is helpful. Please do share with your friends
if you feel the information is useful.
Happy Learning.

You might also like