You are on page 1of 2

BCSE-510L

PRACTICAL-9
Aim: To Describe MySQL Conversion Functions.

CREATING A SAMPLE TABLE:-

-- Creating a sample employee table

CREATE TABLE employee (

employee_id INT,

employee_name VARCHAR(50),

birthdate DATE,

salary DECIMAL(10, 2),

hire_date DATETIME,

PRIMARY KEY (employee_id)

);

-- Inserting sample data into the employee table

INSERT INTO employee (employee_id, employee_name, birthdate, salary, hire_date)

VALUES

(1, 'John Doe', '1990-05-15', 50000.00, '2020-01-15 08:00:00'),

(2, 'Jane Smith', '1985-08-20', 60000.00, '2018-03-10 10:30:00'),

(3, 'Bob Johnson', '1995-02-28', 55000.50, '2019-11-05 09:15:00');


BCSE-510L

-- Using conversion functions on the employee table

1. CAST() function to convert salary to integer

SELECT employee_id, employee_name, CAST(salary AS UNSIGNED) AS


salary_integer FROM employee;

2. IFNULL() function to handle potential null values in the birthdate column

SELECT employee_id, employee_name, IFNULL(birthdate, 'Not available') AS


birthdate_or_default FROM employee;

You might also like