0% found this document useful (0 votes)
15 views7 pages

DBMSpractical5 (A BC)

Uploaded by

shizaqazi1622
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views7 pages

DBMSpractical5 (A BC)

Uploaded by

shizaqazi1622
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Practical No.

5 (A, B & C)

(A)Date Functions:

Table: employees

CREATE TABLE employees (

emp_id INT PRIMARY KEY,

emp_name VARCHAR(50),

hire_date DATE,

birth_date DATE,

last_promotion_date DATE

);

Insert Values into a Table:

Emp_name, hire_date, birth_date, last_promotion_date)

VALUES

(1, ‘Alice’, ‘2021-03-15’, ‘1990-06-25’, ‘2023-01-10’),

(2, ‘Bob’, ‘2019-08-01’, ‘1985-12-10’, ‘2022-05-12’),

(3, ‘Charlie’, ‘2020-11-22’, ‘1992-03-08’, ‘2023-06-19’),

(4, ‘Diana’, ‘2022-02-10’, ‘1988-09-19’, ‘2023-02-28’),

(5, ‘Edward’, ‘2023-05-25’, ‘1995-07-14’, NULL);

Examples of Date Functions SQL Commands on this Table:

1. CURRENT_DATE / CURDATE(): Returns the current date.

Query:

SELECT CURRENT_DATE;

-- or
SELECT CURDATE();

2. CURRENT_TIME / CURTIME(): Returns the current time.

Query:

SELECT CURRENT_TIME;

-- or

SELECT CURTIME();

3. NOW(): Returns the current date and time.

Query:

SELECT NOW();

4. DATE(): Extracts the date part from a datetime expression.

Query:

SELECT DATE(NOW());

5. YEAR(), MONTH(), DAY(): Extracts the year, month, or day from a date.

Query:

SELECT emp_name, YEAR(birth_date) AS birth_year, MONTH(birth_date) AS


birth_month, DAY(birth_date) AS birth_day

FROM employees;

6. HOUR(), MINUTE(), SECOND(): Extracts the hour, minute, or second from a time or
datetime.

Query:
SELECT HOUR(NOW()) AS current_hour, MINUTE(NOW()) AS current_minute,
SECOND(NOW()) AS current_second;

7. DATEDIFF(): Returns the difference in days between two dates.

Query:

SELECT emp_name, hire_date, DATEDIFF(CURRENT_DATE, hire_date) AS


days_since_hired

FROM employees;

(B) String Functions

Sample Table: employees1

Creating the employees Table in SQL

Query:

CREATE TABLE employees1 (

Emp_id INT PRIMARY KEY,

Emp_name VARCHAR(50),

Position VARCHAR(50),

Department VARCHAR(50),

Email VARCHAR(100)

);

Query:

INSERT INTO employees1 (emp_id, emp_name, position, department, email)

VALUES

(1, ‘Alice Johnson’, ‘Manager’, ‘HR’, ‘[email protected]’),

(2, ‘Bob Smith’, ‘Developer’, ‘IT’, ‘[email protected]’),

(3, ‘Charlie Davis’, ‘Designer’, ‘Design’, ‘[email protected]’),

(4, ‘Diana Adams’, ‘Analyst’, ‘Finance’, ‘[email protected]’),


(5, ‘Edward Collins’, ‘Intern’, ‘IT’, ‘[email protected]’);

1. UPPER() and LOWER(): Converts the string to uppercase or lowercase.

Query:

SELECT emp_name, UPPER(position) AS position_upper, LOWER(department) AS


department_lower

FROM employees1;

2. LENGTH(): Returns the length of a string.

Query:

SELECT emp_name, LENGTH(emp_name) AS name_length

FROM employees1;

3. CONCAT(): Concatenates two or more strings.

Query:

SELECT emp_name, CONCAT(emp_name, ‘ – ‘, position) AS full_title

FROM employees1;

4. SUBSTRING() / SUBSTR(): Extracts a substring from a string.

Query:

SELECT emp_name, SUBSTRING(emp_name, 1, 5) AS emp_name_part

FROM employees1;
5. REPLACE(): Replaces all occurrences of a substring within a string.

Query:

SELECT emp_name, REPLACE(email, ‘company.com’, ‘example.com’) AS


updated_email

FROM employees1;

6. REVERSE(): Reverses a string.

Query:

SELECT emp_name, REVERSE(emp_name) AS reversed_name from epmloyees1;

(C) Math Functions

Sample Table: sales

Creating the sales Table in SQL

Query:

CREATE TABLE sales (

sale_id INT PRIMARY KEY,

item VARCHAR(50),

quantity INT,

price_per_unit DECIMAL(10, 2),

discount DECIMAL(3, 2),

sale_date DATE

);

INSERT INTO sales (sale_id, item, quantity, price_per_unit, discount, sale_date)


VALUES

(1, ‘Laptop’, 2, 750, 0.1, ‘2024-01-15’),

(2, ‘Phone’, 5, 200, 0.05, ‘2024-02-20’),

(3, ‘Tablet’, 3, 300, 0.15, ‘2024-03-05’),

(4, ‘Monitor’, 4, 150, 0.2, ‘2024-04-12’),

(5, ‘Headphones’, 10, 50, 0.3, ‘2024-05-18’);

1. ABS(): Returns the absolute value of a number.

Query:

SELECT sale_id, item, ABS(-quantity) AS absolute_quantity

FROM sales;

2. CEIL() / CEILING() and FLOOR(): CEIL() rounds up to the nearest integer, while
FLOOR() rounds down.

Query:

SELECT sale_id, item, price_per_unit, CEIL(price_per_unit) AS ceil_price,


FLOOR(price_per_unit) AS floor_price

FROM sales;

3. ROUND(): Rounds a number to the specified number of decimal places.

Query:

SELECT sale_id, item, ROUND(price_per_unit * (1 – discount), 2) AS discounted_price

FROM sales;

4. POWER(): Returns the value of a number raised to the power of another.

Query:
SELECT sale_id, item, POWER(quantity, 2) AS quantity_squared

FROM sales;

5. SQRT(): Returns the square root of a number.

Query:

SELECT sale_id, item, SQRT(price_per_unit) AS price_sqrt

FROM sales;

6. GREATEST() and LEAST()

Query:

Returns the greatest or least value from a list of expressions.

SELECT sale_id, item, GREATEST(price_per_unit, 200, 500) AS max_value,


LEAST(price_per_unit, 200, 500) AS min_value

FROM sales;

You might also like