SAP HANA Database- SQL Reference Manual.pdf

182
     

Transcript of SAP HANA Database- SQL Reference Manual.pdf

  • 5/19/2018 SAP HANA Database- SQL Reference Manual.pdf

    1/182

  • 5/19/2018 SAP HANA Database- SQL Reference Manual.pdf

    2/182

    SAP AGDietmar-Hopp-Allee 1669190 WalldorfGermanyT +49/18 05/34 34 34F +49/18 05/34 34 20www.sap.com

    Copyright 2011 SAP AG. All rights reserved.

    No part of this publication may be reproduced or transmitted in any

    form or for any purpose without the express permission of SAP AG.

    The information contained herein may be changed without prior notice.

    Some software products marketed by SAP AG and its distributors

    contain proprietary software components of other software vendors.

    Copyright 2011 Sybase, Inc. All rights reserved. Unpublished rights

    reserved under U.S. copyright laws.

    Sybase, the Sybase logo, Adaptive Server, iAnywhere, Sybase 365,

    SQL Anywhere and other Sybase products and services mentioned

    herein as well as their respective logos are trademarks or registered

    trademarks of Sybase, Inc. All other trademarks are the property of

    their respective owners.

    Microsoft, Windows, Excel, Outlook, and PowerPoint are registered

    trademarks of Microsoft Corporation.

    IBM, DB2, DB2 Universal Database, System i, System i5, System p,

    System p5, System x, System z, System z10, System z9, z10, z9,

    iSeries, pSeries, xSeries, zSeries, eServer, z/VM, z/OS, i5/OS, S/390,

    OS/390, OS/400, AS/400, S/390 Parallel Enterprise Server, PowerVM,

    Power Architecture, POWER6+, POWER6, POWER5+, POWER5,

    POWER, OpenPower, PowerPC, BatchPipes, BladeCenter, System

    Storage, GPFS, HACMP, RETAIN, DB2 Connect, RACF, Redbooks,

    OS/2, Parallel Sysplex, MVS/ESA, AIX, Intelligent Miner, WebSphere,

    Netfinity, Tivoli and Informix are trademarks or registered trademarks

    of IBM Corporation.

    Linux is the registered trademark of Linus Torvalds in the U.S. and

    other countries.

    Adobe, the Adobe logo, Acrobat, PostScript, and Reader are either

    trademarks or registered trademarks of Adobe Systems Incorporated in

    the United States and/or other countries.

    Oracle is a registered trademark of Oracle Corporation.

    UNIX, X/Open, OSF/1, and Motif are registered trademarks of the Open

    Group.

    Citrix, ICA, Program Neighborhood, MetaFrame, WinFrame,

    VideoFrame, and MultiWin are trademarks or registered trademarks of

    Citrix Systems, Inc.

    HTML, XML, XHTML and W3C are trademarks or registered

    trademarks of W3C, World Wide Web Consortium, Massachusetts

    Institute of Technology.

    Java is a registered trademark of Sun Microsystems, Inc

    JavaScript is a registered trademark of Sun Microsystems, Inc., used

    under license for technology invented and implemented by Netscape.

    SAP, R/3, xApps, xApp, SAP NetWeaver, Duet, PartnerEdge,

    ByDesign, SAP Business ByDesign, and other SAP products and

    services mentioned herein as well as their respective logos are

    trademarks or registered trademarks of SAP AG in Germany and in

    several other countries all over the world. All other product and service

    names mentioned are the trademarks of their respective companies. Data

    contained in this document serves informational purposes only. National

    product specifications may vary.

    These materials are subject to change without notice. These materials

    are provided by SAP AG and its affiliated companies ("SAP Group")

    for informational purposes only, without representation or warranty of

    any kind, and SAP Group shall not be liable for errors or omissions with

    respect to the materials. The only warranties for SAP Group products

    and services are those that are set forth in the express warranty

    statements accompanying such products and services, if any. Nothing

    herein should be construed as constituting an additional warranty.

    Disclaimer

    Some components of this product are based on Java. Any code

    change in these components may cause unpredictable and severe

    malfunctions and is therefore expressively prohibited, as is any

    decompilation of these components.

    Any Java Source Code delivered with this product is only to be used

    by SAPs Support Services and may not be modified or altered in any

    way.

    Documentation in the SAP Service MarketplaceYou can find this documentation at the following Internet address:service.sap.com/hana

  • 5/19/2018 SAP HANA Database- SQL Reference Manual.pdf

    3/182

    SAP HANA Database: SQL Reference Manual

    2

    Table of Contents

    SAP HANA Guides .................................................................................................................................... 9

    Notation .............................................................................................................................................. 9

    Introduction .......................................................................................................................................... 10

    SQL .................................................................................................................................................... 10

    Supported Languages and Code Pages ............................................................................................. 10

    Comment .......................................................................................................................................... 10

    Identifiers .......................................................................................................................................... 10

    Single Quotation Mark ...................................................................................................................... 11

    Double Quotation Mark .................................................................................................................... 12

    SQL Reserved Words ......................................................................................................................... 12

    Data Types ............................................................................................................................................. 13

    Classification of Data Types .............................................................................................................. 13

    Datetime Types ................................................................................................................................. 13

    Numeric Types .................................................................................................................................. 13

    Character String Types ...................................................................................................................... 15Binary Types ...................................................................................................................................... 15

    Large Object (LOB) Types .................................................................................................................. 15

    Supported Formats for Date, Time and Timestamp ......................................................................... 17

    Data Type Conversion ....................................................................................................................... 19

    Predicates.............................................................................................................................................. 22

    Comparison Predicates ..................................................................................................................... 22

    Range Predicate ................................................................................................................................ 22

    In Predicate ....................................................................................................................................... 22

    Exists Predicate ................................................................................................................................. 22

    LIKE Predicate ................................................................................................................................... 22

    NULL Predicate .................................................................................................................................. 23

    Operators .............................................................................................................................................. 24

    Unary and Binary Operators ............................................................................................................. 24

    Operator Precedence ........................................................................................................................ 24

    Arithmetic Operators ........................................................................................................................ 25

  • 5/19/2018 SAP HANA Database- SQL Reference Manual.pdf

    4/182

    SAP HANA Database: SQL Reference Manual

    3

    String Operator ................................................................................................................................. 25

    Comparison Operators ...................................................................................................................... 25

    Logical Operators .............................................................................................................................. 26

    Set Operators .................................................................................................................................... 26

    Expressions............................................................................................................................................ 27

    Case Expressions ............................................................................................................................... 27

    Function Expressions ........................................................................................................................ 28

    Aggregate Expressions ...................................................................................................................... 28

    Subqueries in expressions ................................................................................................................. 29

    Functions ............................................................................................................................................... 30

    Data type conversion functions ........................................................................................................ 30

    CAST Function ............................................................................................................................... 30

    TO_ALPHANUM Function ............................................................................................................. 30

    TO_BIGINT Function ...................................................................................................................... 30

    TO_BLOB Function ........................................................................................................................ 31

    TO_CLOB Function ........................................................................................................................ 31

    TO_DATE Function ........................................................................................................................ 31

    TO_DATS Function ........................................................................................................................ 31

    TO_DECIMAL Function .................................................................................................................. 31

    TO_DOUBLE Function ................................................................................................................... 32

    TO_INT Function ........................................................................................................................... 32

    TO_INTEGER Function ................................................................................................................... 32

    TO_NCLOB Function ...................................................................................................................... 32

    TO_NVARCHAR Function .............................................................................................................. 33

    TO_REAL Function ......................................................................................................................... 33

    TO_SMALLINT Function ................................................................................................................ 33

    TO_TINYINT Function .................................................................................................................... 34

    TO_TIME Function ......................................................................................................................... 34

    TO_TIMESTAMP Function ............................................................................................................. 34

    TO_VARCHAR Function ................................................................................................................. 34

    DateTime Functions .......................................................................................................................... 36

    ADD_DAYS Function ...................................................................................................................... 36

    ADD_MONTHS Function ............................................................................................................... 36

    ADD_YEARS Function .................................................................................................................... 36

  • 5/19/2018 SAP HANA Database- SQL Reference Manual.pdf

    5/182

    SAP HANA Database: SQL Reference Manual

    4

    ADD_SECONDS Function ............................................................................................................... 36

    DAYS_BETWEEN Function ............................................................................................................. 37

    DAYNAME Function ...................................................................................................................... 37

    DAYOFMONTH Function ............................................................................................................... 37

    DAYOFYEAR Function .................................................................................................................... 37

    EXTRACT Function ......................................................................................................................... 38

    HOUR Function .............................................................................................................................. 38

    LAST_DAY Function ....................................................................................................................... 38

    MINUTE Function .......................................................................................................................... 38

    MONTH Function .......................................................................................................................... 38

    MONTHNAME Function ................................................................................................................ 39

    NEXT_DAY Function ...................................................................................................................... 39

    NOW Function ............................................................................................................................... 39

    SECOND Function .......................................................................................................................... 40

    SECONDS_BETWEEN Function ...................................................................................................... 40

    CURRENT_DATE Function ............................................................................................................. 40

    CURRENT_TIME Function .............................................................................................................. 40

    CURRENT_TIMESTAMP Function .................................................................................................. 41

    CURRENT_UTCDATE Function ....................................................................................................... 41

    CURRENT_UTCTIME Function ....................................................................................................... 41

    CURRENT_UTCTIMESTAMP Function............................................................................................ 41

    WEEK Function .............................................................................................................................. 42

    WEEKDAY Function ....................................................................................................................... 42

    YEAR Function ............................................................................................................................... 42

    Number Functions............................................................................................................................. 43

    ABS Function ................................................................................................................................. 43

    ACOS Function............................................................................................................................... 43

    ASIN Function ................................................................................................................................ 43

    ATAN Function .............................................................................................................................. 43

    ATAN2 Function ............................................................................................................................ 44

    BITAND Function ........................................................................................................................... 44

    CEIL / CEILING Function ................................................................................................................ 44

    COS Function ................................................................................................................................. 44

    COSH Function .............................................................................................................................. 45

  • 5/19/2018 SAP HANA Database- SQL Reference Manual.pdf

    6/182

    SAP HANA Database: SQL Reference Manual

    5

    COT Function ................................................................................................................................. 45

    EXP Function ................................................................................................................................. 45

    FLOOR Function ............................................................................................................................ 45

    GREATEST Function ....................................................................................................................... 46

    LEAST Function .............................................................................................................................. 46

    LN Function ................................................................................................................................... 46

    LOG Function ................................................................................................................................. 46

    MOD Function ............................................................................................................................... 47

    POWER Function ........................................................................................................................... 47

    ROUND Function ........................................................................................................................... 47

    SIGN Function................................................................................................................................ 48

    SIN Function .................................................................................................................................. 48

    SINH Function................................................................................................................................ 48

    SQRT Function ............................................................................................................................... 48

    TAN Function ................................................................................................................................. 49

    TANH Function .............................................................................................................................. 49

    String Functions ................................................................................................................................ 50

    ASCII Function ............................................................................................................................... 50

    CHAR Function .............................................................................................................................. 50

    CONCAT Function .......................................................................................................................... 50

    LEFT Function ................................................................................................................................ 50

    LCASE Function.............................................................................................................................. 51

    LENGTH Function .......................................................................................................................... 51

    LOCATE Function ........................................................................................................................... 51

    LOWER Function ........................................................................................................................... 51

    LPAD Function ............................................................................................................................... 52

    LTRIM Function ............................................................................................................................. 52

    NCHAR Function ............................................................................................................................ 52

    REPLACE Function ......................................................................................................................... 52

    RIGHT Function ............................................................................................................................. 53

    RPAD Function .............................................................................................................................. 53

    RTRIM Function ............................................................................................................................. 53

    SUBSTRING Function ..................................................................................................................... 54

    SUBSTR_AFTER Function ............................................................................................................... 54

  • 5/19/2018 SAP HANA Database- SQL Reference Manual.pdf

    7/182

    SAP HANA Database: SQL Reference Manual

    6

    SUBSTR_BEFORE Function ............................................................................................................ 54

    TRIM Function ............................................................................................................................... 55

    UCASE Function ............................................................................................................................. 55

    UNICODE Function ........................................................................................................................ 55

    UPPER Function ............................................................................................................................. 55

    Miscellaneous Functions ................................................................................................................... 57

    BINTOHEX Function ....................................................................................................................... 57

    COALESCE Function ....................................................................................................................... 57

    HASANYPRIVILEGES Function ........................................................................................................ 57

    HASSYSTEMPRIVILEGE Function ................................................................................................... 58

    HEXTOBIN Function ....................................................................................................................... 58

    ISAUTHORIZED Function ............................................................................................................... 58

    IFNULL Function ............................................................................................................................ 59

    NULLIF Function ............................................................................................................................ 59

    CURRENT_CONNECTION Function ................................................................................................ 59

    CURRENT_SCHEMA Function ........................................................................................................ 60

    CURRENT_USER Function ............................................................................................................. 60

    GROUPING_ID Function ................................................................................................................ 60

    SESSION_CONTEXT Function ......................................................................................................... 61

    SYSUUID Function ......................................................................................................................... 62

    SQL Statements ..................................................................................................................................... 63

    ALTER AUDIT POLICY ..................................................................................................................... 63

    ALTER SYSTEM ............................................................................................................................... 64

    ALTER TABLE.................................................................................................................................. 69

    ALTER USER ................................................................................................................................... 74

    CONNECT ....................................................................................................................................... 76

    CREATE AUDIT POLICY................................................................................................................... 77

    CREATE CALCULATION SCENARIO ................................................................................................. 78

    CREATE INDEX ............................................................................................................................... 80

    CREATE ROLE ................................................................................................................................. 81

    CREATE SCHEMA ........................................................................................................................... 82

    CREATE SEQUENCE........................................................................................................................ 83

    CREATE SYNONYM ........................................................................................................................ 86

    CREATE TABLE ............................................................................................................................... 87

  • 5/19/2018 SAP HANA Database- SQL Reference Manual.pdf

    8/182

    SAP HANA Database: SQL Reference Manual

    7

    CREATE TYPE ................................................................................................................................. 93

    CREATE USER ................................................................................................................................. 94

    CREATE VIEW ................................................................................................................................ 95

    DELETE........................................................................................................................................... 96

    DROP AUDIT POLICY ...................................................................................................................... 97

    DROP CALCULATION SCENARIO .................................................................................................... 98

    DROP INDEX .................................................................................................................................. 99

    DROP ROLE .................................................................................................................................. 100

    DROP SCHEMA ............................................................................................................................ 101

    DROP SEQUENCE ......................................................................................................................... 102

    DROP SYNONYM ......................................................................................................................... 104

    DROP TABLE ................................................................................................................................ 105

    DROP TYPE .................................................................................................................................. 106

    DROP USER .................................................................................................................................. 107

    DROP VIEW.................................................................................................................................. 108

    EXPLAIN PLAN ............................................................................................................................. 109

    EXPORT ........................................................................................................................................ 114

    GRANT ......................................................................................................................................... 115

    IMPORT ....................................................................................................................................... 120

    IMPORT FROM ............................................................................................................................ 121

    INSERT ......................................................................................................................................... 123

    LOAD ........................................................................................................................................... 124

    MERGE DELTA ............................................................................................................................. 125

    RENAME COLUMN ...................................................................................................................... 126

    RENAME INDEX ........................................................................................................................... 127

    RENAME TABLE ........................................................................................................................... 128

    REVOKE ....................................................................................................................................... 129

    SELECT ......................................................................................................................................... 130

    SET [SESSION] .............................................................................................................................. 141

    SET HISTORY SESSION TO ............................................................................................................ 142

    SET SCHEMA ................................................................................................................................ 143

    SET TRANSACTION AUTOCOMMIT DDL ..................................................................................... 144

    SET TRANSACTION ...................................................................................................................... 145

    TRUNCATE TABLE ........................................................................................................................ 147

  • 5/19/2018 SAP HANA Database- SQL Reference Manual.pdf

    9/182

    SAP HANA Database: SQL Reference Manual

    8

    UNLOAD ...................................................................................................................................... 148

    UNSET [SESSION] ......................................................................................................................... 149

    UPDATE ....................................................................................................................................... 150

    UPSERT| REPLACE ....................................................................................................................... 151

    Appendix ............................................................................................................................................. 152

    Restrictions for SQL Statements ..................................................................................................... 152

    SQL Error Codes .............................................................................................................................. 153

  • 5/19/2018 SAP HANA Database- SQL Reference Manual.pdf

    10/182

    SAP HANA Database: SQL Reference Manual

    9

    SAP HANA GuidesFor more information about SAP HANA landscape, security, installation and administration, see the

    resources listed in the table below.

    Topic Guide/Tool Quick Link

    SAP HANA

    Landscape,

    Deployment &

    Installation

    SAP HANA KnowledgeCenter on SAP ServiceMarketplace

    https://service.sap.com/hana

    SAP HANA 1.0 Master Guide

    SAP HANA 1.0 Installation Guide

    SAP HANAAdministration &Security

    SAP HANA KnowledgeCenter on SAP HelpPortal

    http://help.sap.com/hana

    SAP HANA 1.0 Technical Operations Manual

    SAP HANA 1.0 Security Guide

    NotationThis reference use BNF (Backus Naur Form) which is the notation technique used to define

    programming languages, to describe SQL. BNF describes the syntax of a grammar using a set of

    production rules using a set of symbols.

    Symbols used in BNF

    Symbol Description

    < > Angle brackets are used to surround the name of a syntactic element (BNF

    nonterminal) of the SQL language.

    ::= The definition operator is used to provide definitions of the element appearedon the left side of the operator in a production rule.

    [ ] Square brackets are used to indicate optional elements in a formula. Optional

    elements may be specified or omitted.

    { } Braces group elements in a formula. Repetitive elements (zero or more

    elements) can be specified within brace symbols.

    | The alternative operator indicates that the portion of the formula following

    the bar is an alternative to the portion preceding the bar.

    ... The ellipsis indicates that the element may be repeated any number of times.

    If ellipsis appears after grouped elements specifying that the grouped

    elements enclosed with braces are repeated. If ellipsis appears after a single

    element, only that element is repeated.

    !! Introduces normal English text. This is used when the definition of a syntactic

    element is not expressed in BNF.

    https://service.sap.com/hanahttps://service.sap.com/hanahttps://service.sap.com/~sapidb/011000358700000604552011https://service.sap.com/~sapidb/011000358700000604552011https://service.sap.com/~sapidb/011000358700000604562011https://service.sap.com/~sapidb/011000358700000604562011http://help.sap.com/hanahttp://help.sap.com/hanahttp://help.sap.com/hanahttps://service.sap.com/~sapidb/011000358700000604562011https://service.sap.com/~sapidb/011000358700000604552011https://service.sap.com/hana
  • 5/19/2018 SAP HANA Database- SQL Reference Manual.pdf

    11/182

    SAP HANA Database: SQL Reference Manual

    10

    IntroductionThis chapter describes the SAP HANA Database implementation of Structured Query Language

    (SQL). It explains the characteristics of SQL, also how to manage comments and reserve words.

    SQL

    SQL stands for Structured Query Language. It is a standardized language for communicating witha relational database. It is used to retrieve, store or manipulate information in the database.

    SAP HANA Database manages tables using SQL statements to create or modify a table and to addor manipulate data within a table.

    SQL statements can perform the following tasks:

    Schema definition and manipulation Data manipulation System management Session management Transaction management

    Supported Languages and Code PagesThe SAP HANA Database supports Unicode to allow use of all languages in the Unicode Standardand 7 Bit ASCII code page without restriction.

    CommentYou can add comments to improve readability and maintainability of your SQL statements.

    Comments are delimited in SQL statements as follows: Double hyphens . Everything after the double hyphen until the end of a line is

    considered by the SQL parser to be a comment

    "/*" and "*/". This style of commenting is used to place comments on multiple lines. All

    text between the opening "/*" and closing "*/" is ignored by the SQL parser.

    Identifiers

    Syntax:

    ::= |

    ::= [{|}, ...]

    ::= A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S| T | U | V | W | X | Y | Z | a | b | c | d | e | f | g | h | i | j | k | l | m| n | o | p | q | r | s | t | u | v | w | x | y | z

    ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

    ::= |

    ::= _ ::= "

    ::= any character

    Identifiers are used to represent names used in SQL statement including table name, view name,

    synonym name, column name, index name, function name, procedure name, user name, role

    name, and so on. There are two kinds of identifiers; undelimited identifiers and delimited

    identifiers.

    Undelimited table and column names must start with a letter and cannot contain anysymbols other than digits or an underscore "_".

    Delimited identifiers are enclosed in the delimiter, double quotes, then the identifier can

    contain any character including special characters. For example, AB$%CD is a valididentifier name.

    Limitations

  • 5/19/2018 SAP HANA Database- SQL Reference Manual.pdf

    12/182

    SAP HANA Database: SQL Reference Manual

    11

    o "_SYS_" is reserved exclusively for database engine, hence not allowed at thebeginning of schema object names.

    o Role name and user name must be specified as undelimited identifiers.o Maximum length for the identifiers is 127 characters.

    Single Quotation Mark

    Single quotation marks are used to delimit string literals and single quotation mark itself can berepresented using two single quotation marks.

  • 5/19/2018 SAP HANA Database- SQL Reference Manual.pdf

    13/182

    SAP HANA Database: SQL Reference Manual

    12

    Double Quotation MarkDouble quotation marks are used to delimit identifiers and double quotation mark itself can be

    represented using two double quotation marks.

    SQL Reserved Words

    Reserved words are words which have a special meaning to the SQL parser in the SAP HANADatabase that cannot be used as a user-defined name. Reserved words should not be used in SQL

    statements for schema object names. If necessary, you can work around this limitation by

    delimiting a table or column name with double quotation marks.

    The following table lists all the current and future reserved words for the SAP HANA Database.

    Table 1. Reserved Words

    ALL ALTER AS BEFORE

    BEGIN BOTH CASE CHAR

    CONDITION CONNECT CROSS CUBE

    CURRENT_CONNECTION CURRENT_DATE CURRENT_SCHEMA CURRENT_TIME

    CURRENT_TIMESTAMP CURRENT_USER CURRENT_UTCDATE CURRENT_UTCTIME

    CURRENT_UTCTIMESTAMP CURRVAL CURSOR DECLARE

    DISTINCT ELSE ELSEIF ELSIF

    END EXCEPT EXCEPTION EXEC

    FOR FROM FULL GROUP

    HAVING IF IN INNER

    INOUT INTERSECT INTO IS

    JOIN LEADING LEFT LIMIT

    LOOP MINUS NATURAL NEXTVAL

    NULL ON ORDER OUT

    PRIOR RETURN RETURNS REVERSE

    RIGHT ROLLUP ROWID SELECT

    SET SQL START SYSDATE

    SYSTIME SYSTIMESTAMP SYSUUID TOP

    TRAILING UNION USING UTCDATE

    UTCTIME UTCTIMESTAMP VALUES WHEN

    WHERE WHILE WITH

    Undelimited table and column names must start with a letter and cannot contain any symbols

    other than digits or an underscore "_". If the identifier is enclosed in the delimiter, double quotes,

    then the identifier can contain any character including special characters. For example, AB$%CD

    is a valid identifier name. However, "_SYS_" is reserved exclusively for database engine, hence not

    allowed at the beginning of schema object names.

  • 5/19/2018 SAP HANA Database- SQL Reference Manual.pdf

    14/182

    SAP HANA Database: SQL Reference Manual

    13

    Data TypesThis section describes the data types used in the SAP HANA Database.

    Data type specifies the characteristics of a data value. A special value of NULL is included in everydata type to indicate the absence of a value. The following table shows the built-in data types inSAP HANA Database.

    Classification of Data TypesIn the SAP HANA Database each data type can be classified by its characteristic as follows:

    Table 2. Classification of data types

    Classification Data Type

    Datetime types DATE, TIME, SECONDDATE, TIMESTAMP

    Numeric types TINYINT, SMALLINT, INTEGER, BIGINT, SMALLDECIMAL,

    DECIMAL, REAL, DOUBLE, FLOAT

    Character string types VARCHAR, NVARCHAR, ALPHANUM

    Binary types VARBINARY

    Large Object types BLOB, CLOB, NCLOB

    Datetime Types

    DATEThe DATEdata type consists of year, month, and day information to represent a date value.

    The default format for the DATEdata type is 'YYYY-MM-DD'. YYYY represents the year, MMrepresents the month, and DD represents the day. The range of date value is 0001-01-01through 9999-12-31.

    TIMEThe TIMEdata type consists of hour, minute, and second to represent a time value. Thedefault format for the TIMEdata type is 'HH24:MI:SS'. HH24 represents the hour from 0 to 24,MI represents the minute from 0 to 59, SS represents the second from 0 to 59.

    SECONDDATEThe SECONDDATEdata type consists of year, month, day, hour, minute and secondinformation to represent a date with time value. The default format for the SECONDDATEdata type is 'YYYY-MM-DD HH24:MI:SS'. YYYY represents the year, MM represents the month,DD represents the day, HH24 represents hour, MI represents minute, and SS represents

    seconds. The range of date value is 0001-01-01 00:00:01 through 9999-12-31 24:00:00.

    TIMESTAMPThe TIMESTAMPdata type consists of date and time information. Its default format is 'YYYY-MM-DD HH24:MI:SS.FF7'. FFnrepresents the fractional seconds where nindicates the number

    of digits in fractional part. . The range of the timestamp value is 0001-01-0100:00:00.0000000 through 9999-12-31 23:59:59.9999999.

    For details on supported formats for datetime types, refer to Table 4, Table 5, Table 6 andTable 7.

    Numeric Types

    TINYINT

    TheTINYINTdata type stores an 8-bit unsigned integer. The minimum value is 0 and themaximum value is 255 for TINYINT.

  • 5/19/2018 SAP HANA Database- SQL Reference Manual.pdf

    15/182

    SAP HANA Database: SQL Reference Manual

    14

    SMALLINTTheSMALLINTdata type stores a 16-bit signed integer. The minimum value is -32,768 and

    the maximum value is 32,767 for SMALLINT.

    INTEGERThe INTEGERdata type stores a 32-bit signed integer. The minimum value is -2,147,483,648

    and the maximum value is 2,147,483,647 for INTEGER.

    BIGINTThe BIGINTdata type stores a 64-bit signed integer. The minimum value is -

    9,223,372,036,854,775,808 and the maximum value is 9,223,372,036,854,775,807 for

    BIGINT.

    DECIMAL(precision, scale) or DEC(p,s)The DECIMAL (p, s) data type specifies a fixed-point decimal number with precision p andscale s. The precision is the total number of significant digits and can range from 1 to 34.

    The scale is the number of digits from the decimal point to the least significant digit and canrange from -6,111 to 6,176 which means scale specifies the range of the exponent in thedecimal number from 10-6111to 106176. If the scale is not specified, it defaults to 0. Scale ispositive when the number has significant digits to the right of the decimal point and negativewhen the number has significant digits to the left of the decimal point.

    Examples:0.0000001234 (1234 x 10-10) has the precision 4 and the scale 10.1.0000001234 (10000001234 x 10-10) has the precision 11 and scale 10.

    1234000000 (1234x106) has the precision 4 and scale -6.

    When precision and scale are not specified, DECIMALbecomes a floating-point decimalnumber. In this case, precision and scale can vary within the range described above, 1~34 for

    precision and -6,111~6,176 for scale depending on the stored value.

    SMALLDECIMALThe SMALLDECIMALis a floating-point decimal number. The precision and scale can varywithin the range, 1~16 for precision and -369~368 for scale depending on the stored value.SMALLDECIMAL is supported only on column store.

    DECIMAL and SMALLDECIMAL are floating-point types. For instance, a decimal column canstore any of 3.14, 3.1415, 3.141592 keeping their precisions.

    DECIMAL(p, s) is the SQL standard notation for fixed-point decimal. For instance, 3.14, 3.1415,3.141592 are stored in a decimal(5, 4) column as 3.1400, 3.1415, 3.1416, respectivelykeeping the specified precision(5) and scale(4).

    REALThe REALdata type specifies a single-precision 32-bit floating-point number.

    DOUBLEThe DOUBLEdata type specifies a single-precision 64-bit floating-point number. The minimumvalue is -1.79769 x 10308 and the maximum value is 1.79769x10308 . The smallest positiveDOUBLE value is 2.2207x10-308and the largest negative DOUBLE value is -2.2207x10-308.

    FLOAT(n)The FLOAT(n)data type specifies a 32-bit or 64-bit real number, where n specifies thenumber of significant bits and can range between 1 and 53.

  • 5/19/2018 SAP HANA Database- SQL Reference Manual.pdf

    16/182

    SAP HANA Database: SQL Reference Manual

    15

    When you use the FLOAT(n)data type, if n is smaller than 25, it becomes a 32-bit REAL datatype. If n is greater than or equal to 25, it then becomes a 64-bit DOUBLE data type. If n isnot declared, it becomes a 64-bit double data type by default.

    Character String Types

    The character string data types are used to store values that contain character strings. WhileVARCHAR data types contain ASCII character strings, NVARCHAR are used for storing Unicodecharacter strings.

    VARCHARThe VARCHAR(n)data type specifies a variable-length ASCII character string, where n

    indicates the maximum length and is an integer between 1 and 5000.

    NVARCHARThe NVARCHAR(n)data type specifies a variable-length Unicode character set string, wheren indicates the maximum length and is an integer between 1 and 5000.

    ALPHANUMThe ALPHANUM(n)data type specifies a variable-length character string which containsalpha-numeric characters, where n indicates the maximum length and is an integer between 1and 127.

    Binary TypesBinary types are used to store bytes of binary data.

    VARBINARYThe VARBINARY(n)data type is used to store binary data of a specified maximum length inbytes, where n indicates the maximum length and is an integer between 1 and 5000.

    Large Object (LOB) TypesLOB (large objects) data types, CLOB, NCLOB and BLOB, are used to store a large amount of datasuch as text documents and images. The maximum size of an LOB is 2 GB.

    BLOBThe BLOBdata type is used to store large binary data.

    CLOBThe CLOBdata type is used to store large ASCII character data.

    NCLOB

    The NCLOBdata type is used to store a large Unicode character object.

    LOB types are provided for storing and retrieving such large data. LOB types support the followingoperations. The length () function returns the LOB length in bytes. LIKE can be used to search LOB columns.

    The LOB types have the following restrictions: LOB columns cannot appear in ORDER BY or GROUP BY clauses. LOB columns cannot appear in FROM clauses as a join predicate. LOB columns cannot appear in WHERE clauses as a predicate except LIKE, CONTAINS, =, or

    . LOB columns cannot appear in SELECT clauses as an aggregate function argument. LOB columns cannot appear in SELECT DISTINCT clauses. LOB columns cannot be used in set operations such as EXCEPT. UNION ALL is an exception. LOB columns cannot be used as a primary key.

  • 5/19/2018 SAP HANA Database- SQL Reference Manual.pdf

    17/182

    SAP HANA Database: SQL Reference Manual

    16

    LOB columns cannot be used in CREATE INDEX statements. LOB columns cannot be used in statistics update statements.

    Table 3: Mapping between SQL Data Type and Column Store Data Type

    SQL Type Column Store Type

    Integer Types TINYINT, SMALLINT, INT CS_INT

    BIGINT CS_FIXED(18,0)

    Approximate Types REAL CS_FLOAT

    DOUBLE CS_DOUBLE

    FLOAT CS_DOUBLE

    FLOAT(p) CS_FLOAT, CS_DOUBLE

    Decimal Types DECIMAL CS_DECIMAL_FLOAT

    DECIMAL(p,s) CS_FIXED(p-s,s)

    SMALLDECIMAL CS_SDFLOAT

    Character Types VARCHAR CS_STRING,CS_ALPHANUM,CS_UNITDECFLOAT,CS_DATE,CS_TIME

    NVARCHAR CS_STRING,CS_ALPHANUM,CS_UNITDECFLOAT

    CLOB, NCLOB CS_STRING

    ALPHANUM CS_ALPHANUM

    Binary Types BLOB CS_RAW

    VARBINARY CS_RAW

    Date/Time Types DATE CS_DAYDATE, CS_DATE

    TIME CS_SECONDTIME, CS_TIME

    TIMESTAMP CS_LONGDATE, CS_DATE, CS_SECONDDATESECONDDATE CS_SECONDDATE

  • 5/19/2018 SAP HANA Database- SQL Reference Manual.pdf

    18/182

    SAP HANA Database: SQL Reference Manual

    17

    Supported Formats for Date, Time and Timestamp

    The following date/time formats can be used when parsing a string into a date/time type and

    converting a date/time type value into a string value. Please note that format for Timestamp is the

    combination of Date and Time with the additional support for fractional seconds.

    Table 4: Supported formats for Date

    Format Description Examples

    YYYY-MM-DD Default format INSERT INTO TBL VALUES ('1957-06-13');

    YYYY/MM/DD YYYY from 0001 to 9999, MM from

    1 to 12, DD from 1 to 31.

    If year has less than four digits,

    month has less than two digits, or

    day has less than two digits, thenvalues will be padded by one or

    more zeros. For example, a two

    digit year 45 will be saved as year

    0045, a one digit month 9 will be

    saved as 09, and a one digit day 2

    will be saved as 02.

    INSERT INTO TBL VALUES ('1957-06-13');

    INSERT INTO TBL VALUES ('1957/06/13');

    INSERT INTO TBL VALUES ('1957/06-13');

    INSERT INTO TBL VALUES ('1957-06/13');

    YYYY/MM-DD

    YYYY-MM/DD

    YYYYMMDD ABAP Data Type, DATS format INSERT INTO TBL VALUES ('19570613');

    MON Abbreviated name of month. (JAN.

    ~ DEC.)

    INSERT INTO TBL VALUES (TO_DATE('2040-

    Jan-10', 'YYYY-MON-DD'));

    INSERT INTO TBL VALUES (TO_DATE('Jan-

    10', 'MON-DD'));

    MONTH Name of month. (JANUARY -

    DECEMBER).

    INSERT INTO TBL VALUES (TO_DATE('2040-

    January-10', 'YYYY-MONTH-DD'));

    INSERT INTO TBL VALUES

    (TO_DATE('January-10', 'MONTH-DD'));

    RM Roman numeral month (I-XII; JAN

    = I).

    INSERT INTO TBL VALUES (TO_DATE('2040-

    I-10', 'YYYY-RM-DD'));

    INSERT INTO TBL VALUES (TO_DATE('I-10',

    'RM-DD'));

    DDD Day of year (1-366). INSERT INTO TBL VALUES (TO_DATE('204',

    'DDD'));

    INSERT INTO TBL VALUES (TO_DATE(2001-

    204,YYYY-DDD));

  • 5/19/2018 SAP HANA Database- SQL Reference Manual.pdf

    19/182

    SAP HANA Database: SQL Reference Manual

    18

    Table 5: Supported formats for Time

    Format Description Examples

    HH24:MI:SS Default format

    HH:MI[:SS][AM|PM]

    HH12:MI[:SS][AM|PM

    ]

    HH24:MI[:SS]

    HH from 0 to 23. MI from 0 to 59. SS from0 to 59. FFF from 0 to 999.

    If one digit hour, minute, second is

    specified, then 0 will be inserted into the

    value. For example, 9:9:9 will be saved as

    09:09:09.

    HH12 indicates 12 hour clock and HH24

    indicates 24 hour clock.

    AM or PM can be specified as a suffix to

    indicate the time value is before or after

    noon.

    INSERT INTO TBL VALUES('23:59:59');

    INSERT INTO TBL VALUES

    ('3:47:39 AM');

    INSERT INTO TBL VALUES

    ('9:9:9 AM');

    INSERT INTO TBL VALUES

    (TO_TIME('11:59:59','HH1

    2:MI:SS');

    SSSSS Seconds past midnight (0-86399). INSERT INTO TBL VALUES

    (TO_TIME('12345',

    'SSSSS'));

    Table 6: Supported formats for Timestamp

    Format Description Examples

    YYYY-MM-DDHH24:MI:SS.FF7

    Default format

    FF [1..7] Fractional seconds has the range

    1 to 7 after the FF parameter to

    specify the number of digits in

    the fractional second portion of

    the date time value returned.

    If a digit is not specified, the

    default value is used.

    INSERT INTO TBL VALUES

    (TO_TIMESTAMP('2011-05-11

    12:59.999','YYYY-MM-DD HH:SS.FF3'));

    The following date/time formats can additionally be used when converting a date/time type value

    into a string value. The following functions can be used when selecting a date/time values with a

    specific format.

    Table 7: Additional date/time formats

    Format Description Example

    D Day of week (1-7). TO_CHAR(CURRENT_TIMESTAMP,D)

    DAY Name of day (MONDAY - SUNDAY). TO_CHAR(CURRENT_TIMESTAMP,DAY)

    DY Abbreviated name of day (MON - SUN). TO_CHAR(CURRENT_TIMESTAMP,DY)

  • 5/19/2018 SAP HANA Database- SQL Reference Manual.pdf

    20/182

    SAP HANA Database: SQL Reference Manual

    19

    Format Description Example

    MON Abbreviated month name (JAN - DEC) TO_CHAR(CURRENT_TIMESTAMP,MON)

    MONTH Full month name (JANUARY - DECEMBER) TO_CHAR(CURRENT_TIMESTAMP,MONTH)

    RM Roman numeral month (I XII; I is forJanuary)

    TO_CHAR(CURRENT_TIMESTAMP,RM)

    Q Quarter of year (1, 2, 3, 4) TO_CHAR(CURRENT_TIMESTAMP,Q)

    W Week of month (1-5). TO_CHAR(CURRENT_TIMESTAMP,W)

    WW Week of year (1-53). TO_CHAR(CURRENT_TIMESTAMP,WW)

    Data Type Conversion

    This section describes the data type conversion allowed in SAP HANA Database.

    Explicit type conversion

    The type of an expression result, for example a field reference, a function on fields, or literalscan be converted using the following functions:CAST, TO_ALPHANUM, TO_BIGINT, TO_VARBINARY, TO_BLOB, TO_CLOB, TO_DATE, TO_DATS,TO_DECIMAL, TO_DOUBLE, TO_INTEGER, TO_INT, TO_NCLOB, TO_NVARCHAR, TO_REAL,TO_SECONDDATE, TO_SMALLINT, TO_TINYINT, TO_TIME, TO_TIMESTAMP, TO_VARCHAR.

    Implicit type conversion

    When a given set of operand/argument types does not match what an operator/function

    expects, type conversion is carried out by the SAP HANA Database. This conversion onlyoccurs if a relevant conversion is available and it makes the operation/function executable. Forinstance, a comparison of BIGINT and VARCHAR is performed by implicitly convertingVARCHAR to BIGINT. The entire explicit conversions can be used for implicit conversion exceptfor the TIME and TIMESTAMP data types. TIME and TIMESTAMP can be converted to eachother using TO_TIME(TIMESTAMP) and TO_TIMESTAMP(TIME).

    Sample examples

    Table 8. Implicit Type conversion Examples

    Input Expression Transformed Expression with Implicit Conversion

    BIGINT > VARCHAR BIGINT > BIGINT(VARCHAR)

    BIGINT > DECIMAL DECIMAL(BIGINT) > DECIMAL

    TIMESTAMP > DATE TIMESTAMP > TIMESTAMP(DATE)

    DATE > TIME Error because there is no conversion available between DATE and TIME

    In the table below, Boxes with OK mean data type conversions are allowed without any checks.

  • 5/19/2018 SAP HANA Database- SQL Reference Manual.pdf

    21/182

    SAP HANA Database: SQL Reference Manual

    20

    Boxes with CK mean the data type can be converted if the data is valid for the targettype.

    Boxes with - indicate that data type conversion is not allowed.

    The rule is applicable to both implicit and explicit conversion except for Time to Timestampconversion. Only explicit conversion is allowed for converting the Time data type to Timestamp

    using the TO_TIMESTAMP or CAST function.

    Table 9. Data type conversion table

    Targettinyint

    smallint

    integer

    bigint

    decimal

    decimal(p,s)

    smalldecimal

    real

    double

    varchar

    nvarchar

    Source

    tinyint OK OK OK OK OK OK OK OK OK OK

    smallint CK OK OK OK OK OK OK OK OK OK

    integer CK CK OK OK OK OK OK OK OK OK

    bigint CK CK CK OK CK CK CK OK OK OK

    decimal CK CK CK CK CK CK CK OK OK OK

    decimal(p,s) CK CK CK CK CK Ck CK CK CK OK OK

    smalldecimal CK CK CK CK OK CK CK CK OK OK

    real CK CK CK CK OK CK CK OK OK OK

    double CK CK CK CK CK CK CK CK OK OK

    varchar CK CK CK CK CK CK CK CK CK OK

    nvarchar CK CK CK CK CK CK CK CK CK CK

    Target

    time date seconddate timestamp varchar nvarcharSource

    time - - - OK OK

    date - OK OK OK OK

    seconddate time date timestamp OK OK

    timestamp time date seconddate OK OKvarchar CK CK CK CK OK

    nvarchar CK CK CK CK CK

    Target

    varbinary alphanum varchar nvarcharSource

    varbinary - - -

    alphanum - OK OK

    varchar OK OK OK

    nvarchar OK OK CK

  • 5/19/2018 SAP HANA Database- SQL Reference Manual.pdf

    22/182

    SAP HANA Database: SQL Reference Manual

    21

    Data Type Precedence

    This section describes the data type precedence provided by SAP HANA Database. Data typeprecedence specifies that the data type with lower precedence is converted to the data typewith higher precedence.

    Highest TIMESTAMP

    SECONDDATE

    DATE

    TIME

    DOUBLE

    REAL

    DECIMAL

    SMALLDECIMAL

    BIGINT

    INTEGER

    SMALLINT

    TINYINT

    NCLOB

    NVARCHAR

    CLOB

    VARCHAR

    BLOB

    Lowest VARBINARY

  • 5/19/2018 SAP HANA Database- SQL Reference Manual.pdf

    23/182

    SAP HANA Database: SQL Reference Manual

    22

    PredicatesA predicate is specified by combining one or more expressions or logical operators and returns oneof the following logical or truth values: TRUE, FALSE, or UNKNOWN.

    Comparison Predicates

    Two values can be compared using comparison predicates and returns true, false, or unknown.

    Syntax:

    ::= { = | != | | > | < | >= |

  • 5/19/2018 SAP HANA Database- SQL Reference Manual.pdf

    24/182

    SAP HANA Database: SQL Reference Manual

    23

    The percentage sign (%) matches zero or more characters and underscore (_) matches exactlyone character. To match a percent sign or underscore in the LIKE predicate, an escape charactermust be used.

    Using the optional argument, ESCAPE expression3,you can specify the escape character that willbe used so that the underscore (_) or percentage sign (%) can be matched.

    Syntax:

    ::= [NOT] LIKE [ESCAPE ]

    NULL PredicateWhen IS NULL predicate is specified, a value can be compared with NULL and returns true if avalue is NULL. If the IS NOT NULL predicate is specified, it returns true if a value is not NULL.

    Syntax:

    ::= IS [NOT] NULL

  • 5/19/2018 SAP HANA Database- SQL Reference Manual.pdf

    25/182

    SAP HANA Database: SQL Reference Manual

    24

    OperatorsYou can perform arithmetic operations in expressions by using operators. Operators can be usedfor calculation, value comparison or to assign values.

    Unary and Binary OperatorsTable 10. Unary and binary operators

    Operator Operation Format Description

    Unary A unary operator

    applies to one

    operand or a single

    value expression.

    operator operand unary plus operator(+)

    unary negation operator(-)

    logical negation(NOT)

    Binary A binary operator

    applies to two

    operands or twovalue expressions.

    operand1 operator

    operand2

    multiplicative operators ( *, / )

    additive operators ( +,- )

    comparison operators

    ( =,!=,,=)

    logical operators ( AND, OR )

    Operator PrecedenceAn expression can use several operators, if the number of operators is greater than one, then theSAP HANA Database will evaluate them in order of operator precedence. You can change thisordering by using parentheses. The SAP HANA Database will always evaluate expressionscontained within parentheses first.

    If parentheses are not used, the operators have the precedence indicated by the table below.

    Please note, the SAP HANA Database will evaluate operators with equal precedence from left toright within an expression.

    Table 11. SQL operator precedence

    Precedence Operator Operation

    Highest () parentheses

    +, - unary positive and negative operation

    *, / multiplication, division

    +, - addition, subtraction

    || concatenation

    =, !=, , =, IS

    NULL, LIKE, BETWEEN

    comparison

    NOT logical negation

    AND conjunction

  • 5/19/2018 SAP HANA Database- SQL Reference Manual.pdf

    26/182

    SAP HANA Database: SQL Reference Manual

    25

    Precedence Operator Operation

    Lowest OR disjunction

    Arithmetic OperatorsYou use arithmetic operators to perform mathematical operations such as adding, subtracting,multiplying, dividing and negation of numeric values.

    Table 12. Arithmetic operators

    Operator Description

    - Negation. If the expression is the NULL value, the result is NULL.

    + Addition. If either expression is the NULL value, the result is NULL.

    - Subtraction. If either expression is the NULL value, the result is NULL.

    * Multiplication. If either expression is NULL, the result is NULL.

    / Division. If either expression is NULL or if the second expression is 0, anerror is returned.

    String OperatorA concatenation operator combines two items such as strings, expressions, or constants into one.

    Table 13. Concatenation operators

    Operator Description

    || String concatenation (two vertical bars). If either string is NULL, itreturns NULL.

    Two string concatenation results in another string. For VARCHAR or NVARCHAR type strings,leading or trailing spaces are kept. If either string is of data type NVARCHAR, the result has data

    type NVARCHAR and is limited to 5000 characters. The maximum length for VARCHARconcatenation is also limited to 5000 characters.

    Comparison Operators

    Syntax:

    ::=

    Table 14. Comparison operators

    Operator Description Example

    = Equal to SELECT * FROM students WHERE id = 25;

    > Greater than SELECT * FROM students WHERE id > 25;

    < Less than SELECT * FROM students WHERE id < 25;

  • 5/19/2018 SAP HANA Database- SQL Reference Manual.pdf

    27/182

    SAP HANA Database: SQL Reference Manual

    26

    Operator Description Example

    >= Greater than or equal to SELECT * FROM students WHERE id >= 25;

  • 5/19/2018 SAP HANA Database- SQL Reference Manual.pdf

    28/182

    SAP HANA Database: SQL Reference Manual

    27

    ExpressionsAn expression is a clause that can be evaluated to return values.

    Syntax:

    expression ::=

    |

    |

    | ( )

    | ()

    | -

    |

    |

    |

    | [.]

    Case ExpressionsA case expression allows the user to use IF ... THEN ... ELSE logic without using procedures in SQLstatements.

    Syntax:

    case_expression ::=

    CASE

    WHEN

    THEN , ...

    [ ELSE ]

    { END | END CASE }

    If the expression following the CASE statement is equal to the expression following the WHEN

    statement, then the expression following the THEN statement is returned. Otherwise the

    expression following the ELSE statement is returned, if it exists.

  • 5/19/2018 SAP HANA Database- SQL Reference Manual.pdf

    29/182

    SAP HANA Database: SQL Reference Manual

    28

    Function ExpressionsSQL built-in functions can be used as an expression.

    Syntax:

    ::= ( , ... )

    Aggregate Expressions

    An aggregate expression uses an aggregate function to calculate a single value from the values ofmultiple rows in a column.

    Syntax:

    ::= COUNT(*) | ( [ ALL | DISTINCT ] ) ::= COUNT | MIN | MAX | SUM | AVG | STDDEV | VAR

    Aggregate name Description

    COUNT Counts the number of rows returned by a query. COUNT(*) returns the

    number of rows, regardless of the value of those rows and includingduplicate values.COUNT() returns the number of non-NULLvalues for that expression returned by the query.

    MIN Returns the minimum value of expression.

    MAX Returns the maximum value of expression.

    SUM Returns the sum of expression.

    AVG Returns the arithmetical mean of expression.

    STDDEV Returns the standard deviation of given expression as the square root ofVARIANCE function.

    VAR Returns the variance of expression as the square of standard deviation.

  • 5/19/2018 SAP HANA Database- SQL Reference Manual.pdf

    30/182

    SAP HANA Database: SQL Reference Manual

    29

    Subqueries in expressions

    A subquery is a SELECT statement enclosed in parentheses. The SELECT statement can containone and only one select list item. When used as an expression, a scalar subquery is allowed to

    return only zero or one value.

    Syntax:

    ::= ()

    Within the SELECT list of the top level SELECT, or in the SET clause of an UPDATE statement, youcan use a scalar subquery anywhere that you can use a column name. However, scalar_subquerycannot be used inside GROUP BY clause.

    Example:

    For example, the following statement returns the number of employees in each department,

    grouped by department name:

    SELECT DepartmentName, COUNT(*), out of,(SELECT COUNT(*) FROM Employees)FROM Departments AS D, Employees AS EWHERE D.DepartmentID = E.DepartmentIDGROUP BY DepartmentName;

  • 5/19/2018 SAP HANA Database- SQL Reference Manual.pdf

    31/182

    SAP HANA Database: SQL Reference Manual

    30

    FunctionsFunctions are used to return information from the database. They are allowed anywhere an

    expression is allowed. Functions use the same syntax conventions used by SQL statements.

    Data type conversion functions

    Data type conversion functions are used to convert arguments from one data type to another, or totest whether they can be converted.

    CAST Function

    Syntax:

    CAST (expression AS data_type)

    Description:

    Returns the value of an expression converted to a supplied data type.

    Parameters:

    expression - The expression to be converted.data type - The target data type.

    TINYINT | SMALLINT | INTEGER | BIGINT | DECIMAL | SMALLDECIMAL | REAL | DOUBLE |

    ALPHANUM | VARCHAR | NVARCHAR | DAYDATE | DATE | TIME | SECONDDATE |TIMESTAMP

    Example:

    SELECT CAST (7 AS VARCHAR) "cast" FROM DUMMY;

    Retrieves:cast7

    TO_ALPHANUM Function

    Syntax:

    TO_ALPHANUM (expression)

    Description:

    Converts the expression of a data type into a value of alphanum data type.

    Example:

    SELECT TO_ALPHANUM ('10') "to alphanum" FROM DUMMY;

    Retrieves:to alphanum10

    TO_BIGINT Function

    Syntax:

    TO_BIGINT (expression)

    Description:

    Converts the expression of a data type into a value of bigint data type.

    Example:

    SELECT TO_BIGINT ('10') "to bigint" FROM DUMMY;

    Retrieves:to bigint10

  • 5/19/2018 SAP HANA Database- SQL Reference Manual.pdf

    32/182

    SAP HANA Database: SQL Reference Manual

    31

    TO_BLOB Function

    Syntax:

    TO_BLOB (expression)

    Description:

    Converts the expression of a data type into a value of blob type. expressionmust be a binary string.

    Example:

    SELECT TO_BLOB (TO_BINARY('abcde')) "to blob" FROM DUMMY;

    Retrieves:to blobabcde

    TO_CLOB Function

    Syntax:

    TO_CLOB (expression)

    Description:

    Converts the expression of a data type into a value of CLOB data type.

    Example:

    SELECT TO_CLOB ('TO_CLOB converts an expression into a value of CLOB data type') "to clob"FROM DUMMY;

    Retrieves:to clobTO_CLOB converts an expression into a value of CLOB data type

    TO_DATE Function

    Syntax:TO_DATE (expression [, format])

    Description:

    Converts the expression of a data type into a value of DATE data type.

    Example:

    SELECT TO_DATE('2010-01-12', 'YYYY-MM-DD') "to date" FROM DUMMY;Retrieves:to date2010-01-12

    TO_DATS Function

    Syntax:

    TO_DATS (expression)

    Description:

    Converts the expression of a data type into a value of ABAP DATE string with formatYYYYMMDD.

    Example:

    SELECT TO_DATS ('2010-01-12') "abap date" FROM DUMMY;

    Retrieves:abap date20100112

    TO_DECIMAL Function

  • 5/19/2018 SAP HANA Database- SQL Reference Manual.pdf

    33/182

    SAP HANA Database: SQL Reference Manual

    32

    Syntax:

    TO_DECIMAL (expression [, precision, scale])

    Description:

    Converts the expression of a data type into a value of DECIMAL (precision, scale) data type.

    Example:

    SELECT TO_DECIMAL(7654321.89, 9, 2) "to decimal" FROM DUMMY;

    Retrieves:to decimal7654321.89

    TO_DOUBLE Function

    Syntax:

    TO_DOUBLE (expression)

    Description:

    Converts the expression of a data type into a value of DOUBLE (double precision) data type.

    Example:

    SELECT 3*TO_DOUBLE ('15.12') "to double" FROM DUMMY;

    Retrieves:to double45.36

    TO_INT Function

    Syntax:

    TO_INT (expression)

    Description:

    Converts the expression of a data type into a value of INTEGER data type.

    Example:

    SELECT TO_INT('10') "to int" FROM DUMMY;

    Retrieves:to int10

    TO_INTEGER Function

    Syntax:

    TO_INTEGER (expression)

    Description:

    Converts the expression of a data type into a value of INTEGER data type.

    Example:

    SELECT TO_INTEGER ('10') "to int" FROM DUMMY;

    Retrieves:to int10

    TO_NCLOB Function

    Syntax:

  • 5/19/2018 SAP HANA Database- SQL Reference Manual.pdf

    34/182

    SAP HANA Database: SQL Reference Manual

    33

    TO_NCLOB (expression)

    Description:

    Converts the expression of a data type into a value of NCLOB data type.

    Example:

    SELECT TO_NCLOB ('TO_NCLOB converts an expression into a value of NCLOB data type') "to

    nclob" FROM DUMMY;

    Retrieves:to nclobTO_NCLOB converts an expression into a value of NCLOB data type

    TO_NVARCHAR Function

    Syntax:

    TO_NVARCHAR (expression [,format])

    Description:

    Converts the expression of a data type into a value of unicode character data type.

    If formatis omitted, it converts to the corresponding format using the date format model.

    Example:

    SELECT TO_NVARCHAR(TO_DATE('2009/12/31'), 'YY-MM-DD') "to nchar" FROM DUMMY;

    Retrieves:to nchar09-12-31

    TO_REAL Function

    Syntax:

    TO_REAL (expression)

    Description:

    Converts the expression of a data type into the value of REAL (single precision) data type.

    Example:

    SELECT 3*TO_REAL ('15.12') "to real" FROM DUMMY;

    Retrieves:to real45.36

    TO_SMALLINT Function

    Syntax:

    TO_SMALLINT (expression)

    Description:

    Converts the expression of a data type into a value of SMALLINT data type.

    Example:

    SELECT TO_SMALLINT ('10') "to smallint" FROM DUMMY;

    Retrieves:to smallint10

  • 5/19/2018 SAP HANA Database- SQL Reference Manual.pdf

    35/182

    SAP HANA Database: SQL Reference Manual

    34

    TO_TINYINT Function

    Syntax:

    TO_TINYINT (expression)

    Description:

    Converts the expression of a data type into a value of TINYINT data type.

    Example:

    SELECT TO_TINYINT ('10') "to tinyint" FROM DUMMY;

    Retrieves:to tinyint10

    TO_TIME Function

    Syntax:

    TO_TIME (expression [, format])

    Description:Converts the expression of a data type into a value of TIME data type.If format is omitted, it converts expression into the corresponding format using the date format

    model as explained inTable 5.

    Example:

    SELECT TO_TIME ('08:30 AM',HH:MI AM) "to time" FROM DUMMY;

    Retrieves:to time08:30:00

    TO_TIMESTAMP Function

    Syntax:

    TO_TIMESTAMP (expression [, format])

    Description:

    Converts the expression of a data type into the TIMESTAMP data type.

    If format is omitted, it converts expression into the corresponding format using the date format

    model as explained inTable 5.

    Example:

    SELECT TO_TIMESTAMP ('2010-01-11 13:30:00', 'YYYY-MM-DD HH24:MI:SS') "to timestamp"FROM DUMMY;

    Retrieves:to timestamp2010-01-11 13:30:00.0000000

    TO_VARCHAR Function

    Syntax:

    TO_VARCHAR (expression [, format])

    Description:

    Converts the expression of a data type into a value of character data type.

    Example:

    SELECT TO_VARCHAR (TO_DATE('2009-12-31'), 'YYYY/MM/DD') "to char" FROM DUMMY;

  • 5/19/2018 SAP HANA Database- SQL Reference Manual.pdf

    36/182

    SAP HANA Database: SQL Reference Manual

    35

    Retrieves:to char2009/12/31

  • 5/19/2018 SAP HANA Database- SQL Reference Manual.pdf

    37/182

    SAP HANA Database: SQL Reference Manual

    36

    DateTime Functions

    ADD_DAYS Function

    Syntax:

    ADD_DAYS (d, n)

    Description:

    Computes the date d plus n days.

    Example:

    SELECT ADD_DAYS (TO_DATE ('2009-12-05', 'YYYY-MM-DD'), 30) "add days" FROM DUMMY;

    Retrieves:add days2010-01-04

    ADD_MONTHS Function

    Syntax:ADD_MONTHS (d, n)

    Description:

    Computes the date d plus n months.

    Example:

    SELECT ADD_MONTHS (TO_DATE ('2009-12-05', 'YYYY-MM-DD'), 1) "add months" FROM DUMMY;

    Retrieves:add months2010-01-05

    ADD_YEARS Function

    Syntax:

    ADD_YEARS (d, n)

    Description:

    Computes the date d plus n years.

    Example:

    SELECT ADD_YEARS (TO_DATE ('2009-12-05', 'YYYY-MM-DD'), 1) "add years" FROM DUMMY;

    Retrieves:add years2010-12-05

    ADD_SECONDS Function

    Syntax:

    ADD_SECONDS (t, n)

    Description:

    Computes the time t plus n seconds.

    Example:

    SELECT ADD_SECONDS (TO_TIME ('23:30:45'), 60*30) "add seconds" FROM DUMMY;

    Retrieves:add seconds

    00:00:45.000

  • 5/19/2018 SAP HANA Database- SQL Reference Manual.pdf

    38/182

    SAP HANA Database: SQL Reference Manual

    37

    DAYS_BETWEEN Function

    Syntax:

    DAYS_BETWEEN (date1, date2)

    Description:

    Computes the number of days between date1 and date2.

    Example:

    SELECT DAYS_BETWEEN (TO_DATE ('2009-12-05', 'YYYY-MM-DD'), TO_DATE('2010-01-05',

    'YYYYMM-DD')) "days between" FROM DUMMY;

    Retrieves:days between31

    DAYNAME Function

    Syntax:

    DAYNAME (expression)

    Description:

    Returns the name of the weekday in English.

    Example:

    SELECT DAYNAME ('2011-05-30') "dayname" FROM DUMMY;

    Retrieves:daynameMONDAY

    DAYOFMONTH Function

    Syntax:DAYOFMONTH (expression)

    Description:

    Returns the day of the month in integer.

    Example:

    SELECT DAYOFMONTH ('2011-05-30') "dayofmonth" FROM DUMMY;

    Retrieves:dayofmonth30

    DAYOFYEAR Function

    Syntax:

    DAYOFYEAR (expression)

    Description:

    Returns the day of the year in integer.

    Example:

    SELECT DAYOFYEAR ('2011-05-30') "dayofyear" FROM DUMMY;

    Retrieves:dayofyear150

  • 5/19/2018 SAP HANA Database- SQL Reference Manual.pdf

    39/182

    SAP HANA Database: SQL Reference Manual

    38

    EXTRACT Function

    Syntax:

    EXTRACT ({YEAR | MONTH | DAY | HOUR | MINUTE | SECOND} FROM datetime_value)

    Description:

    Finds and returns the value of a specified datetime field from a datetime_value.

    Example:

    SELECT EXTRACT (YEAR FROM TO_DATE ('2010-01-04', 'YYYY-MM-DD')) "extract" FROM DUMMY;

    Retrieves:extract2010

    HOUR Function

    Syntax:

    HOUR (expression)

    Description:Extract hour from expression.

    Example:

    SELECT HOUR ('12:34:56) "hour" FROM DUMMY;

    Retrieves:hour12

    LAST_DAY Function

    Syntax:

    LAST_DAY (d)

    Description:

    Returns the date of the last day of the month that contains d.

    Example:

    SELECT LAST_DAY (TO_DATE('2010-01-04', 'YYYY-MM-DD')) "last day" FROM DUMMY;

    Retrieves:last day2010-01-31

    MINUTE Function

    Syntax:

    MINUTE (expression)

    Description:

    Extract minute from expression.

    Example:

    SELECT MINUTE ('12:34:56) "minute" FROM DUMMY;

    Retrieves:minute34

    MONTH Function

  • 5/19/2018 SAP HANA Database- SQL Reference Manual.pdf

    40/182

    SAP HANA Database: SQL Reference Manual

    39

    Syntax:

    MONTH(expression)

    Description:

    Returns the number of the month from a given date.

    Example:

    SELECT MONTH ('2011-05-30) "month" FROM DUMMY;

    Retrieves:month5

    MONTHNAME Function

    Syntax:

    MONTHNAME(expression)

    Description:

    Returns the name of the month in English.

    Example:

    SELECT MONTHNAME ('2011-05-30) "monthname" FROM DUMMY;

    Retrieves:monthnameMAY

    NEXT_DAY Function

    Syntax:

    NEXT_DAY (d)

    Description:

    Returns the date of the next day of d.

    Example:

    SELECT NEXT_DAY (TO_DATE ('2009-12-31', 'YYYY-MM-DD')) "next day" FROM DUMMY;

    Retrieves:next day2010-01-01

    NOW Function

    Syntax:

    NOW ()

    Description:

    Returns the current timestamp.

    Example:

    SELECT NOW () "now" FROM DUMMY;

    Retrieves:now2010-01-01 16:34:19

  • 5/19/2018 SAP HANA Database- SQL Reference Manual.pdf

    41/182

    SAP HANA Database: SQL Reference Manual

    40

    SECOND Function

    Syntax:

    SECOND (t)

    Description:Returns the second number of a given time t.

    Example:

    SELECT SECOND (12:34:56) "second" FROM DUMMY;

    Retrieves:second56

    SECONDS_BETWEEN Function

    Syntax:

    SECONDS_BETWEEN (d1, d2)

    Description:

    Computes the number of seconds between d1 and d2, which is semantically equal to d2 d1.

    Example:

    SELECT SECONDS_BETWEEN ('2009-12-05', '2010-01-05') "seconds between" FROM DUMMY;

    Retrieves:Seconds between2678400

    CURRENT_DATE Function

    Syntax:CURRENT_DATE

    Description:

    Returns the current local system date.

    Example:

    SELECT CURRENT_DATE "current date" FROM DUMMY;

    Retrieves:current date2010-01-11

    CURRENT_TIME Function

    Syntax:

    CURRENT_TIME

    Description:

    Returns the current local system time.

    Example:

    SELECT CURRENT_TIME "current time" FROM DUMMY;

    Retrieves:current time17:37:37.279

  • 5/19/2018 SAP HANA Database- SQL Reference Manual.pdf

    42/182

    SAP HANA Database: SQL Reference Manual

    41

    CURRENT_TIMESTAMP Function

    Syntax:

    CURRENT_TIMESTAMP

    Description:

    Returns the current local system timestamp information.

    Example:

    SELECT CURRENT_TIMESTAMP "current timestamp" FROM DUMMY;

    Retrieves:current timestamp2010-01-11 17:38:48.802

    CURRENT_UTCDATE FunctionSyntax:

    CURRENT_UTCDATE

    Description:Returns the current UTC date. The UTC stands for Coordinated Universal Time, also known asGreenwich Mean Time (GMT).

    Example:

    SELECT CURRENT_UTCDATE "Coordinated Universal Date" FROM DUMMY;

    Retrieves:Coordinated Universal Time2010-01-11

    CURRENT_UTCTIME Function

    Syntax:

    CURRENT_UTCTIME

    Description:

    Returns the current UTC time.

    Example:

    SELECT CURRENT_UTCTIME "Coordinated Universal Time" FROM DUMMY;

    Retrieves:Coordinated Universal Time08:41:19.267

    CURRENT_UTCTIMESTAMP Function

    Syntax:

    CURRENT_UTCTIMESTAMP

    Description:

    Returns the current UTC timestamp.

    Example:

    SELECT CURRENT_UTCTIMESTAMP "Coordinated Universal Timestamp" FROM DUMMY;

    Retrieves:Coordinated Universal Timestamp2010-01-11 08:41:42.484

  • 5/19/2018 SAP HANA Database- SQL Reference Manual.pdf

    43/182

    SAP HANA Database: SQL Reference Manual

    42

    WEEK Function

    Syntax:

    WEEK (d)

    Description:

    Returns the week number of a given date d.

    Example:

    SELECT WEEK (TO_DATE('2011-05-30', 'YYYY-MM-DD')) "week" FROM DUMMY;

    Retrieves:week22

    WEEKDAY Function

    Syntax:

    WEEKDAY (d)

    Description:Returns the day of week of a given date d. The return value ranges from 0 to 6, each of which

    represents Monday to Sunday.

    Example:

    SELECT WEEKDAY (TO_DATE ('2010-12-31', 'YYYY-MM-DD')) "week day" FROM DUMMY;

    Retrieves:week day4

    YEAR Function

    Syntax:YEAR (d)

    Description:

    Returns the year number of a given date d.

    Example:

    SELECT YEAR (TO_DATE ('2011-05-30', 'YYYY-MM-DD')) "year" FROM DUMMY;

    Retrieves:year2011

  • 5/19/2018 SAP HANA Database- SQL Reference Manual.pdf

    44/182

    SAP HANA Database: SQL Reference Manual

    43

    Number FunctionsNumber functions take numeric values or strings with numeric characters as inputs and returnsnumeric values. When strings with numeric characters are given as inputs, implicit conversion fromstring to number is performed automatically before computing the result values.

    ABS Function

    Syntax :

    ABS (n)

    Description:

    Returns the absolute value of a numeric expression n.

    Example:

    SELECT ABS (-1) "absolute" FROM DUMMY;

    Retrieves:

    absolute

    1

    ACOS Function

    Syntax:

    ACOS (n)

    Description:

    Returns the arc-cosine, in radians, of a numeric expression n between -1 and 1.

    Example:

    SELECT ACOS (0.5) "acos" FROM DUMMY;

    Retrieves:

    acos1.0471975511965979

    ASIN Function

    Syntax:

    ASIN (n)

    Description:

    Returns the arc-sine, in radians, of a number n between -1 and 1.

    Example:

    SELECT ASIN (0.5) "asin" FROM DUMMY;

    Retrieves:asin0.5235987755982989

    ATAN Function

    Syntax :

    ATAN (n)

    Description:

    Returns the arc-tangent, in radians, of a number n. The argument is a number and the range of n

    is unlimited.

    Example:

  • 5/19/2018 SAP HANA Database- SQL Reference Manual.pdf

    45/182

    SAP HANA Database: SQL Reference Manual

    44

    SELECT ATAN (0.5) "atan" FROM DUMMY;

    Retrieves:atan0.4636476090008061

    ATAN2 Function

    Syntax :

    ATAN2 (n, m)

    Description:

    Returns the arc-tangent, in radians, of the ratio of two numbers n and m. This is the same resultas ATAN(n/m).

    Example:

    SELECT ATAN2 (1.0, 2.0) "atan2" FROM DUMMY;

    Retrieves:

    atan20.4636476090008061

    BITAND Function

    Syntax:

    BITAND (expression1, expression2)

    Description:

    Calculates anAND operation on the bits of expression1 and expression2. Both expression1 andexpression2 must be non-negative integers. The BITAND function returns a result with BIGINT type.

    Example:SELECT BITAND (255, 123) "bitand" FROM DUMMY;

    Retrieves:bitand123

    CEIL / CEILING Function

    Syntax :

    CEIL (n)

    Description:

    Returns the first integer that is greater or equal to a given value n. For positive numbers, this isknown as rounding up.

    Example:

    SELECT CEIL (14.5) "ceiling" FROM DUMMY;

    Retrieves:ceiling15

    COS Function

    Syntax:

    COS (n)

    Description:

  • 5/19/2018 SAP HANA Database- SQL Reference Manual.pdf

    46/182

    SAP HANA Database: SQL Reference Manual

    45

    Returns the cosine of the angle in radians given by its argument n.

    Example:

    SELECT COS (0.0) "cos" FROM DUMMY;

    Retrieves:cos1.0

    COSH Function

    Syntax:

    COSH (n)

    Description:

    Computes the hyperbolic cosine of n.

    Example:

    SELECT COSH (0.5) "cosh" FROM DUMMY;

    Retrieves:cosh1.1276259652063807

    COT Function

    Syntax:

    COT (n)

    Description:

    Computes the cotangent of n.

    Example:

    SELECT COT (40) "cot" FROM DUMMY;

    Retrieves:Cot1.5423510453569202

    EXP Function

    Syntax:

    EXP (n)

    Description:

    Returns the result of the base of natural logarithms e raised to the power of the given argument n.

    Example:SELECT EXP (1.0) "exp" FROM DUMMY;

    Retrieves:exp2.718281828459045

    FLOOR Function

    Syntax:

    FLOOR (n)

    Description:

    Returns the largest integer not greater than the given number n.

    Example:

  • 5/19/2018 SAP HANA Database- SQL Reference Manual.pdf

    47/182

    SAP HANA Database: SQL Reference Manual

    46

    SELECT FLOOR (14.5) "floor" FROM DUMMY;

    Retrieves:floor14

    GREATEST Function

    Syntax:

    GREATEST (n1 [, n2]...)

    Description:

    Returns the greatest value among the arguments: n1, n2, ...

    Example:

    SELECT GREATEST ('aa', 'ab', 'ba', 'bb') "greatest" FROM DUMMY;

    Retrieves:greatestbb

    LEAST Function

    Syntax:

    LEAST (n1 [, n2]...)

    Description:

    Retu