Javatpoint Logo
Javatpoint Logo

SQL CAST Function

The SQL CAST function is mainly used to convert the expression from one data type to another data type. If the SQL Server CAST function is unable to convert a declaration to the desired data type, this function returns an error. We use the CAST function to convert numeric data into character or string data.


Here, the [data type] is a type of valid data types in RDBMS.

The syntax is:

  • Expression: It is a valid expression where we want to convert a data type into the SQL.
  • Data_type: It is a Data Type to which we want to convert the expression.
  • Length: It is the optional parameter of an integer type. We can use the setting to define the length of any targeted data type.

By default, it is 30.


Here, we will use the below tables.

Table Manager _ Score

Column Name Date Type
Manager Id Integer
First _ Name char( 20)
Score float

The table contains the following rows:

Table Manager_Score

ManagerID First_Name Score
1 Jame 92.2
2 Boby 87.5
3 Marry 70
4 Sanju 120.2

Example 1:


First_Name Int_Score
Jame 92
Boby 87
Marry 70
sanju 120

In Example 1, we are using the CAST function to convert the SCORE column from type FLOAT to INTEGER. When we do it, various RDBMS have many rules to handle the numbers to the point of decimal.

According to the above example, the numbers after the decimal point are truncated.

Example 2:


First__Name Char__Score
Jame 85.
Boby 92.
Marry 90
sanju 110

In Example 2, we use the CAST function to convert the SCORE column from type FLOAT to CHAR (3). When we do it, we only hold the first 3 character. If there are more than three characters, everything after the first three characters is discarded.

Next TopicSQL Tutorial

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA