Skip to content
Related Articles

Related Articles

CAST function in Cassandra
  • Last Updated : 30 Oct, 2019

CAST function helps in Converts data from one data type to another data type in Cassandra.

In Cassandra CAST function Supported in SELECT statements. lets have a look how we can used CAST function in select statement.

SELECT CAST([fieldname] AS [data type]) 
FROM [table name] 

Basic function of CAST:

  • It Converts From any native data type to text data type in formats such that ASCII and UTF-8.
  • It Converts Between numeric data types such that from `int` to `smallint`, `smallint` to `int`, etc.
  • By using the CAST function we can Converts the Most common use case.
  • It is very helpful in case of optimization where we have the need to frequent change from one data type to another.
  • By using the CAST function we can converts timestamp to text for display purposes in Cassandra.

This is a table for reference in which all native data type which can convert from one data type to another.

Lets have a look,



Table: CAST conversion table
The following table describes the conversions supported by the cast function. Cassandra will silently ignore any cast converting a datatype into its own datatype.

FromTo
asciitext, varchar
biginttinyint, smallint, int, float, double, decimal, varint, text, varchar
booleantext, varchar
countertinyint, smallint, int, bigint, float, double, decimal, varint, text, varchar
datetimestamp
decimaltinyint, smallint, int, bigint, float, double, varint, text, varchar
doubletinyint, smallint, int, bigint, float, decimal, varint, text, varchar
floattinyint, smallint, int, bigint, double, decimal, varint, text, varchar
inettext, varchar
inttinyint, smallint, bigint, float, double, decimal, varint, text, varchar
smallinttinyint, int, bigint, float, double, decimal, varint, text, varchar
timetext, varchar
timestampdate, text, varchar
timeuuidtimestamp, date, text, varchar
tinyinttinyint, smallint, int, bigint, float, double, decimal, varint, text, varchar
uuidtext, varchar
varinttinyint, smallint, int, bigint, float, double, decimal, text, varchar

Source – Cassandra.Apache.org

CQL query for CAST function:
Let’s take an example: movies is a table name in which we want to change its native data type such that movie_date is field name which have timestamp data type and if we want to convert it into another native data type such that in text data type.

To create table used the following CQL query.

CREATE TABLE movies
 (
  movie_id int,
  movie_date timestamp,
  PRIMARY KEY (movie_id)
 ); 

Insert the following data into the table:

movie_id : 7c3cffb8-0dc4-1d27-af24-c007b5fc5643
movie_date : 2019-10-15 01:11:50.000000+0000  

INSERT INTO movies (movie_id, movie_date) 
       VALUES (7c3cffb8-0dc4-1d27-af24-c007b5fc5643, 
                 '2019-10-15 01:11:50.000000+0000 '); 

So, here is the format of how we can convert from one data type to another by using the CAST function. The below-given statement means that we are going to convert movie_date timestamp to movie_date text.

SELECT CAST(movie_date AS text) 
  • Result: Without CAST
    SELECT movie_date
    FROM movies
    WHERE movie_id = 7c3cffb8-0dc4-1d27-af24-c007b5fc5643; 

    Output:

    2019-10-15 01:11:50.000000+0000 (time stamp format) 
  • Result: With CAST
    SELECT CAST(movie_date AS text)
    FROM movies
    WHERE movie_id = 7c3cffb8-0dc4-1d27-af24-c007b5fc5643; 

    Output:

    2019-10-15 01:11:50.000Z (Coordinated Universal Time, or UTC) 
My Personal Notes arrow_drop_up
Recommended Articles
Page :