CAST function in Cassandra - GeeksforGeeks

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 tableThe following table describes the conversions supported by the cast function. Cassandra will silently ignore any cast converting a datatype into its own datatype.
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 CASTSELECT movie_date
FROM movies
WHERE movie_id = 7c3cffb8-0dc4-1d27-af24-c007b5fc5643; Output:2019-10-15 01:11:50.000000+0000 (time stamp format) 
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 CASTSELECT 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) 
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) 
Apache
DBMS
DBMS
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Data Preprocessing in Data Mining
Cosine Similarity
Deadlock in DBMS
Introduction of B-Tree
Second Normal Form (2NF)
Introduction of Relational Algebra in DBMS
Boyce-Codd Normal Form (BCNF)
Advantages of Database Management System
SQL | GROUP BY
KDD Process in Data Mining