You are on page 1of 7

SAP HANA Data Type Conversion Functions

HANA 5,511 Views

inShare17

Hi Everyone,
In any Data warehousing system, one of the main objective is to transform the source data
into meaningful information which can be easily understood and analyzed by business. In
order to achieve this we need to have knowledge on the different types of functions
available at database level and how to use each of those functions.
The SQL Functions in SAP HANA are categorized into 9 types, they are:

String Functions

Numeric Functions

Data Type Conversion functions

Date Time Functions

Window Functions

Miscellaneous Functions

Full Text Functions

Aggregate Functions

Series Data Functions

In this article, I would like to explain about the different SQL Data Type Conversion
Functions available in SAP HANA, along with Syntaxs and some Examples. These functions
are used to convert function arguments from one data type to another, or used to test
whether a conversion is possible or not.
Below are the list of SAP Data Type Conversion functions available in SAP HANA.
Note: Numeric data type conversions always truncate least significant digits towards zero.

CAST: This function converts the given value to the specified data type.
Syntax: CAST(<Value/Column/Expression> AS <Data Type>)
Example: SELECT CAST(1234 AS NVARCHAR) AS STRING FROM DUMMY
Result: This converts the number 1234 to string.
Example: SELECT CAST(123.45 AS INTEGER) AS INTEGER FROM DUMMY
Result: This converts the decimal value 123.45 to integer 123 (decimals are always
truncate towards zero).

TO_ALPHANUM: This function converts a given value to an ALPHANUM data type.


Syntax: TO_ALPHANUM(<Value>)
Example: SELECT TO_ALPHANUM (10) to alphanum FROM DUMMY;
Result: Given value 10 will be converted to alpha numeric.

TO_BIGINT: This function converts the given value to a BIGINT data type.
Note: If the entered value has decimal points then these digits are truncated during
conversion.
Syntax: TO_BIGINT(<Value>)
Example: SELECT TO_BIGINT(123) AS TO BIGINT FROM DUMMY
Result: This string 123 will be converted to 123 number.
Example: SELECT TO_BIGINT(123.75) AS TO BIGINT FROM DUMMY
Result: Decimal value 123.75 will be converted to number 123 and decimal values will
be truncated.

TO_BINARY: This function is used to convert the given/source value to a BINARY


data type.
Syntax: TO_BINARY(<value>)
Example: SELECT TO_BINARY(ABC) AS TO BINARY FROM DUMMY
Result: The binary value for ABC is going to be 414243.

TO_BLOB: This function is used convert the given/source value to a BLOB data type.
Note: The source/given value must be a binary string.
Syntax: TO_BLOB(<value>)
Example: SELECT TO_BLOB (TO_BINARY(ABC)) TO BLOB FROM DUMMY;
Result: The result is going to ABC again because it will be converted to binary value
which gain will be converted to characters to TO_BLOB function.

TO_CLOB: This function is used convert the given/source value to a CLOB data type.
Syntax: TO_CLOB(<value>)
Example: SELECT TO_CLOB(SAPSTUDENT.COM is learning resource for SAP
HANA)AS TO CLOB FROM DUMMY
Result: The result is going to be same string with the data type CLOB.

TO_DATE: This function is used to convert the date values stored in string format
into HANA default date format YYYY-MM-DD. The input should the date string and format
how it is stored. If the string date format is in the same format of HANA default or
YYYY/MM/DD, then we can skip entering the date format in the input.
Syntax: TO_DATE (<Value>,<Format>)
Example: SELECT TO_DATE(1/12/2015, MM/DD/YYYY) to date FROM DUMMY;
Result: 2015-01-12 (the source string date with format MM/DD/YYYY has been
converted HANA data format YYYY-MM-DD

Example: SELECT TO_DATE(2015-01-12) to date FROM DUMMY;


Result: 2015-01-12. If the source string date value is in HANA default format, then
we can skip the format in the input)

TO_DATS: This function is used to convert the date string values to ABAP DATE
string with format YYYYMMDD.
Note: The source date string value format should HANA default date type i.e YYYY-MMDD. If the format is not like HANA default, then we need to use TO_DATE function before
be apply TO_DATS.
Syntax: TO_DATS(<date string>)
Example: SELECT TO_DATS(2015-01-01) AS ABAP DATE FROM DUMMY
Result: This gives use result as 20150101.
Example: SELECT TO_DATS(TO_DATE(01/01/2015,MM/DD/YYYY)) AS ABAP
DATE FROM DUMMY
Result: TO_DATE function first converts source date into HANA format and then
TO_DATS will used to convert it to ABAP format as shown in the above result.

TO_DECIMAL: This function is used to convert the given value to the specified
precision and scale.
Note: The precision value can range from 1 to 34 and scale can vary from -6111 to 6111.
DECIMAL(5,2) means the maximum value it can store is 999.99 but not
99999.99.
Syntax: TO_DECIMAL(<value>,<precision>,<scale>)
Example: SELECT TO_DECIMAL(1234.789,10,2) AS TO DECIMAL FROM DUMMY
Result: 1234.78 (the scale is truncated to 2)
Example: SELECT TO_DECIMAL(1234.789,10,0) AS TO DECIMAL FROM DUMMY
Result: 1234 (The scale is defined as 0, so everything is truncated after precision)

TO_DOUBLE: This function is used to convert the given string value to double data
type.
Syntax: TO_DOUBLE(<vale>)
Example: SELECT TO_DOUBLE(1234.78) AS TO DOUBLE FROM DUMMY
Result: 1234.78
Example: SELECT TO_DOUBLE(1234.78) AS TO DOUBLE FROM DUMMY
Result: 1234.78

TO_FIXEDCHAR: This function is used extract the specified number of characters


from input string.
Syntax: TO_FIXEDCHAR(<string>,<no of characters>)
Example: SELECT TO_FIXEDCHAR(sap student,3) AS TO FIXED
CHAR FROMDUMMY
Result: sap (first 3 characters from the input string)
Proceed to the next page to continue reading

TO_INT: This function is used to convert the input value or string to INTEGER data
type.
Note: If the input value has precision, then this will be truncated during conversion
process.
Syntax: TO_INT(<value>)
Example: SELECT TO_INT(10) AS TO INTEGER FROM DUMMY
Result: 10 (String 10 has converted to number)
Example: SELECT TO_INT(10.50) AS TO INTEGER FROM DUMMY
Result: 10 (The value .50 has been truncated from the input value during conversion
process).

TO_INTEGER: This function is used to convert the input value or string to INTEGER
data type.
Note: If the input value has precision, then this will be truncated during conversion
process. This function is same as TO_INT above.
Syntax: TO_INT(<value>)
Example: SELECT TO_INT(10) AS TO INTEGER FROM DUMMY
Result: 10 (String 10 has converted to number)
Example: SELECT TO_INT(10.50) AS TO INTEGER FROM DUMMY
Result: 10 (The value .50 has been truncated from the input value during conversion
process).

TO_NCLOB: This function is used convert the given/source value to a NCLOB data
type.
Syntax: TO_NCLOB(<value>)
Example: SELECT TO_NCLOB(SAPSTUDENT.COM is learning resource for SAP
HANA) AS TO NCLOB FROM DUMMY
Result: The result is going to be same string with the data type NCLOB.

TO_NVARCHAR: This function is used to convert the input value with the specified
format to NVARCHAR data type.
Syntax: TO_NVARCHAR(<value>,<format>)
Example: SELECT TO_NVARCHAR(1234) AS TO NVARCHAR FROM DUMMY
Result: 1234 (The number is converted to NVARCHAR)
Example: SELECT TO_NVARCHAR(TO_DATE(2009/12/31), MM-DD-YY) TO
NVARCHAR FROM DUMMY;
Result: 12-31-09 (TO_DATE converts the given input to YYYY-MM_DD format and
then TO_NVARCHAR extract the output in MM-DD-YY format)

TO_REAL: This function is used to convert the input value or string to REAL (single
precision value) data type.
Syntax: TO_REAL(<value>)

Example: SELECT TO_REAL(12.04) AS TO REAL FROM DUMMY


Result: 12.039999961853027
Example: SELECT TO_REAL(12.04) AS TO REAL FROM DUMMY
Result: 12.039999961853027

TO_SECONDDATE: This function is used to convert the given input string date value
with specified format into SECONDDATE data type.
Note: If the input date string format is either YYYY-MM-DD or YYYY/MM/DD then no need
to give format. If it is other than the above two formats, we need to specify the date
string format for source.
Syntax: TO_SECONDDATE(<date string>,<format>)
Example: SELECT TO_SECONDDATE (2010-01-11 13:30:00, YYYY-MM-DD
HH24:MI:SS) TO SECONDDATE FROM DUMMY;
Result: 2015-07-01 13:30:00.0
Example: SELECT TO_SECONDDATE (07/01/2015 13/30/00, MM/DD/YYYY
HH24/MI/SS) TO SECONDDATE FROM DUMMY;
Result: 2015-07-01 13:30:00.0

TO_SMALLDECIMAL: This function is used to convert the given value/string to


SMALLDECIMAL data type.
Note: The difference between DECIMAL and SMALLDECIMAL is in terms of precision and
scale. The precision range is 1~16 and scale is -369~369 for SMALLDECIMAL
Syntax: TO_SMALLDECIMAL(<value>)
Example: SELECT TO_SMALLDECIMAL(1234.89) TO
SMALLDECIMAL FROMDUMMY;
Result: 1234.89
Example: SELECT TO_SMALLDECIMAL(1234.89) TO
SMALLDECIMAL FROMDUMMY;
Result: 1234.89

TO_SMALLINT: This function is used to convert the input value or string to


SMALLINT data type.
Note: If the input value has precision, then this will be truncated during conversion
process. The different between SMALLINT and INTEGER lies in the range of values it can
store. SMALLINT can store between -32,768 and 32,768.
Syntax: TO_SMALLINT(<value>)
Example: SELECT TO_SMALLINT(10) AS TO SMALLINT FROM DUMMY
Result: 10 (String 10 has converted to number)
Example: SELECT TO_SMALLINT(10.50) AS TO SMALLINT FROM DUMMY
Result: 10 (The value .50 has been truncated from the input value during conversion
process).

TO_TIME: This function is used to convert the input time string to TIME data type
with the specified source format to HANA format. SAP HANA format for time is HH:MM:SS
Syntax: TO_TIME(<value>,<format>)
Example: SELECT TO_TIME (14/20/10, HH/MI/SS) TO TIME FROM DUMMY;
Result: 14:20:10
Example: SELECT TO_TIME (08:30 AM, HH:MI AM) TO TIME FROM DUMMY;
Result: 08:30:00

TO_TIMESTAMP: This function is used to convert the input string timestamp value
with specified format to HANA TIMESTAMP data type.
Note: The difference between SECONDDATE and TIMESTAMP is in in terms of how much
information it can store. SECONDDATE can store up to seconds where TIMESTAMP can
store up to milli Seconds.
Syntax: TO_TIMESTAMP(<value>,<format).
Example: SELECT TO_TIMESTAMP (2010-01-11 13:30:00.123, YYYY-MM-DD
HH24:MI:SS.FF7) TO TIMESTAMP FROM DUMMY;
Result: 2010-01-11 13:30:00.123

TO_TINYINT: This function is used to convert the input value or string to TINYINT
data type.
Note: If the input value has precision, then this will be truncated during conversion
process. The different between TINYINT and INTEGER lies in the range of values it can
store. TINYINT can store between 0 and 255.
Syntax: TO_TINYINT(<value>)
Example: SELECT TO_TINYINT(10) AS TO TINYINT FROM DUMMY
Result: 10 (String 10 has converted to number)
Example: SELECT TO_TINYINT(10.50) AS TO TINYINT FROM DUMMY
Result: 10 (The value .50 has been truncated from the input value during conversion
process).

TO_VARCHAR: This function is used to convert the input value with the specified
format to NVARCHAR data type.
Syntax: TO_NVARCHAR(<value>,<format>)
Example: SELECT TO_NVARCHAR(1234) AS TO NVARCHAR FROM DUMMY
Result: 1234 (The number is converted to NVARCHAR)
Example: SELECT TO_NVARCHAR(TO_DATE(2009/12/31), MM-DD-YY) TO
NVARCHAR FROM DUMMY;
Result: 12-31-09 (TO_DATE converts the given input to YYYY-MM_DD format and then
TO_NVARCHAR extract the output in MM-DD-YY format)
With this we have successfully looked at different data type conversion functions in SAP
HANA and its syntaxes along with examples.

Thank you for reading and hope this information is helpful. Please do share with your friends
if you feel the information is useful.
Happy Learning.

You might also like