Data type conversion in SQL

Data type conversion

  1. Implicit data type conversion
  2. Explicit data type conversion

Implicit Data Type Conversion

  • In expression oracle server Automatically convert  the following

implicit-conversion2

Explicit Data Type Conversion

  • In following case oracle server can not convert

Explicit conversion in SQL

Using to_char with date

 TO_CHAR(Date,'format_model')

The format model:

  1. Must be enclosed by single quotation marks.

It is case sensitive.
format-model

Query:1.Display sysdate in DD/MM/YYYY format

SELECT TO_CHAR(sysdate,'dd/mm/yyyy') FROM dual;

sysdate

Query:2. list all the employs to join a organization in the month of dec?

SELECT First_name, Hire_date
FROM employees
WHERE TO_CHAR (hire_date,'mon')='dec'

dec

Query: 3.Display employees who joined  on sunday?

SELECT first_name,hire_date FROM employees WHERE TO_CHAR(hire_date,'DY')='SUN';

sun

Using the TO_CHAR Function with Numbers

TO_CHAR(number, 'format_model')
  • These are some of the format elements that you can use with the TO_CHAR function to display a number value as a character

format-model-for-number

i.e

Query:

SELECT TO_CHAR(salary, '$99,999.00') SALARY
FROM employees
WHERE last_name = 'Ernst';

number-sal

Leave a Reply

Your email address will not be published. Required fields are marked *