Implicit Data-Type Conversion
In this type of conversion, the data is converted from one type to another implicitly (by itself/automatically).
From | To |
---|---|
VARCHAR2 or CHAR | NUMBER |
VARCHAR2 or CHAR | DATE |
DATE | VARCHAR2 |
NUMBER | VARCHAR2 |
Query
create table employees(
employee_id INT PRIMARY KEY ,
first_name VARCHAR(50) ,
salary INT);
INSERT INTO employees(employee_id,first_name,salary)
VALUES
(100,'Steven',24000),
(101,'Neena',17000),
(102,'Lex',17000),
(103,'John',11000),
(104,'Robert',12000),
(105,'Leo',10000);
1. Query
Here, we want to retrieve the employee_id, first_name, and salary from the employees table whose salary is greater than 15000 then the query is
SELECT employee_id,first_name,salary
FROM employees
WHERE salary > 15000;
1. Output
2. Query
SELECT employee_id,first_name,salary
FROM employees
WHERE salary > '15000';
2. Output
Here we see the output of both queries came out to be the same, in spite of the 2nd query using ‘15000’ as text, it is automatically converted into an int data type.
Conversion Function in SQL
When you define expressions and local variables then you should specify what type of data will be stored in those objects such as text data, money, dates, numbers, or characters.
- Strings Data types such as CHAR and VARCHAR.
- Decimal values such as FLOAT and REAL.
- Binary String such as BINARY.
- Date and Time Data Types such as DATE, TIME, TIMESTAMP, and DATETIME.
- Numeric Data types such as INT, DOUBLE, and BIGINT.
- MS Access Data Types such as TEXT, LONG, and BYTE.
On the basis of this, there are two types of conversion in the Data first implicit types conversion and the second is explicit data type conversion. In implicit type conversion Server can automatically convert the data type from one type to another (i.e., VARCHAR TO CHAR and INT TO FLOAT) but in explicit data type conversion it can be done by the user side.