SAP In-Memory Database SQL Reference Guide SAP In-Memory Appliance (SAP HANA) 1.0 SPS 02 Target Audience ■ Consultants ■ Administrators ■ Others Public Document version 1.1 – 10/21/2011
SAP In-Memory Database SQL Reference Guide
SAP In-Memory Appliance (SAP HANA) 1.0 SPS 02
Target Audience
Consultants Administrators Others
Public
Document version 1.1 10/21/2011
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, 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, SAP NetWeaver, Duet, PartnerEdge, ByDesign, SAP
BusinessObjects Explorer, 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 other countries.
Business Objects and the Business Objects logo, BusinessObjects,
Crystal Reports, Crystal Decisions, Web Intelligence, Xcelsius, and
other Business Objects products and services mentioned herein as well
as their respective logos are trademarks or registered trademarks of
Business Objects Software Ltd. in the United States and in other
countries.
Sybase and 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. Sybase is an SAP company.
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 Marketplace
You can find this documentation at the following address:
http://service.sap.com/hana
Terms for Included Open
Source Software
This SAP software contains also the third party open source software
products listed below. Please note that for these third party products
the following special terms and conditions shall apply.
1. This software was developed using ANTLR.
2. gSOAP
Part of the software embedded in this product is gSOAP software.
Portions created by gSOAP are Copyright (C) 2001-2004 Robert A.
van Engelen, Genivia inc. All Rights Reserved.
THE SOFTWARE IN THIS PRODUCT WAS IN PART PROVIDED
BY GENIVIA INC AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
3. SAP License Agreement for STLport SAP License Agreement for
STLPort between SAP Aktiengesellschaft Systems, Applications,
Products in Data Processing Neurottstrasse 16 69190 Walldorf,
Germany (hereinafter: SAP) and you (hereinafter: Customer)
a) Subject Matter of the Agreement
A) SAP grants Customer a non-exclusive, non-transferrable, royalty-
free license to use the STLport.org C++ library (STLport) and its
documentation without fee.
B) By downloading, using, or copying STLport or any portion thereof
Customer agrees to abide by the intellectual property laws, and to all
of the terms and conditions of this Agreement.
C) The Customer may distribute binaries compiled with STLport
(whether original or modified) without any royalties or restrictions.
D) Customer shall maintain the following copyright and permissions
notices on STLport sources and its documentation unchanged:
Copyright 2001 SAP AG
E) The Customer may distribute original or modified STLport sources,
provided that:
o The conditions indicated in the above permissions notice are met;
o The following copyright notices are retained when present, and
conditions provided in accompanying permission notices are met:
Coypright 1994 Hewlett-Packard
Company
Copyright 1996,97 Silicon Graphics
Computer Systems Inc.
Copyright 1997 Moscow Center for
SPARC Technology.
Copyright 1999,2000 Boris Fomitchev
Copyright 2001 SAP AG
Permission to use, copy, modify, distribute and sell this software and
its documentation for any purposes is hereby granted without fee,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation. Hewlett-Packard Company makes no
representations about the suitability of this software for any purpose.
It is provided as is without express or implied warranty.
Permission to use, copy, modify, distribute and sell this software and
its documentation for any purpose is hereby granted without fee,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation. Silicon Graphics makes no representations
about the suitability of this software for any purpose. It is provided as
is without express or implied warranty.
Permission to use, copy, modify, distribute and sell this software and
its documentation for any purposes is hereby granted without fee,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation. Moscow Center for SPARC makes no
representations about the suitability of this software for any purpose. It
is provided as is without express or implied warranty.
Boris Fomitchev makes no representations about the suitability of this
software for any purpose. This material is provided "as is", with
absolutely no warranty expressed or implied.
Any use is at your own risk. Permission to use or copy this software
for any purpose is hereby granted without fee, provided the above
notices are retained on all copies.
Permission to modify the code and to distribute modified code is
granted, provided the above notices are retained, and a notice that the
code was modified is included with the above copyright notice.
Permission to use, copy, modify, distribute and sell this software and
its documentation for any purposes is hereby granted without fee,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation. SAP makes no representations about the
suitability of this software for any purpose. It is provided with a
limited warranty and liability as set forth in the License Agreement
distributed with this copy.
SAP offers this liability and warranty obligations only towards its
customers and only referring to its modifications.
b) Support and Maintenance SAP does not provide software
maintenance for the STLport. Software maintenance of the STLport
therefore shall be not included.
All other services shall be charged according to the rates for services
quoted in the SAP List of Prices and Conditions and shall be subject to
a separate contract.
c) Exclusion of warranty
As the STLport is transferred to the Customer on a loan basis and free
of charge, SAP cannot guarantee that the STLport is error-free,
without material defects or suitable for a specific application under
third-party rights. Technical data, sales brochures, advertising text and
quality descriptions produced by SAP do not indicate any assurance of
particular attributes.
d) Limited Liability
A) Irrespective of the legal reasons, SAP shall only be liable for
damage, including unauthorized operation, if this (i) can be
compensated under the Product Liability Act or (ii) if caused due to
gross negligence or intent by SAP or (iii) if based on the failure of a
guaranteed attribute.
B) If SAP is liable for gross negligence or intent caused by employees
who are neither agents or managerial employees of SAP, the total
liability for such damage and a maximum limit on the scope of any
such damage shall depend on the extent to which its occurrence ought
to have anticipated by SAP when concluding the contract, due to the
circumstances known to it at that point in time representing a typical
transfer of the software.
C) In the case of Art. 4.2 above, SAP shall not be liable for indirect
damage, consequential damage caused by a defect or lost profit.
D) SAP and the Customer agree that the typical foreseeable extent of
damage shall under no circumstances exceed EUR 5,000.
E) The Customer shall take adequate measures for the protection of
data and programs, in particular by making backup copies at the
minimum intervals recommended by SAP. SAP shall not be liable for
the loss of data and its recovery, notwithstanding the other limitations
of the present Art. 4 if this loss could have been avoided by observing
this obligation.
F) The exclusion or the limitation of claims in accordance with the
present Art. 4 includes claims against employees or agents of SAP.
4. Adobe Document Services Adobe, the Adobe logo, Acrobat,
PostScript, and Reader are either registered trademarks or trademarks
of Adobe Systems Incorporated in the United States and / or other
countries. For information on Third Party software delivered with
Adobe document services and Adobe LiveCycle Designer, see SAP
Note 854621
SAP In-Memory Database SQL Reference Manual
2
Table of Contents
About this Guide ........................................................................................................................................... 7
SAP HANA Guides .......................................................................................................................................... 7
Notation ........................................................................................................................................................ 8
Functions ....................................................................................................................................................... 9
Data type conversion functions ................................................................................................................ 9
CAST Function ....................................................................................................................................... 9
TO_BIGINT Function .............................................................................................................................. 9
TO_BINARY Function ............................................................................................................................. 9
TO_BLOB Function .............................................................................................................................. 10
TO_CHAR Function .............................................................................................................................. 10
TO_CLOB Function .............................................................................................................................. 10
TO_DATE Function .............................................................................................................................. 10
TO_DATS Function .............................................................................................................................. 11
TO_DECIMAL Function ........................................................................................................................ 11
TO_DOUBLE Function ......................................................................................................................... 11
TO_INT Function ................................................................................................................................. 12
TO_INTEGER Function ......................................................................................................................... 12
TO_NCHAR Function ........................................................................................................................... 12
TO_NCLOB Function ............................................................................................................................ 12
TO_NUMBER Function ........................................................................................................................ 13
TO_REAL Function ............................................................................................................................... 13
TO_SMALLINT Function ...................................................................................................................... 13
TO_TINYINT Function .......................................................................................................................... 13
TO_TIME Function ............................................................................................................................... 14
TO_TIMESTAMP Function ................................................................................................................... 14
DateTime Functions ................................................................................................................................ 15
ADD_DAYS Function ............................................................................................................................ 15
ADD_MONTHS Function ..................................................................................................................... 15
SAP In-Memory Database SQL Reference Manual
3
ADD_YEARS Function .......................................................................................................................... 15
ADD_SECONDS Function ..................................................................................................................... 15
DAYS_BETWEEN Function ................................................................................................................... 16
DAYNAME Function ............................................................................................................................ 16
DAYOFMONTH Function ..................................................................................................................... 16
DAYOFYEAR Function .......................................................................................................................... 16
EXTRACT Function ............................................................................................................................... 17
HOUR Function .................................................................................................................................... 17
LAST_DAY Function ............................................................................................................................. 17
MINUTE Function ................................................................................................................................ 17
MONTH Function ................................................................................................................................ 18
MONTHNAME Function ...................................................................................................................... 18
NEXT_DAY Function ............................................................................................................................ 18
NOW Function ..................................................................................................................................... 18
SECOND Function ................................................................................................................................ 19
SECONDS_BETWEEN Function ............................................................................................................ 19
WEEK Function .................................................................................................................................... 19
WEEKDAY Function ............................................................................................................................. 20
YEAR Function ..................................................................................................................................... 20
Miscellaneous Functions ......................................................................................................................... 21
COALESCE Function ............................................................................................................................. 21
IFNULL Function .................................................................................................................................. 21
NULLIF Function .................................................................................................................................. 21
CURRENT_CONNECTION Function ...................................................................................................... 22
CURRENT_SCHEMA Function .............................................................................................................. 22
CURRENT_USER Function ................................................................................................................... 22
DATABASE Function ............................................................................................................................ 23
USER Function ..................................................................................................................................... 23
GROUPING_ID Function ...................................................................................................................... 23
SESSION_CONTEXT Function ............................................................................................................... 24
SYSUUID Function ............................................................................................................................... 25
Number Functions................................................................................................................................... 26
SAP In-Memory Database SQL Reference Manual
4
ABS Function ....................................................................................................................................... 26
ACOS Function..................................................................................................................................... 26
ASIN Function ...................................................................................................................................... 26
ATAN Function .................................................................................................................................... 26
ATAN2 Function .................................................................................................................................. 27
BITAND Function ................................................................................................................................. 27
CEIL / CEILING Function ...................................................................................................................... 27
COS Function ....................................................................................................................................... 28
COSH Function .................................................................................................................................... 28
COT Function ....................................................................................................................................... 28
EXP Function ....................................................................................................................................... 28
FLOOR Function .................................................................................................................................. 29
GREATEST Function ............................................................................................................................. 29
LEAST Function .................................................................................................................................... 29
LN Function ......................................................................................................................................... 30
LOG Function ....................................................................................................................................... 30
MOD Function ..................................................................................................................................... 30
POWER Function ................................................................................................................................. 31
ROUND Function ................................................................................................................................. 31
SIGN Function...................................................................................................................................... 31
SIN Function ........................................................................................................................................ 31
SINH Function...................................................................................................................................... 32
SQRT Function ..................................................................................................................................... 32
TAN Function ....................................................................................................................................... 32
TANH Function .................................................................................................................................... 32
String Functions ...................................................................................................................................... 34
ASCII Function ..................................................................................................................................... 34
CONCAT Function ................................................................................................................................ 34
LEFT Function ...................................................................................................................................... 34
LCASE Function.................................................................................................................................... 34
LENGTH Function ................................................................................................................................ 35
LOCATE Function ................................................................................................................................. 35
SAP In-Memory Database SQL Reference Manual
5
LOWER Function ................................................................................................................................. 35
LPAD Function ..................................................................................................................................... 36
LTRIM Function ................................................................................................................................... 36
REPLACE Function ............................................................................................................................... 36
RIGHT Function ................................................................................................................................... 37
RPAD Function .................................................................................................................................... 37
RTRIM Function ................................................................................................................................... 37
SUBSTRING Function ........................................................................................................................... 37
SUBSTR_AFTER Function ..................................................................................................................... 38
SUBSTR_BEFORE Function .................................................................................................................. 38
TRIM Function ..................................................................................................................................... 38
UCASE Function ................................................................................................................................... 39
UNICODE Function .............................................................................................................................. 39
UPPER Function ................................................................................................................................... 39
SQL Statements ........................................................................................................................................... 41
ALTER TABLE........................................................................................................................................ 41
ALTER USER ......................................................................................................................................... 44
CREATE CALCULATION SCENARIO ....................................................................................................... 45
CREATE COLUMN VIEW....................................................................................................................... 47
CREATE INDEX ..................................................................................................................................... 52
CREATE ROLE ....................................................................................................................................... 53
CREATE SCHEMA ................................................................................................................................. 54
CREATE SEQUENCE.............................................................................................................................. 55
CREATE SYNONYM .............................................................................................................................. 57
CREATE TABLE ..................................................................................................................................... 58
CREATE TYPE ....................................................................................................................................... 62
CREATE USER ....................................................................................................................................... 63
CREATE VIEW ...................................................................................................................................... 64
DELETE ................................................................................................................................................. 65
DROP CALCULATION SCENARIO .......................................................................................................... 66
DROP INDEX ........................................................................................................................................ 67
DROP ROLE .......................................................................................................................................... 68
SAP In-Memory Database SQL Reference Manual
6
DROP SCHEMA .................................................................................................................................... 69
DROP SEQUENCE ................................................................................................................................. 70
DROP SYNONYM ................................................................................................................................. 71
DROP TABLE ........................................................................................................................................ 72
DROP TYPE .......................................................................................................................................... 73
DROP TABLE ........................................................................................................................................ 74
DROP USER .......................................................................................................................................... 75
DROP VIEW.......................................................................................................................................... 76
RENAME INDEX ................................................................................................................................... 77
GRANT ................................................................................................................................................. 78
INSERT ................................................................................................................................................. 81
LOAD ................................................................................................................................................... 82
MERGE DELTA ..................................................................................................................................... 83
RENAME COLUMN .............................................................................................................................. 84
RENAME INDEX ................................................................................................................................... 85
RENAME TABLE ................................................................................................................................... 86
REVOKE ............................................................................................................................................... 87
SELECT ................................................................................................................................................. 88
SET TRANSACTION .............................................................................................................................. 93
UNLOAD .............................................................................................................................................. 94
UPDATE ............................................................................................................................................... 95
UPSERT / REPLACE .............................................................................................................................. 96
SAP In-Memory Database SQL Reference Manual
7
About this Guide This document contains a list of functions and SQL statements supported by the SAP In-Memory
Database as part of SAP In-Memory Appliance (SAP HANA) 1.0 SPS 02.
SAP HANA Guides For 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 Knowledge
Center on SAP Service
Marketplace
https://service.sap.com/hana
SAP HANA 1.0 Master Guide
SAP HANA 1.0 Installation Guide
SAP HANA
Administration &
Security
SAP HANA Knowledge
Center on SAP Help
Portal
http://help.sap.com/hana
SAP HANA 1.0 Technical Operations Manual
SAP HANA 1.0 Security Guide
SAP In-Memory Database SQL Reference Manual
8
Notation This 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. Table 1 presents meta symbols of BNF and descriptions.
The Meta symbols of BNF
Meta-symbol Description
< > Angle brackets are placeholders for syntactical units explained in this
document.
::= Production rule has a left hand side (LHS) and a right hand side (RHS)
separated by the meta-symbol "::=". The symbol on the left-hand side of
the ::= is defined by the right hand side.
[ ] Enclose optional element. Element between [ ] can be specified or omitted.
{ } Groups items. Elements between {} must be specified.
| Possible option elements in the formula. Elements follow by | can be chosen differently than front of | parts
... Displays several repeated item in the formula
SAP In-Memory Database SQL Reference Manual
9
Functions Functions 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
to test whether they can be converted.
CAST Function Syntax:
CAST(expr 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.
BIGINT | BINARY | BLOB | CHAR | CLOB | DATE | DECIMAL | DOUBLE | NCHAR |
NCLOB | REAL | TIME | TIMESTAMP
Example: SELECT TOP 1 CAST(7 AS CHAR(10)) "cast" FROM users;
Retrieves: cast 7
TO_BIGINT Function Syntax:
TO_BIGINT(expr)
Description:
Converts the expr of a data type into a value of bigint data type. Example:
SELECT TOP 1 TO_BIGINT('10') "to bigint" FROM users;
Retrieves: to bigint
10
TO_BINARY Function Syntax:
TO_BINARY(expr)
Description:
Converts the expr of a data type into a value of binary string type.
SAP In-Memory Database SQL Reference Manual
10
Example:
SELECT TOP 1 TO_BINARY('abcde') "to binary" FROM users;
Retrieves: to binary
6162636465
TO_BLOB Function Syntax:
TO_BLOB(expr)
Description:
Converts the expr of a data type into a value of blob type. expr must be a binary string. Example:
SELECT TOP 1 TO_BLOB(TO_BINARY('abcde')) "to blob" FROM users; Retrieves: to blob
abcde
TO_CHAR Function Syntax:
TO_CHAR(expr [,format]) Description:
Converts the expr of a data type into a value of character data type. Example:
SELECT TOP 1 TO_CHAR(TO_DATE('2009-12-31'), 'YYYY/MM/DD') "to char" FROM users;
Retrieves: to char
2009/12/31
TO_CLOB Function Syntax:
TO_CLOB(expr) Description:
Converts the expr of a data type into a value of CLOB data type. Example:
SELECT TOP 1 TO_CLOB ('TO_CLOB converts an expression into a value of CLOB data type') "to clob" FROM users;
Retrieves: to clob
TO_CLOB converts an expression into a value of CLOB data type
TO_DATE Function Syntax:
TO_DATE(expr [, format]) Description:
SAP In-Memory Database SQL Reference Manual
11
Converts the expr of a data type into a value of DATE data type.
Example:
SELECT TOP 1 TO_DATE('2010-01-12', 'YYYY-MM-DD') "to date" FROM users; Retrieves: to date
2010-01-12
TO_DATS Function Syntax:
TO_DATS(expr) Description:
Converts the expr of a data type into a value of ABAP DATE data type.
Example:
SELECT TOP 1 TO_DATS('2010-01-12') "abap date" FROM users;
Retrieves: abap date
20100112
TO_DECIMAL Function Syntax:
TO_DECIMAL(expr[, precision, scale]) Description:
Converts the expr of a data type into a value of DECIMAL(precision, scale) data type. Example:
SELECT TOP 1 TO_DECIMAL(7654321.89, 9, 2) "to decimal" FROM users;
Retrieves: to decimal
7654321.89
TO_DOUBLE Function Syntax:
TO_DOUBLE(expr) Description:
Converts the expr of a data type into a value of DOUBLE (double precision) data type.
Example:
SELECT TOP 1 3*TO_DOUBLE ('15.12') "to double" FROM users; Retrieves: to double
45.36
SAP In-Memory Database SQL Reference Manual
12
TO_INT Function Syntax:
TO_INT(expr) Description:
Converts the expr of a data type into a value of INTEGER data type. Example:
SELECT TOP 1 TO_INT('10') "to int" FROM users;
Retrieves: to int
10
TO_INTEGER Function Syntax:
TO_INTEGER(expr) Description:
Converts the expr of a data type into a value of INTEGER data type. Example:
SELECT TOP 1 TO_INTEGER('10') "to int" FROM users; Retrieves: to int
10
TO_NCHAR Function Syntax:
TO_NCHAR(expr [,format]) Description:
Converts the expr of a data type into a value of national character data type.
If format is omitted, it converts to the corresponding format using the date format model. Example:
SELECT TOP 1 TO_NCHAR(TO_DATE('2009/12/31'), 'YY-MM-DD') "to nchar" FROM users;
Retrieves: to nchar
09-12-31
TO_NCLOB Function Syntax:
TO_NCLOB(expr) Description:
Converts the expr of a data type into a value of NCLOB data type. Example:
SELECT TOP 1 TO_NCLOB ('TO_NCLOB converts an expression into a value of NCLOB data type') "to nclob" FROM users;
SAP In-Memory Database SQL Reference Manual
13
Retrieves: to nclob
TO_NCLOB converts an expression into a value of NCLOB data type
TO_NUMBER Function Syntax:
TO_NUMBER(expr) Description:
Converts the expr of a data type into a numeric data type.
Example:
SELECT TOP 1 3.1*TO_NUMBER ('15') "to number" FROM users;
Retrieves: to number
46.5
TO_REAL Function Syntax:
TO_REAL(expr) Description:
Converts the expr of a data type into the value of REAL (single precision) data type. Example:
SELECT TOP 1 3*TO_REAL ('15.12') "to real" FROM users;
Retrieves: to real
45.36
TO_SMALLINT Function Syntax:
TO_SMALLINT(expr) Description:
Converts the expr of a data type into a value of SMALLINT data type. Example:
SELECT TOP 1 TO_SMALLINT('10') "to smallint" FROM users; Retrieves: to smallint
10
TO_TINYINT Function Syntax:
TO_TINYINT(expr) Description:
Converts the expr of a data type into a value of TINYINT data type. Example:
SELECT TOP 1 TO_TINYINT('10') "to tinyint" FROM users;
SAP In-Memory Database SQL Reference Manual
14
Retrieves: to tinyint
10
TO_TIME Function Syntax:
TO_TIME(expr [, format]) Description:
Converts the expr of a data type into a value of TIME data type. If format is omitted, it converts expr into the corresponding format using the date format model. Example:
SELECT TOP 1 TO_TIME ('08:30 AM', HH12:MI AM) "to time" FROM users;
Retrieves: to time
08:30:00
TO_TIMESTAMP Function Syntax:
TO_TIMESTAMP(expr [, format]) Description:
Converts the expr of a data type into the TIMESTAMP data type.
If format is omitted, it converts expr into the corresponding format using the date format model. Example:
SELECT TOP 1 TO_TIMESTAMP ('2010-01-11 13:30:00', 'YYYY-MM-DD HH24:MI:SS') "to timestamp" FROM users; Retrieves: to timestamp
2010-01-11 13:30:00.000
SAP In-Memory Database SQL Reference Manual
15
DateTime Functions
ADD_DAYS Function Syntax:
ADD_DAYS(d, n) Description:
Computes the date d plus n days. Example:
SELECT TOP 1 ADD_DAYS(TO_DATE('2009-12-05', 'YYYY-MM-DD'), 30) "add days" FROM users;
Retrieves: add days
2010-01-04
ADD_MONTHS Function Syntax:
ADD_MONTHS(d, n) Description:
Computes the date d plus n months. Example:
SELECT TOP 1 ADD_MONTHS(TO_DATE('2009-12-05', 'YYYY-MM-DD'), 1) "add months" FROM users;
Retrieves: add months
2010-01-05
ADD_YEARS Function Syntax:
ADD_YEARS(d, n) Description:
Computes the date d plus n years. Example:
SELECT TOP 1 ADD_YEARS(TO_DATE('2009-12-05', 'YYYY-MM-DD'), 1) "add years" FROM users;
Retrieves: add years
2010-12-05
ADD_SECONDS Function Syntax:
ADD_SECONDS(t, n) Description:
Computes the time t plus n seconds. Example:
SELECT TOP 1 ADD_SECONDS(TO_TIME('23:30:45'), 60*30) "add seconds" FROM users;
SAP In-Memory Database SQL Reference Manual
16
Retrieves: add seconds
00:00:45.000
DAYS_BETWEEN Function Syntax:
DAYS_BETWEEN (date1, date2) Description:
Computes the number of days between date1 and date2. Example:
SELECT TOP 1 DAYS_BETWEEN(TO_DATE('2009-12-05', 'YYYY-MM-DD'), TO_DATE('2010-01-05', 'YYYYMM-DD')) "days between" FROM users;
Retrieves: days between
31
DAYNAME Function Syntax:
DAYNAME(expr) Description:
Returns the name of the weekday in English. Example:
SELECT TOP 1 DAYNAME('2011-05-30') "dayname" FROM users; Retrieves: dayname
MONDAY
DAYOFMONTH Function Syntax:
DAYOFMONTH(expr) Description:
Returns the day of the month in integer. Example:
SELECT TOP 1 DAYOFMONTH ('2011-05-30') "dayofmonth" FROM users; Retrieves: dayofmonth
30
DAYOFYEAR Function Syntax:
DAYOFYEAR(expr) Description:
Returns the day of the year in integer.
SAP In-Memory Database SQL Reference Manual
17
Example:
SELECT TOP 1 DAYOFYEAR ('2011-05-30') "dayofyear" FROM users;
Retrieves: dayofyear
150
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 TOP 1 EXTRACT(YEAR FROM TO_DATE('2010-01-04', 'YYYY-MM-DD')) "extract" FROM users; Retrieves: extract
2010
HOUR Function Syntax:
HOUR(expr) Description:
Extract hour from expr. Example:
SELECT TOP 1 HOUR ('12:34:56) "hour" FROM users;
Retrieves: hour
12
LAST_DAY Function Syntax:
LAST_DAY(d) Description:
Returns the date of the last day of the month that contains d. Example:
SELECT TOP 1 LAST_DAY(TO_DATE('2010-01-04', 'YYYY-MM-DD')) "last day" FROM users; Retrieves: last day
2010-01-31
MINUTE Function Syntax:
MINUTE(expr) Description:
SAP In-Memory Database SQL Reference Manual
18
Extract minute from expr. Example:
SELECT TOP 1 MINUTE ('12:34:56) "minute" FROM users; Retrieves: minute
34
MONTH Function Syntax:
MONTH(expr) Description:
Returns the number of the month from a given date. Example:
SELECT TOP 1 MONTH ('2011-05-30) "month" FROM users; Retrieves: month
5
MONTHNAME Function Syntax:
MONTHNAME(expr) Description:
Returns the name of the month in English. Example:
SELECT TOP 1 MONTHNAME ('2011-05-30) "monthname" FROM users; Retrieves: monthname
MAY
NEXT_DAY Function Syntax:
NEXT_DAY(d) Description:
Returns the date of the next day of d. Example:
SELECT TOP 1 NEXT_DAY(TO_DATE('2009-12-31', 'YYYY-MM-DD')) "next day" FROM users; Retrieves: next day
2010-01-01
NOW Function Syntax:
SAP In-Memory Database SQL Reference Manual
19
NOW() Description:
Returns the current timestamp. Example:
SELECT TOP 1 NOW() "now" FROM users;
Retrieves: now
2010-01-01 16:34:19
SECOND Function Syntax:
SECOND(t) Description:
Returns the second number of a given time t. Example:
SELECT TOP 1 SECOND(12:34:56) "second" FROM users; Retrieves: second
56
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 TOP 1 SECONDS_BETWEEN('2009-12-05', '2010-01-05') "seconds between" FROM users; Retrieves: Seconds between
2678400
WEEK Function Syntax:
WEEK(d) Description:
Returns the week number of a given date d. Example:
SELECT TOP 1 WEEK(TO_DATE('2011-05-30', 'YYYY-MM-DD')) "week" FROM users; Retrieves: week
22
SAP In-Memory Database SQL Reference Manual
20
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 TOP 1 WEEKDAY(TO_DATE('2010-12-31', 'YYYY-MM-DD')) "week day" FROM users; Retrieves: week day
4
YEAR Function Syntax:
YEAR(d) Description:
Returns the year number of a given date d. Example:
SELECT TOP 1 YEAR(TO_DATE('2011-05-30', 'YYYY-MM-DD')) "year" FROM users; Retrieves: year
2011
SAP In-Memory Database SQL Reference Manual
21
Miscellaneous Functions
COALESCE Function Syntax :
COALESCE(expr_list) Description:
Returns the first non-NULL expression from a list. At least two expressions must be passed into the function, and all expressions must be comparable.The result will be NULL if all the arguments are NULL. Example:
SELECT * FROM tab; Retrieves: ID A B
1 100.0 80.0
2 NULL 63.0
3 NULL NULL
SELECT id, a, b, COALESCE(a, b*1.1, 50.0) "coalesce" FROM tab; Retrieves: ID A B coalesce
1 100.0 80.0 100
2 NULL 63.0 69.3
3 NULL NULL 50
IFNULL Function Syntax:
IFNULL(expr1, expr2) Description:
Returns expr1 if expr1 is not NULL and expr2 if expr1 is NULL. Example:
SELECT TOP 1 IFNULL('diff', 'same') "ifnull" FROM users;
Retrieves: ifnull
diff
NULLIF Function Syntax:
NULLIF(expr1, expr2) Description:
NULLIF compares the values of the two expressions. If the first expression equals the second expression, NULLIF returns NULL. If the first expression does not equal the second expression, or if the second expression is NULL, NULLIF returns the first expression.
SAP In-Memory Database SQL Reference Manual
22
The NULLIF function provides a short way to write some CASE expressions. Example:
SELECT TOP 1 NULLIF('diff', 'same') "nullif" FROM users; Retrieves: nullif
diff
SELECT TOP 1 NULLIF('same', 'same') "nullif" FROM users; Retrieves: nullif
NULL
CURRENT_CONNECTION Function Syntax:
CURRENT_CONNECTION
Description:
Returns the current connection id. Example:
SELECT TOP 1 CURRENT_CONNECTION "current connection" FROM users; Retrieves: current connection
2
CURRENT_SCHEMA Function Syntax:
CURRENT_SCHEMA
Description:
Returns the current schema name in the string. Example:
SELECT TOP 1 CURRENT_SCHEMA "current schema" FROM users; Retrieves: current schema
SYSTEM
CURRENT_USER Function Syntax:
CURRENT_USER
Description:
Returns the current user name in the string. Example:
SELECT TOP 1 CURRENT_USER "current user" FROM users; Retrieves: current user
SAP In-Memory Database SQL Reference Manual
23
SYSTEM
DATABASE Function Syntax:
DATABASE()
Description:
Returns the current schema name. Example:
SELECT TOP 1 DATABASE() FROM users; Retrieves: CURRENT_SCHEMA
SYSTEM
USER Function Syntax:
USER()
Description:
Returns the current user name. Example:
SELECT TOP 1 USER() "current user" FROM users;
Retrieves: current user
SYSTEM
GROUPING_ID Function Syntax:
GROUPING_ID(column_name_list)
Description:
GROUPING_ID function can be used with GROUPING SETS to return multiple levels of aggregations in a single result set. GROUPING_ID returns an integer value to identify which grouping set each row belongs to. Each column in GROUPING_ID must be an element of the GROUPING SETS.
GROUPING_ID is assigned by converting the bit vector generated from GROUPING SETS to a decimal
number by treating the bit vector as a binary number. When a bit vector is composed, 0 is assigned to each column specified in the GROUPING SETS and 1 otherwise in the order it appears in the GROUPING SETS. By treating the bit vector as a binary number, this function returns an integer value as the output. Example:
SELECT customer, year, product, SUM(sales), GROUPING_ID(customer, year, product) FROM guided_navi_tab GROUP BY GROUPING SETS ( (customer, year, product),
(customer, year), (customer, product),
SAP In-Memory Database SQL Reference Manual
24
(year, product), (customer), (year), (product)
); Retrieves:
CUSTOMER YEAR PRODUCT SUM(SALES) GROUPING_ID(CUSTOMER,YEAR,PRODUCT)
1 C1 2009 P1 100 0
2 C1 2010 P1 50 0
3 C2 2009 P1 200 0
4 C2 2010 P1 100 0
5 C1 2009 P2 200 0
6 C1 2010 P2 150 0
7 C2 2009 P2 300 0
8 C2 2010 P2 150 0
9 C1 2009 ? 300 1
10 C1 2010 ? 200 1
11 C2 2009 ? 500 1
12 C2 2010 ? 250 1
13 C1 ? P1 150 2
14 C2 ? P1 300 2
15 C1 ? P2 350 2
16 C2 ? P2 450 2
17 ? 2009 P1 300 4
18 ? 2010 P1 150 4
19 ? 2009 P2 500 4
20 ? 2010 P2 300 4
21 C1 ? ? 500 3
22 C2 ? ? 750 3
23 ? 2009 ? 800 5
24 ? 2010 ? 450 5
25 ? ? P1 450 6
26 ? ? P2 800 6
SESSION_CONTEXT Function Syntax:
SESSION_CONTEXT(session_parameter)
Description:
SAP In-Memory Database SQL Reference Manual
25
Returns the value of session_parameter assigned to the current user or the value of a personal
setting. The parameter can be set when the session is created. Currently available read only session
variables are locale, locale_sap, client, conn_id, applicationname, clientuser, clienthostname.
Example:
SELECT TOP 1 SESSION_CONTEXT(conn_Id) session context FROM users; Retrieves: session context
4
SYSUUID Function Syntax:
SYSUUID
Description:
Returns the SYSUUID. Example:
SELECT TOP 1 SYSUUID FROM users; Retrieves: SYSUUID
4DE3CD576C79511BE10000000A3C2220
SAP In-Memory Database SQL Reference Manual
26
Number Functions Number functions take numeric values or strings with numeric characters as inputs and returns numeric values. When strings with numeric characters are given as inputs, implicit conversion from string 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 TOP 1 ABS(-1) "absolute" FROM users;
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 TOP 1 ACOS(0.5) "acos" FROM users; Retrieves: acos
1.0471975511965979
ASIN Function Syntax:
ASIN ( n )
Description:
Returns the arc-sine, in radians, of a number n between -1 and 1. Example:
SELECT TOP 1 ASIN(0.5) "asin" FROM users; Retrieves: asin
0.5235987755982989
ATAN Function
SAP In-Memory Database SQL Reference Manual
27
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:
SELECT TOP 1 ATAN(0.5) "atan" FROM users;
Retrieves: atan
0.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 result as ATAN(n/m). Example:
SELECT TOP 1 ATAN2(1.0, 2.0) "atan2" FROM users;
Retrieves: atan2
0.4636476090008061
BITAND Function Syntax:
BITAND (expr1, expr2) Description:
Calculates an AND operation on the bits of expr1 and expr2. Both expr1 and expr2 must be non-negative
integers. The BITAND function returns an integer. Example:
SELECT TOP 1 BITAND(255, 123) "bitand" FROM users; Retrieves: bitand
123
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 is known as rounding up.
SAP In-Memory Database SQL Reference Manual
28
Example:
SELECT TOP 1 CEIL(14.5) "ceiling" FROM users;
Retrieves: ceiling
15
COS Function Syntax:
COS ( n ) Description:
Returns the cosine of the angle in radians given by its argument n. Example:
SELECT TOP 1 COS(0.0) "cos" FROM users;
Retrieves: cos
1.0
COSH Function Syntax:
COSH ( n ) Description:
Computes the hyperbolic cosine of n. Example:
SELECT TOP 1 COSH(0.5) "cosh" FROM users;
Retrieves: cosh
1.1276259652063807
COT Function Syntax:
COT ( n ) Description:
Computes the cotangent of n. Example:
SELECT TOP 1 COT(40) "cot" FROM users;
Retrieves: Cot
1.5423510453569202
EXP Function Syntax:
EXP ( n )
SAP In-Memory Database SQL Reference Manual
29
Description:
Returns the result of the base of natural logarithms e raised to the power of the given argument n. Example:
SELECT TOP 1 EXP(1.0) "exp" FROM users; Retrieves: exp
2.718281828459045
FLOOR Function Syntax:
FLOOR ( n ) Description:
Returns the largest integer not greater than the given number n. Example:
SELECT TOP 1 FLOOR(14.5) "floor" FROM users; Retrieves: floor
14
GREATEST Function Syntax:
GREATEST ( n1 [, n2]...) Description:
Returns the greatest value among the arguments: n1, n2, ... Example:
SELECT TOP 1 GREATEST ('aa', 'ab', 'ba', 'bb') "greatest" FROM users; Retrieves: greatest
bb
LEAST Function Syntax:
LEAST ( n1 [, n2]...) Description:
Returns the least value among the arguments: n1, n2... Example:
SELECT TOP 1 LEAST('aa', 'ab', 'ba', 'bb') "least" FROM users;
Retrieves: least
aa
SAP In-Memory Database SQL Reference Manual
30
LN Function Syntax:
LN ( n ) Description:
Returns the natural logarithm of a given value n, where n is greater than 0.
Example:
SELECT TOP 1 LN(9) "ln" FROM users;
Retrieves:
ln
2.1972245773362196
LOG Function Syntax :
LOG(m, n) Description:
Returns the natural logarithm of n base m. The base m must be a positive value other than 1 and n must be
any positive value. Example:
SELECT TOP 1 LOG(10, 2) "log" FROM users;
Retrieves: log
0.30102999566398114
MOD Function Syntax:
MOD(n, d) Description:
Returns the remainder of a number n divided by a divisor d.
When n is negative this function acts differently to the standard computational modulo operation.
The following explains example of what MOD functions returns as the result. If d is zero, then this function returns n. If n is greater than 0 and n is less than d, then this function returns n.
If n is less than 0 and n is greater than d, then this function returns n. Otherwise, this function calculates the remainder of the absolute value of n divided by the
absolute value of d to calculate the absolute value of the remainder. If m is less than 0, then the returned remainder from MOD is a negative number, and if m is greater than 0, then the returned remainder from MOD is a positive number.
Example:
SELECT TOP 1 MOD(15, 4) "modulus" FROM users;
Retrieves: modulus
SAP In-Memory Database SQL Reference Manual
31
3
POWER Function Syntax:
POWER(b, e)
Description:
Calculates the base number b raised to the power of an exponent e. Example:
SELECT TOP 1 POWER(2, 10) "power" FROM users; Retrieves: power
1024.0
ROUND Function Syntax:
ROUND(n [, pos ]) Description:
Rounds n to the specified pos amount of places after the decimal point. Example:
SELECT TOP 1 ROUND(16.16, 1) "round" FROM users;
Retrieves: round
16.2
SELECT TOP 1 ROUND(16.56) "round" FROM users; Retrieves: round
17
SIGN Function Syntax:
SIGN ( n ) Description:
Returns the sign (positive or negative) of the given number.n. Returns 1 if n is a positive value, -1 if n is a negative value, and 0 if n is equal to zero. Example:
SELECT TOP 1 SIGN(-15) "sign" FROM users;
Retrieves: sign
-1
SIN Function Syntax:
SAP In-Memory Database SQL Reference Manual
32
SIN ( n ) Description:
Returns the sine of n, where the argument is an angle expressed in radians. Example:
SELECT TOP 1 SIN( 3.141592653589793/2) "sine" FROM users; Retrieves: sine
1.0
SINH Function Syntax:
SINH (n) Description:
Returns the hyperbolic sine of n, where the argument is an angle expressed in radians. Example:
SELECT TOP 1 SINH(0.0) "sinh" FROM users;
Retrieves: sinh
0.0
SQRT Function Syntax:
SQRT ( n ) Description:
Returns the square root of a number n. Example:
SELECT TOP 1 SQRT(2) "sqrt" FROM users; Retrieves: sqrt
1.4142135623730951
TAN Function Syntax:
TAN ( n ) Description:
Returns the tangent of a number n. Example:
SELECT TOP 1 TAN(0.0) "tan" FROM users; Retrieves: tan
0.0
TANH Function
SAP In-Memory Database SQL Reference Manual
33
Syntax:
TANH ( n ) Description:
Returns the hyperbolic tangent of a number n. Example:
SELECT TOP 1 TANH(1.0) "tanh" FROM users; Retrieves: tanh
0.7615941559557649
SAP In-Memory Database SQL Reference Manual
34
String Functions
ASCII Function Syntax:
ASCII(c) Description:
Returns the integer ASCII value of the first byte in a string c. Example:
SELECT TOP 1 ASCII('Ant') "ascii" FROM users; Retrieves: ascii
65
CONCAT Function Syntax :
CONCAT(str1, str2) Description:
Returns a combined string which consists of str1 followed by str2. The concatenation operator (||) is
identical to this function. Example:
SELECT TOP 1 CONCAT('C', 'at') "concat" FROM users; Retrieves: concat
Cat
LEFT Function Syntax :
LEFT(str, n) Description:
Returns n characters from the beginning of a string str. Example:
SELECT TOP 1 LEFT('Hello', 3) "left" FROM users;
Retrieves: left
Hel
LCASE Function Syntax:
LCASE(s) Description:
Converts all characters in a string s to lowercase. The LCASE function is identical to the LOWER function.
SAP In-Memory Database SQL Reference Manual
35
Example:
SELECT TOP 1 LCASE('Test') "lcase FROM users; Retrieves: lcase
test
LENGTH Function Syntax:
LENGTH(s) Description:
Returns the number of characters in the specified string.s. For CLOB or NCLOB, it returns the length in bytes. Example:
SELECT TOP 1 LENGTH('length in char') "length" FROM users; Retrieves: length
14
LOCATE Function Syntax:
LOCATE(haystack, needle) Description:
Returns the position of sub-string needle within string haystack. Returns 0 if needle is not found from haystack. Example:
SELECT TOP 1 LOCATE('length in char', char) "locate" FROM users; Retrieves: Locate
11
LOWER Function Syntax
LOWER(s) Description
Converts all characters in a string s to lowercase. The LOWER function is identical to the LCASE function. Example
SELECT TOP 1 LOWER('Ant') "lower" FROM users;
Retrieves: lower
ant
SAP In-Memory Database SQL Reference Manual
36
LPAD Function Syntax:
LPAD(target, n[, pattern]) Description:
Pad the left side of a target string with spaces or pattern to make the target n characters in length.. Example:
SELECT TOP 1 LPAD('end', 15, '12345') "lpad" FROM users;
Retrieves: lpad
123451234512end
LTRIM Function Syntax:
LTRIM(target [, remove_set]) Description:
Removes from the leftmost of target all of the characters that appear in remove_set until reaching a character not in remove_set and then returns the result. If remove_set is not specified, a single blank
space is used. Please note that remove_set is treated as a set of characters and not a search string. Example:
SELECT TOP 1 LTRIM('babababAabend','ab') "ltrim" FROM users;
Retrieves: ltrim
Aabend
REPLACE Function Syntax:
REPLACE (original_string, search_string, replace_string) Description:
Searches in original_string for all occurrences of search_string and replaces them with replace_string. If original_string is an empty string, then the result will also be an empty string.
If two overlapping substrings match the search_string in the original_string, then only the first occurrence will be replaced with the replace_string.
If original_string does not contain any occurrence of search_string, then the function returns the
original_string unchanged. If original_string, search_string, or replace_string is NULL, then the function returns NULL.
Example:
SELECT TOP 1 REPLACE ('DOWNGRADE DOWNWARD','DOWN', 'UP') "replace" FROM users;
Retrieves: replace
UPGRADE UPWARD
SAP In-Memory Database SQL Reference Manual
37
RIGHT Function Syntax:
RIGHT(target, n) Description:
Returns the rightmost n characters of a string target. Example:
SELECT TOP 1 RIGHT('HI0123456789', 3) "right" FROM users; Retrieves: right
789
RPAD Function Syntax:
RPAD (target, n[, pattern]) Description:
Pad the right side of a target string with spaces or pattern to make the target n characters in length. Example:
SELECT TOP 1 RPAD('end', 15, '12345') "right padded" FROM users; Retrieves: right padded
end123451234512
RTRIM Function Syntax:
RTRIM (target [,remove_set ]) Description:
Removes from the rightmost of target all of the characters that appear in remove_set until reaching a character not in remove_set and then returns the result. If remove_set is not specified, a single blank
space is used. Please note that remove_set is treated as a set of characters and not a search string.
Example:
SELECT TOP 1 RTRIM ('endabAabbabab','ab') "rtrim" FROM users;
Retrieves: rtrim
endabA
SUBSTRING Function Syntax:
SUBSTRING (target, start_position [, string_length]) Description:
Returns a substring of a string target starting from the start_position of the string. SUBSTRING can either return the remaining part of a string from the start_position or, optionally, a number of characters set by the string_length parameter.
SAP In-Memory Database SQL Reference Manual
38
If start_position is less than 0, then it is considered as 1. If string_length is less than 1, then an empty string is returned as the result. Example:
SELECT TOP 1 SUBSTRING('1234567890',4,2) "substring" FROM users;
Retrieves: substring
45
SUBSTR_AFTER Function Syntax:
SUBSTR_AFTER(target, pattern) Description:
Returns a substring of the target string that follows the first occurrence of the pattern argument in the target string. If target does not contain a substring which is the same as pattern, then an empty string is
returned.
If pattern is an empty string, then target is returned. If target or pattern is NULL, then NULL is returned. Example:
SELECT TOP 1 SUBSTR_AFTER('Hello My Friend','My ') "substr after" FROM users;
Retrieves: substr after
Friend
SUBSTR_BEFORE Function Syntax:
SUBSTR_BEFORE(target, pattern) Description:
Returns a substring of the target string before the first occurrence of the pattern argument in the target string. If target does not contain a substring which is the same as pattern, then an empty string is
returned. If pattern is an empty string, then target is returned. If target or pattern is NULL, then NULL is returned. Example:
SELECT TOP 1 SUBSTR_BEFORE('Hello My Friend','My') "substr before" FROM users; Retrieves: substr before
Hello
TRIM Function Syntax:
TRIM ([[LEADING | TRAILING | BOTH] trim_char FROM] target_string ) Description:
Returns a string after trimming a trim_char from trim_source string. The trimming operation is carried
out either from the start (LEADING), end (TRAILING) or both(BOTH) ends of the target_string.
SAP In-Memory Database SQL Reference Manual
39
If either target_string or trim_char is a null value, then a NULL is returned. If no option is specified, it removes both the leading and trailing substring trim_char from
target_string string. If trim_char is not specified, then a single blank space will be used.
Example:
SELECT TOP 1 TRIM (a FROM aaa123456789aa) "trim both" FROM users; Retrieves: trim both
123456789
SELECT TOP 1 TRIM (LEADING a FROM aaa123456789aa) "trim leading" FROM users; Retrieves: trim leading
123456789aa
UCASE Function Syntax :
UCASE(target) Description:
Converts all characters in the target string to uppercase Example:
SELECT TOP 1 UCASE('Ant') "ucase" FROM users;
Retrieves: ucase
ANT
UNICODE Function Syntax :
UNICODE(c) Description:
Returns an integer containing the Unicode code point of the first character in the string, or NULL if the first character is not a valid encoding. Example:
SELECT TOP 1 UNICODE('#') "unicode" FROM users;
Retrieves: unicode
54620
UPPER Function Syntax:
UPPER(target) Description:
Converts all characters in the target string to uppercase. Example:
SAP In-Memory Database SQL Reference Manual
40
SELECT TOP 1 UPPER('Ant') "uppercase" FROM users; Retrieves: uppercase
ANT
SAP In-Memory Database SQL Reference Manual
41
SQL Statements This chapter describes the SQL statements that are supported by the HDB.
ALTER TABLE
SQL Syntax: ALTER TABLE table_name { | < drop_column_clause> | | < drop_primary_key_clause> | < preload_clause> |
| < move_clause> } [WITH PARAMETERS ( parameter_key_value, ... ) ]
Parameters:
add_column_clause::= ADD ( column_definition, ... )
drop_column_clause ::=DROP ( column_name, ... )
add_primary_key_clause ::= ADD [CONSTRAINT constraint_name] PRIMARY KEY ( column_name, ... )
CONSTRAINT
Specifies the name of a constraint. PRIMARY KEY A primary key constraint is a combination of a NOT NULL constraint and a UNIQUE constraint. It prohibits multiple rows from having the same value in the same column.
drop_primary_key_clause ::= DROP PRIMARY KEY
preload_clause ::= PRELOAD ALL | PRELOAD ( column_name ) | PRELOAD NONE
PRELOAD sets/removes the preload flag of the given tables or columns. As a consequence,
these tables are automatically loaded into memory after an index server start. The current status of the preload flag is visible in the system table TABLES, column PRELOAD, possible values ('FULL', 'PARTIALLY', 'NO' ) and in system table TABLE_COLUMNS, column PRELOAD, possible values ('TRUE', 'FALSE').
table_conversion_clause ::= [ MODIFY TYPE ] [ ROW | COLUMN ] [ THREADS int_constr ] [ BATCH
batch_size ] MODIFY TYPE ROW | COLUMN This command is used to convert the table storage from ROW to COLUMN or from COLUMN to ROW.
THREADS int_const Specifies how many threads should be used in parallel for table conversion. Default: The default value is TABLE_CONVERSION_PARALLELISM that is, the number of CPU cores
specified in the indexserver.ini file. BATCH int_const
SAP In-Memory Database SQL Reference Manual
42
Specifies the number of rows inserted in batch. Insert into column table will be immediately committed after every int_const records insertion, which may reduce memory consumption. BATCH option can be used only when the table is converted from ROW to COLUMN. The default value is 2147483647.
A new table with a different storage type can be created from an existing table by copying the existing table's columns and data. This command is used to convert the table storage from ROW to COLUMN or from COLUMN to ROW. If the source table was in ROW storage, then the created table will be in COLUMN storage.
move_clause ::= MOVE TO LOCATION
MOVE TO LOCATION A column store table can be moved to the specified location in a distributed environment.
When a table is in row store need to be moved, convert the row table to a column table first and then move to a new location using this command.
WITH PARAMETERS ( , ... ) Column store-specific options can be passed in using the "WITH PARAMETERS" clause.
Keys and single values can be any string constant Duplicate keys are allowed Keys are automatically mapped into their uppercase representation
parameter_key_value ::=
= |
= | = (, ... )
Current parameters
AUTO_MERGE = 'ON' | 'OFF' Default=ON Automatic delta merge triggered by memwatcher.
CONCAT_ATTRIBUTE = (new_att, exist_att_list) Creating concatenated attributes:
new_att:= string_literal exist_att_list = 'string_literal' | exist-att-list ',' 'string_literal'
DELETE_CONCAT_ATTRIBUTE := exist_att Deleting concatenated attributes:
exist_att := 'string_literal'
TREX_FLAGS = ('column_name','flags'), Flags are the implementation flags; the integer value is currently between 0 and 67108864. INDEX_PROPERTY = ('prop#', 'value') Changing column table properties ALTER TABLE WITH PARAMETERS (INDEX_PROPERTY = ('prop#', 'value') )
PARTITION_SPEC = 'partition spec',
SAP In-Memory Database SQL Reference Manual
43
For example, 'HASH 2 a, b' where 2 is the number of parts to create and 'a' and 'b' are the columns that are used to create that hash value for each row. The ALTER TABLE either splits or merges the table. Use 'MERGE' as partition spec to merge the parts. The tables have to be merged before they can be split using a different partition specification.
DSO_ACTIVATE_REQUESTS = (rid_list) dso-activation of the given request IDs in the corresponding DSO index. rid_list has to be a comma-separated list of strings. Activation in the DSO sense means changing the active data of the DSO index by applying the changes of the given requests residing in the Activation Queue. This parameter will only work for DSO indexes.
DSO_ROLLBACK_REQUESTS = (rid_list)
dso-rollback of the given RequestIds in the corresponding DSO index.
rid_list has to be a comma-separated list of strings. Rollback in the DSO sense means
removing the changes in the active data of the DSO that were caused by the given requests.
This parameter will only work for DSO indexes.
Description: The ALTER TABLE statement adds, deletes or modifies a column from a table and creates or drops a
primary key. It can also delete or add multiple columns at a time. The new column will initially be filled with a default value. If a default is not specified, then the value is NULL. Example 1: ALTER TABLE T1 ADD (column_a VARCHAR(10));
ALTER TABLE T1 WITH PARAMETERS('CONCAT_ATTRIBUTE'=('A$B','A','B'));
SAP In-Memory Database SQL Reference Manual
44
ALTER USER SQL Syntax: ALTER USER user_name IDENTIFIED BY password ALTER USER user_name IDENTIFIED EXTERNALLY AS external_identity
Parameters: IDENTIFIED BY You can change a users password with this command
IDENTIFIED EXTERNALLY You can change the external authentication. For information about external identities, contact your
system administrator. Description: The ALTER USER statement modifies the database user.
Users created with IDENTIFIED BY cannot be changed to EXTERNALLY and vice versa, the users created with EXTERNALLY cannot be changed to IDENTIFIED BY.
Example: ALTER USER my_user IDENTIFIED BY AAAAAAa1;
SAP In-Memory Database SQL Reference Manual
45
CREATE CALCULATION SCENARIO SQL Syntax:
CREATE CALCULATION SCENARIO name USING [WITH PARAMETERS ( , ... ) ]
Parameters: xml ::= string_literal
parameter_key_value ::=
'DEFAULT_SCHEMA' = schema_name |'INMEMORY_SCENARIO' = '0' |'INMEMORY_SCENARIO' = '1'
|'EXPOSE_NODE' = ( node_name, view_name )
|'EXPOSE_NODE' = ( node_name, schema_name, view_name )
Description: The CREATE CALCULATION SCENARIO statement creates a calculation scenario.
Please note that a calculation scenario is not a default catalog object, so it cannot be accessed via a SELECT statement after its creation. A column view of type calculation is required on top of the scenario to query it.
As it is not a catalog object, it is also not linked to a catalog schema. The first part of the name can be rather seen as a package in which the scenario is created. This implies that a drop of a (database)
schema does not drop the calculation scenarios. The scenario is defined with the used XML-string which has to be formatted as described in the XSD. The default schema defines on the one hand the package in which the scenario is created (if not defined explicitly), and the schema for the views on top listed in the expose nodes on the other.
It can be either in-memory only (set optional parameter value 'INMEMORY_SCENARIO' = '1') or persistent. The default scenario is persistent and stored in the repository. With an expose node item it is possible to directly create a column view of type calculation on top of a specified node on top of the scenario. The view name is defined in 'view_name'. The referenced node has to be specified in 'node_name'.
Note that sub-transactions have started (and committed) in order to create the views.
Note: For application development, do not use these exposed node items. Instead, create the column views of type calculation manually. Example
DROP TABLE TEST1;
CREATE INSERT ONLY COLUMN TABLE TEST1(A INTEGER, B DOUBLE);
insert into test1 values ( 1, 2);
insert into test1 values ( 11, 22);
insert into test1 values ( 111, 222);
DROP CALCULATION SCENARIO SYSTEM.DEMO_PROJECTION_OP cascade;
CREATE CALCULATION SCENARIO SYSTEM.DEMO_PROJECTION_OP USING '
SAP In-Memory Database SQL Reference Manual
46
"A" * "B"
"C" > 1
'
;
DROP VIEW SYSTEM.MYCALCVIEW;
CREATE COLUMN VIEW SYSTEM.MYCALCVIEW TYPE CALCULATION
WITH PARAMETERS
('PARENTCALCINDEXPACKAGE'='SYSTEM',
'PARENTCALCINDEX'='DEMO_PROJECTION_OP',
'PARENTCALCNODE'='demo_projection');
SELECT * FROM SYSTEM.MYCALCVIEW;
DROP CALCULATION SCENARIO SYSTEM.DEMO_PROJECTION_OP CASCADE;
SAP In-Memory Database SQL Reference Manual
47
CREATE COLUMN VIEW
SQL Syntax:
CREATE COLUMN VIEW view_name [ ( column_name, ... ) ] [ TYPE ] AS [ WITH PARAMETERS (, ... ) ]
Parameters:
types := OLAP | JOIN | HIERARCHY | PROPERTY | LANGUAGE | CALCULATION | FBO
parameter_key_value ::=
= |
= | = (, ... )
string_literal ::= description | index_type | join_index | join_condition | join_path | constraint | index_alias | join_index_type | join_index_estimation |view_attribute | view | default_view | semantic_relation | rfc_destination | auto_replication| key_figure | unit_conversion |
optimize_metamodel | free_style_search_attribute |all_attrs_free_style_relevant | flags | characteristic | hierarchy_definition_type | hierarchy_definition description ::= DESCRIPTION = string_literal
index_type ::= INDEXTYPE = int_const
OLAP_INDEX = 5,
JOIN_INDEX = 6,
CALC_INDEX = 11
join_index ::= JOININDEX = table_name Table name to be joined. Join_index can be repeated as desired.
join_condition ::= JOINCONDITION = ( string_literal , table_name , column_name , table_name ,column_name ) |JOINCONDITION = ( string_literal , table_name , column_name , table_name, column_name , string_literal , int_const , int_const ) |JOINCONDITION = ( string_literal ,table_name , column_name , table_name , column_name , string_literal , int_const , int_const', string_literal )
|JOINCONDITION = ( string_literal , table_name , column_name , table_name ,column_name , string_literal , int_const , int_const' , string_literal , string_literal )
Specifies the join attributes, join condition can be repeated as desired. Example: join_condition = name, table1, field1, table2, field2, [constraint]. join_path ::=
JOINPATH' = ( string_literal , string_literal ) |JOINPATH = ( string_literal , string_literal ,string_literal ) JOINPATH can be repeated as desired. Example: join_path = name, join1, join2, [constraint]
constraint ::= CONSTRAINT = ( string_literal , int_const , string_literal , table_name , column_name, string_literal , string_literal [ , int_const , string_literal ] )
SAP In-Memory Database SQL Reference Manual
48
Example: constraint=name,type,location,indexed,attrname,operator,value[,specialConstraintType, defaultValue]
index_alias ::= INDEXALIAS = ( table_name = string_literal ) Species an alias for an index. join_index_type ::= JOININDEXTYPE = int_const Example: join_index_type =(0 None, 1 fact table, 2 d table, 3 x table, 4 y table, 5 hirearchy, 6
s table, 7 external object, 8 conv table) join_index_estimation ::= JOININDEXESTIMATION = int_const join_index_estimation specifies number of estimated documents in an index
view_attribute ::= VIEWATTRIBUTE = ( string_literal , table_name , column_name , string_literal , string_literal , string_literal ) |VIEWATTRIBUTE = ( string_literal , table_name , column_name , string_literal , string_literal ,string_literal , string_literal ) |VIEWATTRIBUTE = ( string_literal , table_name , column_name , string_literal , string_literal ,string_literal , string_literal , string_literal )
|VIEWATTRIBUTE = ( string_literal , string_literal ) Example: view_attribute = (name, table, field, joinPath, viewName, type, defaultDefinition, [expression])
view ::= VIEW = (string_literal , table_name [ , string_literal ]) view = (name, anchor table)
default_view ::= DEFAULTVIEW = string_literal
Specifies default view name.
semantic_relation ::= SEMANTICRELATION = ( string_literal , string_literal , string_literal , string_literal )
Example: semantic_relation = name, joinPath, view, type
key_figure ::= KEYFIGURE = ( column_name , int_const ) | KEYFIGURE = ( column_name , int_const , string_literal ) | KEYFIGURE = ( column_name , string_literal , int_const ) | KEYFIGURE = ( column_name , string_literal , int_const , string_literal ) | KEYFIGURE = ( column_name , string_literal , int_const , string_literal , RESTRICTION = string_literal) | KEYFIGURE = ( column_name , string_literal , int_const , string_literal , string_literal , RESTRICTION= string_literal ) | KEYFIGURE = ( column_name , int_const , FORMULA = string_literal , DESCRIPTION = string_literal , UNITCONVERSIONNAME = string_literal , EXPRESSION = string_literal , EXPRESSIONFLAGS = int_const )
| KEYFIGURE = ( column_name , int_const , FORMULA = string_literal , DESCRIPTION = string_literal , UNITCONVERSIONNAME = string_literal , EXPRESSION = string_literal , EXPRESSIONFLAGS = int_const , RESTRICTION = string_literal ) | KEYFIGURE = ( column_name , int_const , FORMULA = string_literal , DESCRIPTION = string_literal , UNITCONVERSIONNAME = string_literal , EXPRESSION = string_literal , EXPRESSIONFLAGS = int_const , INDEXID = table_ref_trex , ATTRIBUTE = column_name )
SAP In-Memory Database SQL Reference Manual
49
| KEYFIGURE = ( column_name , int_const , FORMULA = string_literal , DESCRIPTION = string_literal , UNITCONVERSIONNAME = string_literal , EXPRESSION = string_literal , EXPRESSIONFLAGS = int_const , INDEXID = table_ref_trex , ATTRIBUTE = column_name , RESTRICTION = string_literal , ALIAS = int_const )
Possible key_figure values are as follows: keyFigure,defaulttype keyFigure,defaulttype,description keyFigure,formula,defaulttype keyFigure,formula,defaulttype,restriction=x keyFigure,formula,defaulttype,description
keyFigure,formula,defaulttype,description,restriction=x keyFigure,defaulttype,formula=x,description=x,unitConversionName=x,expression=x,expressionFlags=x keyFigure,defaulttype,formula=x,description=x,unitConversionName=x,expression=x,expressi
onFlags=x,restriction=x keyFigureName,defaulttype,formula=x,description=x,unitConversionName=x,expres