Sunteți pe pagina 1din 35

SQL Statements in SAP HANA

SQL statements in SAP HANA are separated into 10 different categories as shown below.

Data Definition Statements (DDS)

Data Definition Statements (DDS) category covers below the SQL operations.

ALTER FULLTEXT INDEX

ALTER INDEX

ALTER SEQUENCE

ALTER TABLE

ALTER VIRTUAL TABLE

COMMENT ON

CREATE FULLTEXT INDEX

CREATE INDEX

CREATE SCHEMA

CREATE SEQUENCE

CREATE STATISTICS

CREATE SYNONYM

CREATE TABLE
CREATE TRIGGER

CREATE VIEW

DROP INDEX

DROP SCHEMA

DROP SEQUENCE

DROP STATISTICS

DROP SYNONYM

DROP TABLE

DROP TRIGGER

DROP VIEW

REFRESH STATISTICS

RENAME COLUMN

RENAME INDEX

RENAME TABLE

Please go through article Data Definition Statements in SAP HANA to know the syntax for each
of the above operations.

Data Manipulation Statements (DMS)

Data Manipulation Statements (DMS) category covers below SQL operations that can be
performed on SAP HANA database.
DELETE
EXPLAIN PLAN
INSERT
LOAD
MERGE DELTA
REPLACE | UPSERT
SELECT
TRUNCATE TABLE
UNLOAD
UPDATE
Please go through article Data Manipulation Statements in SAP HANA to know the syntax for
each of the above operations.
Procedural Statements (PS)

Procedural Statements (PS) category covers the below SQL operations that can be performed
on SAP HANA Database.
ALTER PROCEDURE RECOMPILE
CALL
CREATE FUNCTION
CREATE PROCEDURE
CREATE TYPE
CREATE VIRTUAL FUNCTION
DO BEGINEND
DROP FUNCTION
DROP PROCEDURE
DROP TYPE
Please go through article Procedural Statements in SAP HANA to know the syntax for each of
the above operations.
Transaction Management Statements (TMS)
Transaction Management Statements (TMS) category covers the below SQL operations that can
be performed on SAP HANA Database.
COMMIT
LOCK TABLE
ROLLBACK
SET TRANSACTION
Please go through article Transaction Management Statements in SAP HANA to know the
syntax for each of the above operations.

Session Management Statements (SNMS)


Session Management Statements (SNMS) category covers the below SQL operations that can
be performed on SAP HANA Database.
CONNECT
SET HISTORY SESSION
SET SCHEMA
SET [SESSION]
UNSET [SESSION]
Please go through article Session Management Statements in SAP HANA to know the syntax
for each of the above operations.
Access Control Statements (ACS)
Access Control Statements (ACS) category covers the below SQL operations that can be
performed on SAP HANA Database.
ALTER AUDIT POLICY
ALTER CREDENTIAL
ALTER REMOTE SOURCE
ALTER SAML PROVIDER
ALTER USER
CREATE AUDIT POLICY
CREATE CREDENTIAL
CREATE USER
CREATE REMOTE SOURCE
CREATE ROLE
CREATE SAML PROVIDER
DROP AUDIT POLICY
DROP CREDENTIAL
DROP REMOTE SOURCE
DROP ROLE
DROP SAML PROVIDER
DROP USER
GRANT
REVOKE
Please go through article Access Control Statements in SAP HANA to know the syntax for each
of the above operations.

Data Import Export Statements (DIES):


Data Import Export Statements (DIES): This category covers the below SQL operations that can
be performed on SAP HANA Database.
EXPORT
IMPORT
IMPORT FROM
IMPORT SCAN
Please go through article Data Import Export Statements in SAP HANA to know the syntax for
each of the above operations.
System Management Statements (SMMS):
System Management Statements (SMMS): This category covers the below SQL operations that
can be performed on SAP HANA Database.
ALTER SYSTEM ALTER CONFIGURATION
ALTER SYSTEM ALTER SESSION SET
ALTER SYSTEM ALTER SESSION UNSET
ALTER SYSTEM APPLICATION ENCRYPTION
ALTER SYSTEM ALTER TABLE PLACEMENT
ALTER SYSTEM CANCEL [WORK IN] SESSION
ALTER SYSTEM CLEAR AUDIT LOG
ALTER SYSTEM CLEAR SQL PLAN CACHE
ALTER SYSTEM CLEAR TRACES
ALTER SYSTEM DISABLE ALL ASYNCHRONOUS TABLE REPLICAS
ALTER SYSTEM DISCONNECT SESSION
ALTER SYSTEM ENABLE ALL ASYNCHRONOUS TABLE REPLICAS
ALTER SYSTEM LOAD PERFTRACE
ALTER SYSTEM LOGGING
ALTER SYSTEM PERSISTENCE ENCRYPTION
ALTER SYSTEM RECLAIM DATA SPACE
ALTER SYSTEM RECLAIM DATAVOLUME
ALTER SYSTEM RECLAIM LOG
ALTER SYSTEM RECLAIM VERSION SPACE
ALTER SYSTEM RECONFIGURE SERVICE
ALTER SYSTEM RECOMPILE SQL PLAN CACHE ENTRY
ALTER SYSTEM REMOVE TRACES
ALTER SYSTEM RESET MONITORING VIEW
ALTER SYSTEM SAVE PERFTRACE
ALTER SYSTEM SAVEPOINT
ALTER SYSTEM START PERFTRACE
ALTER SYSTEM STOP PERFTRACE
ALTER SYSTEM STOP SERVICE
CREATE CERTIFICATE
DROP CERTIFICATE
SET SYSTEM LICENSE
UNSET SYSTEM LICENSE ALL
ALTER PSE
CREATE PSE
DROP PSE
SET PSE
UNSET PSE
Please go through article System Management Statements in SAP HANA to know the syntax
for each of the above operations.

Workload Management Statements (WMS)


Workload Management Statements (WMS) category covers the below SQL operations that can
be performed on SAP HANA Database.
CREATE WORKLOAD MAPPING
ALTER WORKLOAD MAPPING
DROP WORKLOAD MAPPING
CREATE WORKLOAD CLASS
ALTER WORKLOAD CLASS
DROP WORKLOAD CLASS
Please go through article Workload Management Statements in SAP HANA to know the syntax
for each of the above operations.

Tenant Database Management Statements (TDMS)


Tenant Database Management Statements (TDMS) category covers the below SQL operations
that can be performed on SAP HANA Database.
CREATE DATABASE
DROP DATABASE
ALTER DATABASE
ALTER SYSTEM START DATABASE
ALTER SYSTEM STOP DATABASE
Please go through article Tenant Database Management Statements in SAP HANA to know the
syntax for each of the above operations.
SQL Functions in SAP HANA
In any Data warehousing system, one of the main objective is to transform the source data into
meaningful information which can be easily understand and analyze by business. In order to
achieve this we need to have knowledge on the different functions available at database level
and how to use those functions.

The SQL Functions in SAP HANA are categorized into 9 types, they are:

String Functions
Numeric Functions
Data Type Conversion functions
Date Time Functions
Window Functions
Miscellaneous Functions
Full Text Functions
Aggregate Functions
Series Data Functions

SQL String Functions in SAP HANA:


The different String functions available in SAP HANA and its syntaxs.
ASCII: This functions returns the ASCII value of first character in the input.
Syntax: ASCII(character_value) or ASCII(number)

Example: SELECT ASCII(Abcd) AS ASCII VALUE FROM DUMMY


Result: 65 (ASCII Value of A)
Example: SELECT ASCII(989) AS ASCII VALUE FROM DUMMY
Result: 57 (ASCII Value of 9)
BINTOSTR: This function converts VARBINARY string binary string into character string
using CESU-8 encoding. This will be useful when we want to do any data encoding in Data
Warehouse system.
Syntax: BINTOSTR(encoded_value)

Example: SELECT BINTOSTR (41626364) AS BINTOSTR FROM DUMMY;


Result: Abcd (Decoded value for 41626364)
Example: SELECT BINTOSTR (393839) AS BINTOSTR FROM DUMMY;
Result: 989 (Decoded value for 393839)
CHAR: This is used to get character value for the ASCII value.
Syntax: CHAR(ASCII_VALUE)
Example: SELECT CHAR(65) || CHAR(98) || CHAR(99) || CHAR(100) AS CHARACTER
FROM DUMMY
Result: Abcd (Concatenation of ASCII values for A,b,c,d)
Example: SELECT CHAR(57) || CHAR(56) || CHAR(57) AS NUMBER FROM DUMMY
Result: 989 (Concatenation of ASCII values for 9,8,9)

CONCAT: This is used to concatenate more than one string values.


Syntax: CONCAT(string1,string2)
Example: SELECT CONCAT(HELLO, WORLD) AS FULL STRING FROM DUMMY
Result: HELLO WORLD (combined string of HELLO and WORLD)
Note: CONCAT can only take two strings as input. If we have to combine more than two strings
then CONCAT has to be more than once like below.
Example: SELECT CONCAT(HELLO,CONCAT( WORLD, GOOD MORNING)) FROM DUMMY
Result: HELLO WORLD GOOD MORNING (concatenation of 3 strings)

INSTR: This functions returns the position of specified string in the input string.
Syntax: INSTR(string,search_string)
Example: SELECT INSTR(Abcd,c) FROM DUMMY
Result: 3 (c position in the input string Abcd)

LCASE: This convers the given input characters into lower case.
Syntax: LCASE(string)

Example: SELECT LCASE(AbCD) AS LOWER CASE FROM DUMMY


Result: abcd (Lower case characters for AbCD)

LEFT: This returns the number of characters/bytes specified from the left side in the input
string.
Syntax: LEFT(string,n)
Example: SELECT LEFT(Abcd,2) AS LEFT STRING FROM DUMMY
Result: Ab (Left two characters from Abcd)
LENGTH: This gives the length of the given string as number. The starting number is always
1.
Syntax: LENGTH(string)
Example: SELECT LENGTH(Hello World) AS STRING LENGTH FROM DUMMY
Result: 11 (length of string Hellow World)

LOCATE: This give the starting position of the specified substring in a given input string
Syntax: LOCATE(string,substring)
Example: SELECT LOCATE(Hello World,World) AS LOCATE FROM DUMMY
Result: 7 (Starting position of World)
Note: If the substring is not found in the main string then it gives the output as 0.
Example: SELECT LOCATE(Hello World,Dorld) AS LOCATE FROM DUMMY
Result: 0 (word Doraldis not there in Hello World)
LOWER: This converts all the characters in the given string to lower case. This is same as
LCASE function.
Syntax: LOWER(string)
Example: SELECT LOWER(AbCD) AS LOWER CASE FROM DUMMY
Result: abcd (lower case letters for string AbCD)

LPAD: This function is useful to do padding on left side for the given input string. If the
pattern is not specified, then it places spaces or it uses the specified pattern for padding.
Syntax: LPAD(string,length,pattern)
Example: SELECT LPAD(Abcd,10,123) AS LPAD FROM DUMMY
Result: 123123Abcd (123 is padded on the left side until the string length becomes 10)
Example: SELECT LPAD(Abcd,10) AS LPAD FROM DUMMY
Result: Abcd (system pads 6 spaces before Abcd to make total length 10)

LTRIM: Thus function removes all the leading spaces in the given input string. If specific
pattern is mentioned then it removes that pattern from the input string.
Note: The pattern is used as set of characters, not as search string.
Syntax: LTRIM(string,remove_pattern)
Example: SELECT LTRIM( Abcd) AS LTRIM FROM DUMMY
Result: Abcd (All the leading spaces are removed)
Example: SELECT LTRIM(Abcd,Ab) AS LTRIM FROM DUMMY
Result: cd (Ab is removed from the string)
NCHAR: This function returns the UNICODE character for the given input number.
Syntax: NCHAR(number)
Example: SELECT NCHAR(97) AS NCHAR FROM DUMMY
Result: a (character for the number 97)
REPLACE: This function can be used to replace specific string pattern with different one in
the input string.
Syntax: REPLACE(string,search_string,replace_string)
Example: SELECT REPLACE (Abcd,cd, CD) REPLACE FROM DUMMY;
Result: AbCD (cd has been replaced with CD)
Note: Some of the points to remember are
1. If the main string is NULL then the result is NULL
2. If the search string is not found in the main string, then same main string will be
displayed as result.

RIGHT: This function returns the number of characters in input string from right based on
the specified number.
Syntax: RIGHT (string,number)
Example: SELECT RIGHT (Abcd,2) AS RIGHT STRING FROM DUMMY;
Result: cd (two characters from right in Abcd)
RPAD: This function is useful to do padding on right side for the given input string. If the
pattern is not specified, then it places spaces or it uses the specified pattern for padding.
Syntax: RPAD(string,length,pattern)
Example: SELECT RPAD(Abcd,10,123) AS RPAD FROM DUMMY
Result: Abcd123123 (123 is padded on the right side until the string length becomes 10)
Example: SELECT RPAD(Abcd,10) AS RPAD FROM DUMMY
Resut: Abcd (system pads 6 spaces after Abcd to make total length 10)
RTRIM: Thus function removes all the trailing spaces in the given input string. If specific
pattern is mentioned then it removes that pattern from the input string.
Note: The pattern is used as set of characters, not as search string.
Syntax: RTRIM(string,remove_pattern)
Example: SELECT RTRIM(Abcd ) AS RTRIM FROM DUMMY
Result: Abcd (All the trailing spaces are removed)
Example: SELECT RTRIM(Abcd,cd) AS RTRIM FROM DUMMY
Result: Ab (cd is removed from the string)
STRTOBIN: This function convers all the characters in the input string into a binary
encoding with defined codepage.
Syntax: STRTOBIN(string,codepage)
Example: SELECT STRTOBIN (Abcd, UTF-16BE) STRTOBIN FROM DUMMY
Result: 0041006200630064 (encoded value for Abcd)
SUBSTR_AFTER: This function gives the substring from the input string after the first
occurrence of the specified pattern.
Note: Some of the notes to remember are
1. If the input string doesnt have the specified pattern, then the result is NULL
2. If the specified pattern is empty, then entire string will be returned.
3. If the input string or patter is NULL, then the result is NULL.
Syntax: SUBSTR_AFTER(string,pattern)
Example: SELECT SUBSTR_AFTER(Hello World, ) AS SUSTR AFTER FROM DUMMY
Result: World (substring after in Hello World is World)
SUBSTR_BEFORE: This function gives the substring from the input string before the first
occurrence of the specified pattern.
Note: Some of the notes to remember are
1. If the input string doesnt have the specified pattern, then the result is NULL
2. If the specified pattern is empty, then entire string will be returned.
3. If the input string or patter is NULL, then the result is NULL.
Syntax: SUBSTR_BEFORE(string,pattern)
Example: SELECT SUBSTR_BEFORE(Hello World, ) AS SUSTR BEFORE FROM DUMMY
Result: Hello (substring before in Hello World is Hello)
SUBSTRING: This function returns the substring from the input string based on the stating
position and number of characters specified.
Note: Some of the notes to remember are
1. If the start position is less than 0 then it is considered as 1.
2. If the input string length is less than 1 then empty sting will be returned.
3. If the length is not specified then entire string will be returned after the starting
position.
Syntax: SUBSTRING(string,start_position,length)
Example: SELECT SUBSTRING(Hello World,4,2) AS SUBSTRING FROM DUMMY
Result: lo (Starting from position 4 and 2 characters in Hello World)
Example: SELECT SUBSTRING(Hello World,4) AS SUBSTRING FROM DUMMY
Result: lo World (starting from position 4, entire string is returned)
TRIM: This function can be used to remove both LEADING, TRAILING spaces from input
string or specified string pattern.
Note: Some of the notes to remember are
1. If either input string, string pattern are NULL then NULL is returned.
2. If the option is not specified like LEADING or TRAILING then it moves both
TRAILING and LEADING.
3. If the string pattern is not specified, then system consider it as single space.
Syntax: TRIM(LEADING/TRAILING/BOTH string_pattern FROM string)
Example: SELECT TRIM(LEADING FROM Abcd) AS TRIM FROM DUMMY
Result: Abcd (Removes all the leading spaces from Abcd)
Example: SELECT TRIM(LEADING Ab FROM Abcd) AS TRIM FROM DUMMY
Result: cd (Removes the pattern Ab from left side in Abcd)

UCASE: This convers the given input characters into upper case.
Syntax: UCASE(string)
Example: SELECT UCASE(AbCD) AS UPPER CASE FROM DUMMY
Result: ABCD (Upper case characters for AbCD)

UNICODE: This function returns the integer containing UNICODE code for the first
character in the given string. If the first character is not a valid encoding then NULL is
returned.
Syntax: UNICODE(string)
Example: SELECT UNICODE ([) UNICODE FROM DUMMY;
Result: 91 (UNICODE code for the character [)

UPPER: This converts all the characters in the given string to upper case. This is same as
UCASE function.
Syntax: UPPER(string)
Example: SELECT UPPER(AbCD) AS UPPER CASE FROM DUMMY
Result: ABCD (Upper case letters for string AbCD)
With this we have seen different string functions available in SAP HANA and usage of each. In
the next article we will be discussing about the other SQL functions.
NUMERIC /NUMBER FUNCTIONS
ABS: This function is used to return the absolute value of the numeric argument n.
Syntax: ABS (n)
Example: SELECT ABS(-678) AS ABSOLUTE VALUE FROM DUMMY
Result: 678 (Absolute values for -678)
ACOS: This function is used to return the arc-cosine, in radians, of the numeric argument n
between -1 and 1.
Syntax: ACOS (n)
Example: SELECT ACOS (.3) acos FROM DUMMY;
Result: acos: 1.2661036727794992
ASIN: This function is used to return the arc-sine, in radians, of the numeric argument n
between -1 and 1.
Syntax: ASIN (n)
Example: SELECT ASIN (0.3) asin FROM DUMMY;
Result: asin: 0.3046926540153975
ATAN: This function is used to return the arc-tangent, in radians, of the numeric argument
n. The range of n is unlimited.
Syntax: ATAN (n)
Example: SELECT ATAN (0.3) atan FROM DUMMY;
Result: atan: 0.2914567944778671
ATAN2: This function is used to return the arc-tangent, in radians, of the ratio of two
numbers n and m. This produces the same result as ATAN(n/m).
Syntax: ATAN2 (n, m)
Example: SELECT ATAN2 (0.5, 1.0) atan2 FROM DUMMY;
Result: atan2: 0.4636476090008061
BINTOHEX: This function is used to convert a binary value to a hexadecimal value.
Syntax: BINTOHEX (expression)
Example: SELECT BINTOHEX(A) bintohex FROM DUMMY;
Result: bintohex: 41
BITAND: This function is used to perform an AND operation on the bits of the arguments n
and m. Both n and m must be non-negative integers or varbinary. The BITAND function
return a result along arguments type.
Syntax: BITAND (n, m)
Example: SELECT BITAND (20, 40) bitand FROM DUMMY;
Result: bitand: 0
BITCOUNT: This function is used to count the number of set bits of the argument
<expression>. <expression> must be an integer or a varbinary. The BITCOUNT function
return a integer type.
Syntax: BITCOUNT (<expression>)
Example: SELECT BITCOUNT (20) bitcount FROM DUMMY;
Result: bitcount: 2
BITNOT: This function is used to perform a bitwise NOT operation on the bits of the
argument <expression>. <expression>must be an integer. The BITNOT function returns a
result along arguments type.
Syntax: BITNOT (expression)
Example: SELECT BITNOT (20) bitnot FROM DUMMY;
Result: bitnot: -20
BITOR: This function performs an OR operation on the bits of the arguments <expression1>
and <expression2>. Both <expression1> and <expression2> must be non-negative integers
or varbinary. The BITOR function returns a result along arguments type.
Syntax: BITOR (<expression1>, <expression2>)
Example: SELECT BITOR (20, 40) bitor FROM DUMMY;
Result: bitor: 60
BITSET: This function is used to set <num_to_set> bits to 1 in <target_num> from the
<start_bit> position.
Syntax: BITSET (<target_num>, <start_bit>, <num_to_set>)
Example: SELECT BITSET (abcd, 1, 2) bitset FROM DUMMY;
Result: bitset: EBCD
BITUNSET: This function is used to set <num_to_unset> bits to 0 in <target_num> from the
<start_bit> position.
Syntax: BITUNSET (<target_num>, <start_bit>, <num_to_unset>)
Example: SELECT BITUNSET (abcd, 1, 2) bitunset FROM DUMMY;
Result: bitunset: 2BCD
BITXOR: This function is used to perform an XOR operation on the bits of the arguments
<expression1> and <expression2>. Both <expression1> and <expression2> must be non-
negative integers or varbinary. The BITXOR function returns a result along arguments type.
Syntax: BITXOR (expression1, expression2)
Example: SELECT BITXOR (20,40) bitxor FROM DUMMY;
Result: bitxor: 60
CEIL: This function is used to return the first integer that is greater or equal to the value n.
Syntax: CEIL (n)
Example: SELECT CEIL (2.1) ceiling FROM DUMMY;
Result: ceiling: 3
COS: This function is used to return the cosine of the angle, in radians, of the argument n.
Syntax: COS (n)
Example: SELECT COS (0.5) cos FROM DUMMY;
Result: cos: 0.8775825618903728
COSH: This function is used to compute the hyperbolic cosine of the argument n.
Syntax: COSH (n)
Example: SELECT COSH (0.5) cosh FROM DUMMY;
Result: cosh: 1.1276259652063807
COT: This function is used to compute the cotangent of a number n, where the argument is
an angle expressed in radians.
Syntax: COT (n)
Example: SELECT COT (0.5) cot FROM DUMMY;
Result: cot: 1.830487721712452
EXP: This function is used to return the result of the base of natural logarithms e raised to
the power of the argument n.
Syntax: EXP (n)
Example: SELECT EXP (2.0) exp FROM DUMMY;
Result: exp: 7.38905609893065
FLOOR: This function is used to return the largest integer not greater than the numeric
argument n.
Syntax: FLOOR (n)
Example: SELECT FLOOR (2.1) floor FROM DUMMY;
Result: floor: 2
HEXTOBIN: This function is used to convert a hexadecimal value to a binary value.
Syntax: HEXTOBIN (value)
Example: SELECT HEXTOBIN (B) HEXTOBIN FROM DUMMY;
Result: : 0B
LN: This function is used to return the natural logarithm of the argument n.
Syntax: LN (n)
Example: SELECT LN (2) LN FROM DUMMY;
Result: LN: 0.6931471805599453
LOG: This function is used to return the natural logarithm of a number n base b. Base b
must be a positive value greater than 1 and n must be any positive value.
Syntax: LOG (b, n)
Example: SELECT LOG (15, 3) log FROM DUMMY;
Result: log: 0.40568387108221293
MOD: This function is used to return the remainder of a number n divided by a divisor d.
Syntax: MOD (n, d)
Example: SELECT MOD (35, 8) Modulus FROM DUMMY;
Result: Modulus: 3
POWER: This function is used to calculate the base number b raised to the power of an
exponent e.
Syntax: POWER (b, e)
Example: SELECT POWER (5, 6) Power FROM DUMMY;
Result: Power: 15625
RAND: This function is used to return a pseudo-random value in the range of 0 to 1.0. Its
return value type is DOUBLE.
Syntax: DOUBLE RAND()
Example: SELECT RAND() FROM DUMMY;
Result: RAND: 0.0009853946746503084
ROUND: This function is used to round argument <n> to the specified <pos> amount of
places after the decimal point. The <rounding_mode> defines how the rounding should be
carried out.
Syntax: ROUND (<n> [, <pos>])
ROUND (<n>, <pos> [, <rounding_mode>])
Example: SELECT ROUND (5.23, -1) round FROM DUMMY;
SELECT ROUND( 5.23, 1, ROUND_HALF_DOWN) round FROM DUMMY;
Result: round: 10
round: 5.2
SIGN: This function is used to return the sign (positive or negative) of the numeric
argument n. Returns 1 if n is a positive value,-1 if n is a negative value, and 0 if n is equal to
zero.
Syntax: SIGN (n)
Example: SELECT SIGN (-5) sign FROM DUMMY;
Result: sign: -1
SIN: This function is used to return the sine of n, where the argument is an angle expressed
in radians.
Syntax: SIN (n)
Example: SELECT SIN (0.5) sine FROM DUMMY;
Result: sine: 0.479425538604203
SINH: This function is used to return the hyperbolic sine of n, where the argument is an
angle expressed in radians.
Syntax: SINH (n)
Example: SELECT SINH (0.5) sinh FROM DUMMY;
Result: sinh: 0.5210953054937474
SQRT: This function is used to return the square root of the argument n.
Syntax: SQRT (n)
Example: SELECT SQRT (117) sqrt FROM DUMMY;
Result: sqrt: 10.816653826391969
TAN: This function is used to return the tangent of n, where the argument is an angle
expressed in radians.
Syntax: TAN (n)
Example: SELECT TAN (0.5) tan FROM DUMMY;
Result: tan: 0.5463024898437905
TANH: This function is used to return the hyperbolic tangent of the numeric argument n.
Syntax: TANH (n)
Example: SELECT TANH (0.5) tanh FROM DUMMY;
Result: tanh: 0.46211715726000974
UMINUS: This function is used to return the negated value of the numeric argument n.
Syntax: UMINUS (n)
Example: SELECT UMINUS(444) uminus FROM DUMMY;
Result: -444
With this we have seen different numeric functions available in SAP HANA and usage of
each one.
SAP HANA Data Type Conversion Functions
These functions are used to convert function arguments from one data type to another, or
used to test whether a conversion is possible or not.
Below are the lists of SAP Data Type Conversion functions available in SAP HANA.
Note: Numeric data type conversions always truncate least significant digits towards zero.
CAST: This function converts the given value to the specified data type.
Syntax: CAST(<Value/Column/Expression> AS <Data Type>)
Example: SELECT CAST (1234 AS NVARCHAR) AS STRING FROM DUMMY
Result: This converts the number 1234 to string.
Example: SELECT CAST(123.45 AS INTEGER) AS INTEGER FROM DUMMY
Result: This converts the decimal value 123.45 to integer 123 (decimals are always
truncate towards zero).
TO_ALPHANUM: This function converts a given value to an ALPHANUM data type.
Syntax: TO_ALPHANUM(<Value>)
Example: SELECT TO_ALPHANUM (10) to alphanum FROM DUMMY;
Result: Given value 10 will be converted to alpha numeric.
TO_BIGINT: This function converts the given value to a BIGINT data type.
Note: If the entered value has decimal points then these digits are truncated during
conversion.
Syntax: TO_BIGINT(<Value>)
Example: SELECT TO_BIGINT(123) AS TO BIGINT FROM DUMMY
Result: This string 123 will be converted to 123 number.
Example: SELECT TO_BIGINT(123.75) AS TO BIGINT FROM DUMMY
Result: Decimal value 123.75 will be converted to number 123 and decimal values will
be truncated.
TO_BINARY: This function is used to convert the given/source value to a BINARY data type.
Syntax: TO_BINARY(<value>)
Example: SELECT TO_BINARY(ABC) AS TO BINARY FROM DUMMY
Result: The binary value for ABC is going to be 414243.
TO_BLOB: This function is used convert the given/source value to a BLOB data type.
Note: The source/given value must be a binary string.
Syntax: TO_BLOB(<value>)
Example: SELECT TO_BLOB (TO_BINARY(ABC)) TO BLOB FROM DUMMY;
Result: The result is going to ABC again because it will be converted to binary value
which gain will be converted to characters to TO_BLOB function.
TO_CLOB: This function is used convert the given/source value to a CLOB data type.
Syntax: TO_CLOB(<value>)
Example: SELECT TO_CLOB(SAPSTUDENT.COM is learning resource for SAP
HANA)AS TO CLOB FROM DUMMY
Result: The result is going to be same string with the data type CLOB.
TO_DATE: This function is used to convert the date values stored in string format into
HANA default date format YYYY-MM-DD. The input should the date string and format how
it is stored. If the string date format is in the same format of HANA default or
YYYY/MM/DD, then we can skip entering the date format in the input.
Syntax: TO_DATE (<Value>,<Format>)
Example: SELECT TO_DATE(1/12/2015, MM/DD/YYYY) to date FROM DUMMY;
Result: 2015-01-12 (the source string date with format MM/DD/YYYY has been
converted HANA data format YYYY-MM-DD
Example: SELECT TO_DATE(2015-01-12) to date FROM DUMMY;
Result: 2015-01-12. If the source string date value is in HANA default format, then we
can skip the format in the input)
TO_DATS: This function is used to convert the date string values to ABAP DATE string with
format YYYYMMDD.
Note: The source date string value format should HANA default date type i.e YYYY-MM-
DD. If the format is not like HANA default, then we need to use TO_DATE function before
be apply TO_DATS.
Syntax: TO_DATS(<date string>)
Example: SELECT TO_DATS(2015-01-01) AS ABAP DATE FROM DUMMY
Result: This gives use result as 20150101.
Example: SELECT TO_DATS(TO_DATE(01/01/2015,MM/DD/YYYY)) AS ABAP
DATE FROM DUMMY
Result: TO_DATE function first converts source date into HANA format and then
TO_DATS will used to convert it to ABAP format as shown in the above result.
TO_DECIMAL: This function is used to convert the given value to the specified precision
and scale.
Note: The precision value can range from 1 to 34 and scale can vary from -6111 to 6111.
DECIMAL(5,2) means the maximum value it can store is 999.99 but not 99999.99.
Syntax: TO_DECIMAL(<value>,<precision>,<scale>)
Example: SELECT TO_DECIMAL(1234.789,10,2) AS TO DECIMAL FROM DUMMY
Result: 1234.78 (the scale is truncated to 2)
Example: SELECT TO_DECIMAL(1234.789,10,0) AS TO DECIMAL FROM DUMMY
Result: 1234 (The scale is defined as 0, so everything is truncated after precision)
TO_DOUBLE: This function is used to convert the given string value to double data type.
Syntax: TO_DOUBLE(<vale>)
Example: SELECT TO_DOUBLE(1234.78) AS TO DOUBLE FROM DUMMY
Result: 1234.78
Example: SELECT TO_DOUBLE(1234.78) AS TO DOUBLE FROM DUMMY
Result: 1234.78
TO_FIXEDCHAR: This function is used extract the specified number of characters from
input string.
Syntax: TO_FIXEDCHAR(<string>,<no of characters>)
Example: SELECT TO_FIXEDCHAR(sap student,3) AS TO FIXED CHAR FROMDUMMY
Result: sap (first 3 characters from the input string)
TO_INT: This function is used to convert the input value or string to INTEGER data type.
Note: If the input value has precision, then this will be truncated during conversion
process.
Syntax: TO_INT(<value>)
Example: SELECT TO_INT(10) AS TO INTEGER FROM DUMMY
Result: 10 (String 10 has converted to number)
Example: SELECT TO_INT(10.50) AS TO INTEGER FROM DUMMY
Result: 10 (The value .50 has been truncated from the input value during conversion
process).
TO_INTEGER: This function is used to convert the input value or string to INTEGER data
type.
Note: If the input value has precision, then this will be truncated during conversion
process. This function is same as TO_INT above.
Syntax: TO_INT(<value>)
Example: SELECT TO_INT(10) AS TO INTEGER FROM DUMMY
Result: 10 (String 10 has converted to number)
Example: SELECT TO_INT(10.50) AS TO INTEGER FROM DUMMY
Result: 10 (The value .50 has been truncated from the input value during conversion
process).
TO_NCLOB: This function is used convert the given/source value to a NCLOB data type.
Syntax: TO_NCLOB(<value>)
Example: SELECT TO_NCLOB(SAPSTUDENT.COM is learning resource for SAP
HANA) AS TO NCLOB FROM DUMMY
Result: The result is going to be same string with the data type NCLOB.
TO_NVARCHAR: This function is used to convert the input value with the specified format
to NVARCHAR data type.
Syntax: TO_NVARCHAR(<value>,<format>)
Example: SELECT TO_NVARCHAR(1234) AS TO NVARCHAR FROM DUMMY
Result: 1234 (The number is converted to NVARCHAR)
Example: SELECT TO_NVARCHAR(TO_DATE(2009/12/31), MM-DD-YY) TO
NVARCHAR FROM DUMMY;
Result: 12-31-09 (TO_DATE converts the given input to YYYY-MM_DD format and then
TO_NVARCHAR extract the output in MM-DD-YY format)
TO_REAL: This function is used to convert the input value or string to REAL (single precision
value) data type.
Syntax: TO_REAL(<value>)
Example: SELECT TO_REAL(12.04) AS TO REAL FROM DUMMY
Result: 12.039999961853027
Example: SELECT TO_REAL(12.04) AS TO REAL FROM DUMMY
Result: 12.039999961853027
TO_SECONDDATE: This function is used to convert the given input string date value with
specified format into SECONDDATE data type.
Note: If the input date string format is either YYYY-MM-DD or YYYY/MM/DD then no need
to give format. If it is other than the above two formats, we need to specify the date string
format for source.
Syntax: TO_SECONDDATE(<date string>,<format>)
Example: SELECT TO_SECONDDATE (2010-01-11 13:30:00, YYYY-MM-DD
HH24:MI:SS) TO SECONDDATE FROM DUMMY;
Result: 2015-07-01 13:30:00.0
Example: SELECT TO_SECONDDATE (07/01/2015 13/30/00, MM/DD/YYYY
HH24/MI/SS) TO SECONDDATE FROM DUMMY;
Result: 2015-07-01 13:30:00.0
TO_SMALLDECIMAL: This function is used to convert the given value/string to
SMALLDECIMAL data type.
Note: The difference between DECIMAL and SMALLDECIMAL is in terms of precision and
scale. The precision range is 1~16 and scale is -369~369 for SMALLDECIMAL
Syntax: TO_SMALLDECIMAL(<value>)
Example: SELECT TO_SMALLDECIMAL(1234.89) TO
SMALLDECIMAL FROMDUMMY;
Result: 1234.89
Example: SELECT TO_SMALLDECIMAL(1234.89) TO SMALLDECIMAL FROMDUMMY;
Result: 1234.89
TO_SMALLINT: This function is used to convert the input value or string to SMALLINT data
type.
Note: If the input value has precision, then this will be truncated during conversion
process. The different between SMALLINT and INTEGER lies in the range of values it can
store. SMALLINT can store between -32,768 and 32,768.
Syntax: TO_SMALLINT(<value>)
Example: SELECT TO_SMALLINT(10) AS TO SMALLINT FROM DUMMY
Result: 10 (String 10 has converted to number)
Example: SELECT TO_SMALLINT(10.50) AS TO SMALLINT FROM DUMMY
Result: 10 (The value .50 has been truncated from the input value during conversion
process).
TO_TIME: This function is used to convert the input time string to TIME data type with the
specified source format to HANA format. SAP HANA format for time is HH:MM:SS
Syntax: TO_TIME(<value>,<format>)
Example: SELECT TO_TIME (14/20/10, HH/MI/SS) TO TIME FROM DUMMY;
Result: 14:20:10
Example: SELECT TO_TIME (08:30 AM, HH:MI AM) TO TIME FROM DUMMY;
Result: 08:30:00
TO_TIMESTAMP: This function is used to convert the input string timestamp value with
specified format to HANA TIMESTAMP data type.
Note: The difference between SECONDDATE and TIMESTAMP is in in terms of how much
information it can store. SECONDDATE can store up to seconds where TIMESTAMP can
store up to milli Seconds.
Syntax: TO_TIMESTAMP(<value>,<format).
Example: SELECT TO_TIMESTAMP (2010-01-11 13:30:00.123, YYYY-MM-DD
HH24:MI:SS.FF7) TO TIMESTAMP FROM DUMMY;
Result: 2010-01-11 13:30:00.123
TO_TINYINT: This function is used to convert the input value or string to TINYINT data type.
Note: If the input value has precision, then this will be truncated during conversion
process. The different between TINYINT and INTEGER lies in the range of values it can
store. TINYINT can store between 0 and 255.
Syntax: TO_TINYINT(<value>)
Example: SELECT TO_TINYINT(10) AS TO TINYINT FROM DUMMY
Result: 10 (String 10 has converted to number)
Example: SELECT TO_TINYINT(10.50) AS TO TINYINT FROM DUMMY
Result: 10 (The value .50 has been truncated from the input value during conversion
process).
TO_VARCHAR: This function is used to convert the input value with the specified format to
NVARCHAR data type.
Syntax: TO_NVARCHAR(<value>,<format>)
Example: SELECT TO_NVARCHAR(1234) AS TO NVARCHAR FROM DUMMY
Result: 1234 (The number is converted to NVARCHAR)
Example: SELECT TO_NVARCHAR(TO_DATE(2009/12/31), MM-DD-YY) TO
NVARCHAR FROM DUMMY;
Result: 12-31-09 (TO_DATE converts the given input to YYYY-MM_DD format and then
TO_NVARCHAR extract the output in MM-DD-YY format)
SQL DateTime Functions in SAP HANA
Analyzing the organization data by different date elements is most common requirement
across the modules in any industry. Extracting different elements from date data available
from source place a major role in data warehousing platform.
ADD_DAYS: This function is used to add N number of days to the existing date value.
Syntax: ADD_DAYS (date_value,n)
Example: SELECT ADD_DAYS(2015-07-01,30) AS ADD DAYS FROM DUMMY;
Res: 2015-07-31 (Added 30 days to existing value)
SELECT ADD_DAYS(2015-07-01,-30) AS ADD DAYS FROM DUMMY;
Res: 2015-06-01 (subtracted 30 days from existing value.
ADD_MONTHS: This function is used to add N number of months to the existing date
value.
Syntax: ADD_MONTHS (date_value,n)
Example: SELECT ADD_MONTHS(2015-07-01,1) AS ADD MONTHS FROM DUMMY;
Res: 2015-08-01 (Added 1 month to the existing value)
SELECT ADD_MONTHS(2015-07-01,-1) AS ADD MONTHS FROM DUMMY;
Res: 2015-06-01 (subtracted 1 month from existing value)
ADD_SECONDS: This function is used to add N number of seconds to the existing time.
Syntax: ADD_SECONDS(time_value,n)
Example: SELECT ADD_SECONDS(2015-07-01 10:00:00,60*30) AS ADD
SECONDSFROM DUMMY;
Res: 2015-07-01 10:30:00.0 (30 minutes added to the actual time)
SELECT ADD_SECONDS(2015-07-01 10:00:00,-60*30) AS ADD SECONDS FROMDUMMY;
Res: 2015-07-01 09:30:00.0 (30 minutes added to the actual time)
ADD_YEARS: This function is used to add N number of years to the existing date value.
Syntax: ADD_YEARS(date_value,n)
Example: SELECT ADD_YEARS(2015-07-01,1) AS ADD YEARS FROM DUMMY;
Res: 2016-07-01 (Added 1 year to the existing value)
SELECT ADD_YEARS(2015-07-01,-1) AS ADD YEARS FROM DUMMY;
Res: 2014-07-01 (subtracted 1 year from existing value)
ADD_WORKDAYS: This function is used to compute a date by adding a number of
workdays to a starting date.
Syntax: ADD_WORKDAYS (<factory_calendar_id>, <start_date>, <workdays> [,
<source_schema>])
Note: Pre-requisite to use ADD_WORKDAYS and WORKDAYS_BETWEEN functions is the
table TFACS (Factory calendar table should exist in HANA). If the system is SAP BW, SAP
CRM and SAP ECC on HANA then the table will will available in ABAP schema which is
SAP<SID>. If it is SAP HANA Plarform (stand alone) then table can be replicated using SLT
or Data Services.
CURRENT_DATE: This function is used to return the current local system date.
Syntax: CURRENT_DATE
Example: SELECT CURRENT_DATE AS CURRENT DATE FROM DUMMY
Res: 2015-07-01 (Current system date. It is local date)
CURRENT_TIME: This function is used to return the current local system time.
Syntax: CURRENT_TIME
Example: SELECT CURRENT_TIME AS CURRENT TIME FROM DUMMY
Res: 16:48:03 (Current system time. It is local time)
CURRENT_TIMESTAMP: This function is used to return the current local system timestamp
information.
Syntax: CURRENT_TIMESTAMP
Example: SELECT CURRENT_TIMESTAMP AS CURRENT DATE&TIME FROM DUMMY.
Res: 2015-07-01 16:48:03 (Local Current Date and Time)
CURRENT_UTCDATE: This function is used to return the current UTC date. (UTC
Coordinated Universal Time / Greenwich Mean Time (GMT)).
Syntax: CURRENT_UTCDATE
Example: SELECT CURRENT_UTCDATE AS CURRENT UTC DATE FROM DUMMY
Res: 2015-07-02 (Current UTC Date)
CURRENT_UTCTIME: This function is used to return the current UTC time.
Syntax: CURRENT_UTCTIME
Example: SELECT CURRENT_UTCTIME AS CURRENT UTC TIME FROM DUMMY
Res: 00:01:21 (Current UTC time)
CURRENT_UTCTIMESTAMP: This function is used to return the current local system
timestamp information.
Syntax: CURRENT_UTCTIMESTAMP
Example: SELECT CURRENT_UTCTIMESTAMP AS CURRENT UTC
TIMESTAMP FROMDUMMY
Res: 2015-07-02 00:01:21 (Current UTC Date and Time)
DAYNAME: This function is used to return the weekday in English for date d.
Syntax: DAYNAME (<d>)
Example: SELECT DAYNAME(2015-07-01) AS DAY NAME FROM DUMMY
Res: WEDNESDAY (Day Name in English)
DAYOFMONTH: This function is used to return an integer for the day of the month for date
d.
Syntax: DAYOFMONTH (<d>)
Example: SELECT DAYOFMONTH(2015-07-01) AS DAY OF MONTH FROM DUMMY
Res: 1 (Day number in the month of given date)
DAYOFYEAR: This function is used to return an integer representation of the day of the
year for date d.
Syntax: DAYOFYEAR (d)
Example: SELECT DAYOFYEAR(2015-07-01) AS DAY OF YEAR FROM DUMMY
Res: 182 (Day number in the year of given date)

DAYS_BETWEEN: This function is used to compute the number of days between d1 and d2.
Syntax: DAYS_BETWEEN (d1, d2)
Example: SELECT DAYS_BETWEEN(2015-07-01,2015-07-15) AS DAYS
BETWEENFROM DUMMY
Res: 14 (Difference between July 15th and July 1st)

EXTRACT: This function is used to find and return the value of a specified datetime field
from date d.
Syntax: EXTRACT ({YEAR | MONTH | DAY | HOUR | MINUTE | SECOND} FROM d)
Example: SELECT EXTRACT(YEAR FROM 2015-07-01) AS YEAR FROM DUMMY
Res: 2015 (Year was extracted from the given date)

HOUR: This function is used to return an integer representation of the hour for time t.
Syntax: HOUR (t)
Example: SELECT HOUR(2015-07-01 16:45:12) AS HOUR FROM DUMMY
Res: 16 (Hour has been extracted from the given time).

ISOWEEK: This function is used to return the ISO year and week numbers of date d. The
week number is prefixed by the letter W.
Syntax: ISOWEEK (d)
Example: SELECT ISOWEEK(2015-07-01) AS ISO WEEK FROM DUMMY;
Res: 2015-W27 (ISO week for along with the year for the given date)
LAST_DAY: This function is used to return the date of the last day of the month that
contains the date d.
Syntax: LAST_DAY (d)
Example: SELECT LAST_DAY(2015-07-01) AS LAST DAY FROM DUMMY;
Res: 2015-07-01 (last day of the month in the given date)
LOCALTOUTC: This function is used to convert the local time t from a timezone to the
UTC(GMT) time
Syntax: LOCALTOUTC (t, timezone)
Example: SELECT LOCALTOUTC(2015-07-01
18:42:21,PST) AS LOCALTOUTC FROMDUMMY;
Res: 2015-07-02 01:42:21 (GMT/UTC for the given PST time)
MINUTE: This function is used to return an integer representation of the minute for time t.
Syntax: MINUTE (t)
Example: SELECT MINUTE(2015-07-01 18:42:21) AS MINUTE FROM DUMMY;
Res: 42 (Minute extracted from the given time)
MONTH: This function is used to return the number of the month from date d.
Syntax: MONTH(d)
Example: SELECT MONTH(2015-07-01) AS MONTH FROM DUMMY;
Res: 7 (Month from the given date)
MONTHNAME: This function is used to return the name of the month in English for date d.
Syntax: MONTHNAME(d)
Example: SELECT MONTHNAME(2015-07-01) AS MONTH NAME FROM DUMMY;
Res: JULY (Month name from the given date)
NANO100_BETWEEN: This function is used to compute the time difference between date
arguments d1 and d2, to the precision of 0.1 microseconds.
Syntax: NANO100_BETWEEN (d1, d2)
Example: SELECT NANO100_BETWEEN (2015-07-01 18:40:11.000,2015-07-01
18:40:11.002) AS NANO BETWEEN FROM DUMMY;
Res: 20000 (difference between given times in nano seconds)
NEXT_DAY: This function is used to return the date of the next day after date d.
Syntax: NEXT_DAY (d)
Example: SELECT NEXT_DAY(2015-07-01) AS NEXT DAY FROM DUMMY;
Res: 2015-07-02 (Next day for the given date)
NOW: This function is used to returns the current timestamp.
Syntax: NOW ()
Example: SELECT NOW() AS CURRENT TIME FROM DUMMY;
Res: 2015-07-01 18:42:18.123 (Current time stamp (local system time))
QUARTER: This function is used to return the numerical year quarter of date d. The first
quarter starts in the month specified by start_month. If start_month is not specified the
first quarter is assumed to begin in January.
Syntax: QUARTER (d, [, start_month ])
Example: SELECT QUARTER(2015-07-01) AS QUARTER FROM DUMMY;
Res: 2015-Q3 (Quarter from the given date by assuming starting month as January)
SELECT QUARTER(2015-07-01,7) AS QUARTER FROM DUMMY;
Res: 2015-Q1 (Quarter from the given date by considering starting month for the first
quarter as 7)
SECOND: This function is used to return a value of the seconds for a given time.
Syntax: SECOND (t)
Example: SELECT SECOND(18:48:12) AS SECOND FROM DUMMY;
Res: 12 (Second value from the given time)
SECONDS_BETWEEN: This function is used to compute the number of seconds between
date arguments d1 and d2, which is semantically equal to d2 d1.
Syntax: SECONDS_BETWEEN (d1, d2)
Example: SELECT SECONDS_BETWEEN(2015-07-01 12:00:00,2015-07-01
18:00:00) ASSECOND BETWEEN FROM DUMMY;
Res: 21600 (seconds difference for the given two dates)
UTCTOLOCAL: This function is used to convert the UTC(GMT) time t to the local time in a
time zone.
Syntax: UTCTOLOCAL (t, timezone)
Example: SELECT UTCTOLOCAL(2015-07-02 10:00:00,PST) AS UTC/GMT TO
LOCALFROM DUMMY;
Res: 2015-07-02 03:00:00 (PST time for the UTC/GMT time)
WEEK: This function is used to return the week number of date d.
Syntax: WEEK (d)
Example: SELECT WEEK(2015-07-01) AS WEEK FROM DUMMY;
Res: 27 (week number for the given date)
WEEKDAY: This function is used to return an integer representation of the day of the week
for date d. The return value ranges from 0 to 6, representing Monday(0) through to
Sunday(6).
Syntax: WEEKDAY (d)
Example: SELECT WEEKDAY(2015-07-01) AS WEEKDAY FROM DUMMY;
Res: 2 (2 represents WEDNESDAY of the week from the given date)
WORKDAYS_BETWEEN: This function is used to compute the number of workdays
between a <start_date> and an <end_date>.Computations are done with respect to a
factory calendar with ID <factory_calendar_id>.
Syntax: WORKDAYS_BETWEEN (<factory_calendar_id>, <start_date>, <end_date> [,
<source_schema>])
Note: Pre-requisite to use ADD_WORKDAYS and WORKDAYS_BETWEEN functions is the
table TFACS (Factory calendar table should exist in HANA). If the system is SAP BW, SAP
CRM and SAP ECC on HANA then the table will will available in ABAP schema which is
SAP<SID>. If it is SAP HANA Plarform (stand alone) then table can be replicated using SLT
or Data Services.
YEAR: This function is used to return the year number of date d.
Syntax: YEAR(d)
Example: SELECT YEAR(2015-07-01) AS YEAR FROM DUMMY;
Res: 2015 (Year from the given date)
SQL Miscellaneous functions in SAP HANA
COALESCE: This function is used to return the first non-NULL expression from a list. At least
two expressions must be contained in expression_list, and all expressions must be
comparable. The result will be NULL if all the arguments are NULL.
Syntax: COALESCE (expression_list)
Example: SELECT COALESCE(A,B,C) AS COAL_ESCE FROM DUMMY
Ans: A (first not null value).
SELECT COALESCE(NULL,B,C)AS COAL_ESCE FROM DUMMY
Ans: B (First not null value)
CONVERT_CURRENCY: The CONVERT_CURRENCY function provides an efficient method to
calculate values in different currencies.(In order to use the CONVERT_CURRENCY function
currency conversion tables TCURV, TCURX, TCURN, TCURR and TCURF must be available in
the SAP HANA database.)
Syntax: CONVERT_CURRENCY ( <named_parameter_value>[{,
<named_parameter_value>}])
CONVERT_UNIT: CONVERT_UNIT function is a SQL representation of SQLScript Built-In
function CE_CONVERSION, and internally uses CE_CONVERSION for computation.(In order
to use the CONVERT_UNIT function, the unit conversion tables T006 and T006D must be
available in the SAP HANA database.) Syntax: CONVERT_UNIT( <named_parameter_value>,
)
CURRENT_CONNECTION: This function is used to return the id of the current connection.
Syntax: CURRENT_CONNECTION
Example: SELECT CURRENT_CONNECTION AS CURRENT CONNECTION FROM DUMMY
Ans: 213947 (ID of the current connetion)
CURRENT_IDENTITY_VALUE: This function is used to return a BIGINT value representing
the latest inserted identity value in the current session. If no identity value was inserted in
the current session, NULL will be returned as a result.
Syntax: CURRENT_IDENTITY_VALUE()
Example: SELECT CURRENT_IDENTITY_VALUE() FROM DUMMY
Ans: 234 (This is the current identity value for that session)
CURRENT_SCHEMA: This function is used to return a string containing the current schema
name.
Syntax: CURRENT_SCHEMA
Example: SELECT CURRENT_SCHEMA AS CURRENT SCHEMA FROM DUMMY
Ans: SAP_STUDENT (this is the current schema set for the session)
Note: The schema can be set manually using SET <schema_name>.
CURRENT_TRANSACTION_ISOLATION_LEVEL: This function is used to return a string
containing the current transaction isolation level.
Syntax: CURRENT_TRANSACTION_ISOLATION_LEVEL
Example: SELECT CURRENT_TRANSACTION_ISOLATION_LEVEL FROMM DUMMY
Ans: READ COMMITTED
CURRENT_UPDATE_STATEMENT_SEQUENCE: This function is used to return the current
write statement sequence number of the current transaction. This function is available
from HANA SPS10.
Syntax: CURRENT_UPDATE_STATEMENT_SEQUENCE ()
Example: SELECT CURRENT_UPDATE_STATEMENT_SEQUENCE () FROM DUMMY
Ans: 3 (update statement sequence in the sql session)
CURRENT_UPDATE_TRANSACTION: This function is used to return the id of the current
update transaction. This function is available from HANA SPS10.
Syntax: CURRENT_UPDATE_TRANSACTION ( )
Example: SELECT CURRENT_UPDATE_TRANSACTION() AS CURRENT UPDATE
TRANSACTION FROM DUMMY
Ans: 2 ( update transaction in that section).
CURRENT_USER: This function is used to return the current user name at the current
statement context. This will be user name which is currently at the top of authorization
stack.
Syntax: CURRENT_USER
Example: SELECT CURRENT_USER AS CURRENT USER FROM DUMMY
Ans: SAP_STUDENT (Current login user in the system)
GREATEST: This function is used to return the greatest value among the arguments: n1, n2,
Syntax: GREATEST (<argument> [{, <argument>}])
Example: SELECT GREATEST(12,45,75,32) AS HIGHEST VALUE FROM DUMMY
Ans: 75 (Highest value from the given input values)
GROUPING: GROUPING function can be used with GROUPING SETS/ROLLUP/CUBE that
return multiple levels of aggregations in a single result set.
GROUPING (column) returns 1 if column is used in grouping and 0 otherwise. The column
of GROUPING must be an element of the GROUPING SETS.
Syntax: GROUPING (column_name)
Example:
GROUPING_ID: GROUPING_ID function can be used with GROUPING SETS/ROLLUP/CUBE
that return multiple levels of aggregations in a single result set.
Syntax: GROUPING_ID(column_name_list)
Example:
HASH_SHA256: This function is used to return a 32 byte VARBINARY hash value of the
concatenated arguments. The hash is calculated using a SHA256 algorithm.
Syntax: HASH_SHA256 (<argument> [{, <argument>}])
Example:
IFNULL: This function is used to return the first not NULL input expression.
Returns expression1 if expression1 is not NULL.
Returns expression2 if expression1 is NULL.
Returns NULL if both input expressions are NULL.
Syntax: IFNULL (expression1, expression2)
Example: SELECT IFNULL(2,3) AS IF NULL FROM DUMMY
Ans: 2 (result is first value because it is not null)
SELECT IFNULL(NULL,3) AS IF NULL FROM DUMMY
Ans: 3 (result is second value because first value is null)
LEAST: This function is used to return the least value among the arguments: <n1>, <n2>
Syntax: LEAST (<n1> [, <n2>])
Example: SELECT LEAST(57,32,47) AS LEAST VALUE FROM DUMMY
Ans: 32 (Least value among the input values)
MAP: This function is used to search for an expression within a set of search values and
returns the corresponding result.
If the expression value is not found and default_result is defined, MAP returns
default_result.
If the expression value is not found and default_result is not defined, MAP returns NULL.
Syntax: MAP (<expression>, <search>, <result> [{, <search>, <result>}] [, default_result])
Example: SELECT MAP(A,A,B,C) AS MAP FROM DUMMY
Ans: B (expression A is same as A so the results is B)
SELECT MAP(A,D,B,C) AS MAP FROM DUMMY
Ans: C (expression A is not equal to D so the result is C)
Note: we can also use CASE as an alternative function to MAP. This function is perfect
replacement for DECODE function in ORACLE Database.
NULLIF: NULLIF compares the values of two input expressions. If the first expression equals
the second expression, NULLIF returns NULL.
If expression1 does not equal expression2, NULLIF returns expression1.
If expression2 is NULL, NULLIF returns expression1.
Syntax: NULLIF (expression1, expression2)
Example: SELECT NULLIF(A,A) AS NULL IF FROM DUMMY
Ans: null/blank (expression 1 is equal to expression 2)
SELECT NULLIF(A,B) AS NULL IF FROM DUMMY
Ans: A (expression 1 is not equal to expression 2)
SESSION_CONTEXT: This function is used to return the value of session_variable assigned
to the current user.
Syntax: SESSION_CONTEXT(session_variable)
Example: SELECT SESSION_CONTEXT(APPLICATIONUSER) FROM DUMMY
Ans: SAP_STUDENT (Application user set for the current session)
Note: variables can be set manually using SET SESSION <Variable Name> =
<Value>.
SESSION_USER: This function is used to return the user name of the current session.
Syntax: SESSION_USER
Example: SELECT SESION_USER AS SESSION USER FROM DUMMY
Ans: SAP_STUDENT (user for current session)
Note: This function is available only from HANA SP10.
SYSUUID: This function is used to return a new universally unique identifier, generated by
the connected SAP HANA instance. Each call of SYSUUID returns a new UUID value.
SYSUUID calls from multiple connections are internally serialized to guarantee unique value
generation.
Syntax: SYSUUID
Example: SELECT SYSUUID AS SYS UNIQUE NUMBER FROM DUMMY
Ans: 55AEB003F1DE0B6EE10000007F000101 (unique number generated by the system).

SQL Aggregate Functions in SAP HANA


AUTO_CORR: This function is used to compute all autocorrelation coefficients for a given
input column and returns an array of values.
The time frame size is limited by the maxTimeLag parameter. This parameter must be a
positive integer. The result size is the minimum of maxTimeLag and column size 2 for
dense series data.
Syntax: AUTO_CORR(<column>, <maxTimeLag> {SERIES() | ORDER BY <col1>, })
CORR: This function is used to compute the Pearson product momentum correlation
coefficient between two columns.
The result ranges from -1 to 1, depending on the correlation, or null if a correlation could
not be computed.
Syntax: CORR (<column1>, <column2>) [OVER([PARTITION BY <col1>, ] [ORDER BY <col1>,
[<window_frame>]])]
CORR_SPEARMAN: This function is used to return the Spearmans rank correlation
coefficient of the values found in the corresponding rows of <column1> and <column2>.
Column1 and column2 may contain number or even character types.Syntax:
CORR_SPEARMAN (<column1>, <column2>) [OVER([PARTITION BY <col1>, ][ORDER BY
<col1>, [<window_frame>]])]
CROSS_CORR: This function is used to compute all cross-correlation coefficients between
two given columns.
The result is an array of cross-correlation coefficients of length
<maxLag>.Syntax:CROSS_CORR (<expression1>, <expression2>, <maxLag> {
<series_orderby> | ORDER BY <expression3> [ ASC | DESC ] [ NULLS FIRST | NULLS ] } ). {
POSITIVE_LAGS | NEGATIVE_LAGS | ZERO_LAG }
DFT: This function is used to compute the Discrete Fourier Transform of a column for the
first N values and returns an array with exactly N elements.
The returned values depend on the output parameter, which must be one of REAL,
IMAGINARY, AMPLITUDE, or PHASE.
Syntax: DFT (<column>, <N>{SERIES( ) | ORDER BY <col1>,
}).{REAL|IMAGINARY|AMPLITUDE|PHASE}
FIRST_VALUE: This function is used to return the value of the first element in <expression>
as ordered by <column>.
Null is returned if the value is null or if <expression> is empty.
Syntax: FIRST_VALUE (<expression> ORDER BY <column>)
LAST_VALUE: This function is used to return the value of the last element in <column1> as
ordered by <column2>.
Null is returned if the value is null or if <column1> is empty.
Syntax: LAST_VALUE (<column1> ORDER BY <column2>)
MEDIAN: This function is used to find the statistical median of an input column with a
numeric data type.
Null values are eliminated. If there is an even number of elements, the average of the two
middle elements is returned. Otherwise, the middle element is returned.
Syntax: MEDIAN (<column>)[OVER([PARTITION BY <col1>, ] [ORDER BY <col1>,
[<window_frame>]])]
NTH_VALUE: This function is used to return the value of the element at position <n> in
<column1> as ordered by <column2>. Null is returned if the value is null or if <n> is larger
than the number of elements in <column1>. An error is raised if <n> is less than or equal to
0.
Syntax: NTH_VALUE (<column1>, <n> ORDER BY <column2>)
STDDEV_POP: This function is used to return the standard deviation of the given
expression as the square root of VAR_POP function.
Syntax: STDDEV_POP(<expression>)
STDDEV_SAMP: This function is used to return the standard deviation of the given
expression as the square root of VAR_SAMP function.
Syntax: STDDEV_SAMP(<expression>)
VAR_POP: This function is used to return the population variance of the expression as the
sum of squares of the difference of <expression> from the mean of <expression>, divided
by the number of rows remaining.
Syntax: VAR_POP(<expression>)
VAR_SAMP: This function is used to return the sample variance of the expression as the
sum of squares of the difference of <expression> from the mean of <expression>, divided
by the number of rows remaining minus 1 (one). This functions is similar to VAR, the only
difference is that it returns NULL when the number of rows is 1.
Syntax: VAR_SAMP(<expression>)

S-ar putea să vă placă și