STRATEGY & CREATIVITY

SAP HANA In Memory Database.

PORTFOLIO

We pride ourselves on bringing a good technology and effective knowledge to every perfect one.

  • SAP HANA - SQL Expressions

    An Expression is used to evaluate a clause to return values. There are different SQL expressions that can be used in HANA −
    • Case Expressions
    • Function Expressions
    • Aggregate Expressions
    • Subqueries in Expressions

    Case Expression

    This is used to pass multiple conditions in a SQL expression. It allows the use of IF-ELSE-THEN logic without using procedures in SQL statements.

    Example

    SELECT COUNT( CASE WHEN sal < 2000 THEN 1 ELSE NULL END ) count1,
    COUNT( CASE WHEN sal BETWEEN 2001 AND 4000 THEN 1 ELSE NULL END ) count2,
    COUNT( CASE WHEN sal > 4000 THEN 1 ELSE NULL END ) count3 FROM emp;
    This statement will return count1, count2, count3 with integer value as per passed condition.

    Function Expressions

    Function expressions involve SQL inbuilt functions to be used in Expressions.

    Aggregate Expressions

    Aggregate functions are used to perform complex calculations like Sum, Percentage, Min, Max, Count, Mode, Median, etc. Aggregate Expression uses Aggregate functions to calculate single value from multiple values.
    Aggregate Functions − Sum, Count, Minimum, Maximum. These are applied on measure values (facts) and It is always associated with a dimension.
    Common aggregate functions include −
    • Average ()
    • Count ()
    • Maximum ()
    • Median ()
    • Minimum ()
    • Mode ()
    • Sum ()

    Subqueries in Expressions

    A subquery as an expression is a Select statement. When it is used in an expression, it returns a zero or a single value.
    A subquery is used to return data that will be used in the main query as a condition to further restrict the data to be retrieved.
    Subqueries can be used with the SELECT, INSERT, UPDATE, and DELETE statements along with the operators like =, <, >, >=, <=, IN, BETWEEN etc.
    There are a few rules that subqueries must follow −
    • Subqueries must be enclosed within parentheses.
    • A subquery can have only one column in the SELECT clause, unless multiple columns are in the main query for the subquery to compare its selected columns.
    • An ORDER BY cannot be used in a subquery, although the main query can use an ORDER BY. The GROUP BY can be used to perform the same function as the ORDER BY in a subquery.
    • Subqueries that return more than one row can only be used with multiple value operators, such as the IN operator.
    • The SELECT list cannot include any references to values that evaluate to a BLOB, ARRAY, CLOB, or NCLOB.
    • A subquery cannot be immediately enclosed in a set function.
    • The BETWEEN operator cannot be used with a subquery; however, the BETWEEN operator can be used within the subquery.

    Subqueries with the SELECT Statement

    Subqueries are most frequently used with the SELECT statement. The basic syntax is as follows −

    Example

    SELECT * FROM CUSTOMERS
    WHERE ID IN (SELECT ID
    FROM CUSTOMERS
    WHERE SALARY > 4500) ;
    +----+----------+-----+---------+----------+
    | ID | NAME     | AGE | ADDRESS | SALARY   |
    +----+----------+-----+---------+----------+
    | 4  | Chaitali | 25  | Mumbai  | 6500.00  |
    | 5  | Hardik   | 27  | Bhopal  | 8500.00  |
    | 7  | Muffy    | 24  | Indore  | 10000.00 |
    +----+----------+-----+---------+----------+
  • SAP HANA - SQL Functions

    There are various SQL functions provided by SAP HANA database −
    • Numeric Functions
    • String Functions
    • Fulltext Functions
    • Datetime Functions
    • Aggregate Functions
    • Data Type Conversion Functions
    • Window Functions
    • Series Data Functions
    • Miscellaneous Functions

    Numeric Functions

    These are inbuilt numeric functions in SQL and use in scripting. It takes numeric values or strings with numeric characters and return numeric values.
    • ABS − It returns the absolute value of a numeric argument.
    Example  SELECT ABS (-1) "abs" FROM TEST;
    abs
    1
    ACOS, ASIN, ATAN, ATAN2 (These functions return trigonometric value of the argument)
    • BINTOHEX − It converts a Binary value to a hexadecimal value.
    • BITAND − It performs an AND operation on bits of passed argument.
    • BITCOUNT − It performs the count of number of set bits in an argument.
    • BITNOT − It performs a bitwise NOT operation on the bits of argument.
    • BITOR − It perform an OR operation on bits of passed argument.
    • BITSET − It is used to set bits to 1 in <target_num> from the <start_bit> position.
    • BITUNSET − It is used to set bits to 0 in <target_num> from the <start_bit> position.
    • BITXOR − It performs XOR operation on bits of passed argument.
    • CEIL − It returns the first integer that is greater or equal to the passed value.
    • COS, COSH, COT ((These functions return trigonometric value of the argument)
    • EXP − It returns the result of the base of natural logarithms e raised to the power of passed value.
    • FLOOR − It returns the largest integer not greater than the numeric argument.
    • HEXTOBIN − It converts a hexadecimal value to a binary value.
    • LN − It returns the natural logarithm of the argument.
    • LOG − It returns the algorithm value of a passed positive value. Both base and log value should be positive.
    Various other numeric functions can also be used − MOD, POWER, RAND, ROUND, SIGN, SIN, SINH, SQRT, TAN, TANH, UMINUS

    String Functions

    Various SQL string functions can be used in HANA with SQL scripting. Most common string functions are −
    • ASCII − It returns integer ASCII value of passed string.
    • CHAR − It returns the character associated with passed ASCII value.
    • CONCAT − It is Concatenation operator and returns the combined passed strings.
    • LCASE − It converts all character of a string to Lower case.
    • LEFT − It returns the first characters of a passed string as per mentioned value.
    • LENGTH − It returns the number of characters in passed string.
    • LOCATE − It returns the position of substring within passed string.
    • LOWER − It converts all characters in string to lowercase.
    • NCHAR − It returns the Unicode character with passed integer value.
    • REPLACE − It searches in passed original string for all occurrences of search string and replaces them with replace string.
    • RIGHT − It returns the rightmost passed value characters of mentioned string.
    • UPPER − It converts all characters in passed string to uppercase.
    • UCASE − It is identical to UPPER function. It converts all characters in passed string to uppercase.
    Other string functions that can be used are − LPAD, LTRIM, RTRIM, STRTOBIN, SUBSTR_AFTER, SUBSTR_BEFORE, SUBSTRING, TRIM, UNICODE, RPAD, BINTOSTR

    Date Time functions

    There are various Date Time functions that can be used in HANA in SQL scripts. Most common Date Time functions are −
    • CURRENT_DATE − It returns the current local system date.
    • CURRENT_TIME − It returns the current local system time.
    • CURRENT_TIMESTAMP − It returns the current local system timestamp details (YYYY-MM-DD HH:MM:SS:FF).
    • CURRENT_UTCDATE − It returns current UTC (Greenwich Mean date) date.
    • CURRENT_UTCTIME − It returns current UTC (Greenwich Mean Time) time.
    • CURRENT_UTCTIMESTAMP
    • DAYOFMONTH − It returns the integer value of day in passed date in argument.
    • HOUR − It returns integer value of hour in passed time in argument.
    • YEAR − It returns the year value of passed date.
    Other Date Time functions are − DAYOFYEAR, DAYNAME, DAYS_BETWEEN, EXTRACT, NANO100_BETWEEN, NEXT_DAY, NOW, QUARTER, SECOND, SECONDS_BETWEEN, UTCTOLOCAL, WEEK, WEEKDAY, WORKDAYS_BETWEEN, ISOWEEK, LAST_DAY, LOCALTOUTC, MINUTE, MONTH, MONTHNAME, ADD_DAYS, ADD_MONTHS, ADD_SECONDS, ADD_WORKDAYS

    Data Type Conversion Functions

    These functions are used to convert one data type to other or to perform a check if conversion is possible or not.
    Most common data type conversion functions used in HANA in SQL scripts −
    • CAST − It returns the value of an expression converted to a supplied data type.
    • TO_ALPHANUM − It converts a passed value to an ALPHANUM data type
    • TO_REAL − It converts a value to a REAL data type.
    • TO_TIME − It converts a passed time string to the TIME data type.
    • TO_CLOB − It converts a value to a CLOB data type.
    Other similar Data Type conversion functions are − TO_BIGINT, TO_BINARY, TO_BLOB, TO_DATE, TO_DATS, TO_DECIMAL, TO_DOUBLE, TO_FIXEDCHAR, TO_INT, TO_INTEGER, TO_NCLOB, TO_NVARCHAR, TO_TIMESTAMP, TO_TINYINT, TO_VARCHAR, TO_SECONDDATE, TO_SMALLDECIMAL, TO_SMALLINT
    There are also various Windows and other miscellaneous functions that can be used in HANA SQL scripts.
    • Current_Schema − It returns a string containing the current schema name.
    • Session_User − It returns the user name of current session
  • SAP HANA - SQL Operators

    An operator is a special character used primarily in SQL statement's with WHERE clause to perform operation, such as comparisons and arithmetic operations. They are used to pass conditions in a SQL query.
    Operator types given below can be used in SQL statements in HANA −
    • Arithmetic Operators
    • Comparison/Relational Operators
    • Logical Operators
    • Set Operators

    Arithmetic Operators

    Arithmetic operators are used to perform simple calculation functions like addition, subtraction, multiplication, division and percentage.
    OperatorDescription
    +Addition − Adds values on either side of the operator
    -Subtraction − Subtracts right hand operand from left hand operand
    *Multiplication − Multiplies values on either side of the operator
    /Division − Divides left hand operand by right hand operand
    %Modulus − Divides left hand operand by right hand operand and returns remainder

    Comparison Operators

    Comparison operators are used to compare the values in SQL statement.
    OperatorDescription
    =Checks if the values of two operands are equal or not, if yes then condition becomes true.
    !=Checks if the values of two operands are equal or not, if values are not equal then condition becomes true.
    <>Checks if the values of two operands are equal or not, if values are not equal then condition becomes true.
    >Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true.
    <Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true.
    >=Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true.
    <=Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true.
    !<Checks if the value of left operand is not less than the value of right operand, if yes then condition becomes true.
    !>Checks if the value of left operand is not greater than the value of right operand, if yes then condition becomes true.

    Logical operators

    Logical operators are used to pass multiple conditions in SQL statement or are used to manipulate the results of conditions.
    OperatorDescription
    ALLThe ALL Operator is used to compare a value to all values in another value set.
    ANDThe AND operator allows the existence of multiple conditions in an SQL statement's WHERE clause.
    ANYThe ANY operator is used to compare a value to any applicable value in the list according to the condition.
    BETWEENThe BETWEEN operator is used to search for values that are within a set of values, given the minimum value and the maximum value.
    EXISTSThe EXISTS operator is used to search for the presence of a row in a specified table that meets certain criteria.
    INThe IN operator is used to compare a value to a list of literal values that have been specified.
    LIKEThe LIKE operator is used to compare a value to similar values using wildcard operators.
    NOTThe NOT operator reverses the meaning of the logical operator with which it is used. Eg − NOT EXISTS, NOT BETWEEN, NOT IN, etc. This is a negate operator.
    ORThe OR operator is used to compare multiple conditions in an SQL statement's WHERE clause.
    IS NULLThe NULL operator is used to compare a value with a NULL value.
    UNIQUEThe UNIQUE operator searches every row of a specified table for uniqueness (no duplicates).

    Set Operators

    Set operators are used to combine results of two queries into a single result. Data type should be same for both the tables.
    • UNION − It combines the results of two or more Select statements. However it will eliminate duplicate rows.
    • UNION ALL − This operator is similar to Union but it also shows the duplicate rows.
    • INTERSECT − Intersect operation is used to combine the two SELECT statements, and it returns the records, which are common from both SELECT statements. In case of Intersect, the number of columns and datatype must be same in both the tables.
    • MINUS − Minus operation combines result of two SELECT statements and return only those results, which belong to first set of result and eliminate the rows in second statement from the output of first.
  • SAP HANA - Data Types

    You can create row or Column store tables in SAP HANA using create table option. A table can be created by executing a data definition create table statement or using graphical option in HANA studio.
    When you create a table, you also need to define attributes inside it.
    SQL statement to create a table in HANA Studio SQL Console −
    Create column Table TEST (
       ID INTEGER,
       NAME VARCHAR(10),
       PRIMARY KEY (ID)
    );
    Creating a table in HANA studio using GUI option −
    GUI Option
    When you create a table, you need to define the names of columns and SQL data types. The Dimension field tells the length of value and the Key option to define it as primary key.
    SAP HANA supports the following data types in a table −
    Data Types
    SAP HANA supports 7 categories of SQL data types and it depends on the type of data you have to store in a column.
    • Numeric
    • Character/ String
    • Boolean
    • Date Time
    • Binary
    • Large Objects
    • Multi-Valued
    The following table gives the list of data types in each category −
    Data Types Category

    Date Time

    These data types are used to store date and time in a table in HANA database.
    • DATE − data type consists of year, month and day information to represent a date value in a column. Default format for a Date data type is YYYY-MM-DD.
    • TIME − data type consists of hours, minutes, and seconds value in a table in HANA database. Default format for Time data type is HH: MI: SS.
    • SECOND DATE − data type consists of year, month, day, hour, minute, second value in a table in HANA database. Default format for SECONDDATE data type is YYYY-MM-DD HH:MM:SS.
    • TIMESTAMP − data type consists of date and time information in a table in HANA database. Default format for TIMESTAMP data type is YYYY-MM-DD HH:MM:SS:FFn, where FFn represents fraction of second.

    Numeric

    • TinyINT − stores 8 bit unsigned integer. Min value: 0 and max value: 255
    • SMALLINT − stores 16 bit signed integer. Min value: -32,768 and max value: 32,767
    • Integer − stores 32 bit signed integer. Min value: -2,147,483,648 and max value: 2,147,483,648
    • BIGINT − stores 64 bit signed integer. Min value: -9,223,372,036,854,775,808 and max value: 9,223,372,036,854,775,808
    • SMALL − Decimal and Decimal: Min value: -10^38 +1 and max value: 10^38 -1
    • REAL − Min Value:-3.40E + 38 and max value: 3.40E + 38
    • DOUBLE − stores 64 bit floating point number. Min value: -1.7976931348623157E308 and max value: 1.7976931348623157E308

    Boolean

    Boolean data types stores Boolean value, which are TRUE, FALSE

    Character

    • Varchar − maximum of 8000 characters.
    • Nvarchar − maximum length of 4000 characters
    • ALPHANUM − stores alphanumeric characters. Value for an integer is between 1 to 127.
    • SHORTTEXT − stores variable length character string which supports text search features and string search features.

    Binary

    Binary types are used to store bytes of binary data.
    VARBINARY − stores binary data in bytes. Max integer length is between 1 and 5000.

    Large Objects

    LARGEOBJECTS are used to store a large amount of data such as text documents and images.
    • NCLOB − stores large UNICODE character object.
    • BLOB − stores large amount of Binary data.
    • CLOB − stores large amount of ASCII character data.
    • TEXT − it enables text search features. This data type can be defined for only column tables and not for row store tables.
    • BINTEXT − supports text search features but it is possible to insert binary data.

    Multivalued

    Multivalued data types are used to store collection of values with same data type.

    Array

    Arrays store collections of value with the same data type. They can also contain null values.
  • CONTENT

    We are providing SAP HANA Administration Materials.

    CONTACT US

    For enquiries you can contact us in several different ways. Contact details are below.

    VENKAT SAP HANA

    • Street :Hyderabad
    • Person :Venkat
    • Country :INDIA
    • Email :venkatsaptraining@gmail.com

    For any Query,Please leave message

    Learn SAP HANA Be with latest update.