SQL Tutorial
SQL Database
SQL Table
SQL Select
SQL Order By
SQL Insert
SQL Update
SQL Delete
Difference
SQL Injection
SQL String Functions
Miscl
- SQL Formatter
- SQL group by
- SQL add/drop/update column operation
- SQL CAST Function
- SQL Comments
- SQL CONCAT Function
- CTE (Common Table Expression)SQL
- How to use distinct in SQL?
- Joining Three or More Tables in SQL
- What is Web SQL?
- How to create functions in SQL?
- How to run SQL Script?
- How to Delete Duplicate Rows in SQL?
- Nth Highest salary
- 12 Codd's Rules
- SQL EXCEPT
- Types of SQL JOIN
- Change datatype of column in SQL
- SQL Auto Increment
- SQL Like
- Commit and Rollback in SQL
- SQL Concatenate
- SQL get month from the date
- Savepoint in SQL
- SQL ORDER BY DATE
- TIME Datatype in SQL
- SQL BETWEEN
- CRUD Operations in SQL
- SQL INDEX
- Scalar Functions in SQL
- SET Operators in SQL
- Types of SQL Commands
- TCL Commands in SQL
- SQL Subquery
- SQL View
- Constraints in SQL
- Pattern Matching in SQL
- SQL Date Functions
- DDL Commands in SQL
- DML Commands in SQL
- SQL CASE
- SQL Inner Join
- SQL IN Operator
- Check Constraint in SQL
- SQL CLAUSES
- SQL LOGICAL OPERATORS
- Delete Column from Table
- Add Column in the Table
- Delete one row in SQL
- Change the Column Value
- How to Add Foreign Key in SQL
- Add a Primary Key
- Insert One or More rows
- How to Use LIKE in SQL
- Cursor in SQL
- Difference Between DROP and Truncate
- SQL Comparison Operators
- SQL COUNT WHERE
- SQL SELECT MIN
- SQL Stored Procedure
- SQL SELECT AVG
- SQL SELECT MAX
- SQL ADD COLUMN
- How to use Auto-Increment in SQL
- SQL Languages
- SQL Arithmetic Operators
- How to Use GROUP BY in SQL
- How to Use ORDER BY in SQL
- Trigger in SQL
- What is Race Condition
- SQL COUNT DISTINCT
PL/SQL Tutorial
Sql Interview Question
SQl Quiz
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.
Syntax:
Here, the [data type] is a type of valid data types in RDBMS.
The syntax is:
_ _ CAST in the SQL example
SELECT CAST (123 AS VARCHAR (20)) [result_name]
FROM [Source]
- 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.
Examples:
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:
Int_Score FROM Student_Score;
Result:
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:
Char_Score FROM Student_Score;
Result:
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.