Table of contents
1.
Introduction
2.
CAST SQL
2.1.
Syntax
2.2.
Example 1:
2.3.
Code
2.3.1.
Output
2.4.
Explanation
2.5.
Code
2.5.1.
Output
2.6.
Explanation
2.7.
Example 2:
2.8.
Code
2.8.1.
Output
2.9.
Explanation
3.
Properties of CAST SQL
4.
Applications of SQL CAST
5.
Drawbacks of CAST SQL
6.
Frequently Asked Questions
6.1.
What is CAST() in SQL?
6.2.
What is CAST () and convert () function in SQL Server?
6.3.
What is CAST in MySQL query?
6.4.
Why we will use CAST in SQL?
7.
Conclusion
Last Updated: Feb 5, 2025
Medium

SQL CAST() Function

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The CAST SQL function is generally used to convert the expression from one data type to another data type. Sometimes we need to perform conversion from one data type to another in SQL. Do you know how we can achieve this? If not, don't worry; we will discuss how to achieve this using cast SQL.

cast sql

In this article, we will discuss about the CAST SQL function, its syntax, properties, applications, and drawbacks. We will understand the concept of CAST SQL with an example.

CAST SQL

In Sql, the cast function is used for converting a value of one data type to another. We can convert the data type by specifying the data type we want it to convert to. This function accepts two parameters, i.e., the value we want to convert and the desired data type. The cast() function returns the value of the converted data type, but if it fails to return the value, it returns an error.

Before studying cast SQL, one must know the data types and their uses. In SQL, multiple data types exist, such as ‘INT,’ ‘VARCHAR,’ ‘FLOAT,’ ‘DATETIME,’ ‘DATE,’ and many more. One must know about the functions of each and their characteristics.

Apart from these, we must know about SQL’s syntax and should know how to write basic queries.

Syntax

Below is the syntax of CAST SQL.

CAST( expression AS [data type] [length] )

 

  • The [data type] is a valid data type in RDBMS.
     
  • The expression refers to the value or expression that we wish to convert to a specific data type.
     
  • [data type] is the type of data we want.
     
  • [length], an optional parameter, sets the desired data type's length. Its default value is 30.

Example 1:

First, we create a table named as “students” containing students data like ‘student_id’, ‘name’ and ‘marks’.

Code

CREATE TABLE students (
    student_id INT PRIMARY KEY,
    first_name VARCHAR(50),
    marks INT
);

INSERT INTO students (student_id, first_name, marks)
VALUES
    (1, 'Ninja1', 90.8),
    (2, 'Ninja2', 85.56),
    (3, 'Ninja3', 78),
    (4, 'Ninja4', 92.796),
    (5, 'Ninja5', 88.01);
    
SELECT * FROM students;

Output

output

Explanation

In this, we have created a table named students, which contains information about students like ‘student_id’, ‘first_name’, and ‘marks’ obtained.

Now, let's perform cast function to above table.

Code

SELECT 
   first_name, 
   CAST(marks AS INT) AS converted_integer_marks
FROM 
   students;

Output

output

Explanation

In this code, we have selected the first_name from the “students” table and cast the “marks” column as an integer. The "AS" keyword is used to give a label to the column that is created by casting (converting) the "marks" column to a different type (Integer). 

Example 2:

Let's understand CAST() function through another example. Consider the same table that we have consider above. 

Code

SELECT 
	first_name
,
	CAST(marks AS CHAR(6)) Char_marks
FROM 
	students;

Output

output

Explanation

This code will show the "first_name" and "Char_Score" columns for each row in the "students" table. The "Char_Score" column will show the "marks" values converted to a fixed-length character of 6.

Also see, SQL EXCEPT

Properties of CAST SQL

The properties of CAST SQL are as follows.

  • We can convert our expression to any of the following data types: bigint, int, smallint, tinyint, bit, decimal, numeric, money, smallmoney, float, real, datetime, smalldatetime, char, varchar, text, nchar, nvarchar, ntext, binary, varbinary and image.
    Recommended read- Data types in sql
     
  • The cast SQL function that helps to achieve explicit conversion for converting one data type to another. It is useful when the system or language does not support a particular type of conversion. 
     
  • The function truncates the result for converting a numeric or float data type value to an integer. This means that the decimal value is discarded. For example, SELECT CAST(5.4 AS INT); gives the result 5. But for others, we get a round result, i.e., SELECT CAST(5.4 AS FLOAT); gives the result 5.4.
     
  • The TRY_CAST() function is used if a conversion fails and we don't want an error to be returned instead, it returns a null in case of failed conversion.

Applications of SQL CAST

The applications of CAST SQL are as follows.

  • The CAST SQL function is used for converting one value of one data type to another data type; this is also known as explicit type conversion.
     
  • The cast() function is useful in scenarios when we need to compare a string containing a numeric value to a real one. The cast() function can be used to convert this string to a number.
     
  • Similarly, many other operations, such as grouping, sorting, and optimizing a query, can be done using cast() SQL.

Drawbacks of CAST SQL

The drawbacks of CAST SQL are as follows.

  • The cast function may slow down the queries in dealing with large datasets.
     
  • The cast function gives an error for an invalid conversion, and it becomes important to handle these errors carefully to avoid unnecessary behavior.
     
  • If the function is not used carefully, it may result in a loss of information. For example, If we wish to convert a numeric or float data type value to an integer, the function truncates the result. This means that the decimal value is discarded. 

Frequently Asked Questions

What is CAST() in SQL?

In SQL, the cast function is used for converting a value of one data type to another. We can convert the data type by specifying the data type we want it to convert to. This function accepts two parameters, i.e., the value we want to convert and the desired data type.

What is CAST () and convert () function in SQL Server?

Cast and convert functions transform data types. Both convert a value from one type to another, serving similar purposes in database queries or programming.

What is CAST in MySQL query?

MySQL's CAST() function converts values between data types within expressions, commonly used in WHERE, HAVING, and JOIN clauses, similar to the CONVERT() function.

Why we will use CAST in SQL?

The CAST function in SQL converts values between data types, ensuring compatibility during operations like calculations or comparisons and providing accurate results.

Conclusion

In this article, we have discussed about the cast SQL function, its syntax, properties, and applications and understood the concept by discussing some examples. You can read more such articles on our platform, Coding Ninjas Studio

Also, read-


You will find straightforward explanations of almost every topic on this platform. So take your preparation journey to the next level using Coding Ninjas.

Happy Coding!

Live masterclass