Functions, Dates and Text
💻 MySQLUse built-in functions for formatting, dates and text so reports read well.
MySQL can format and transform values before they reach your application. Use it for reporting, not for business rules that belong in code.
- Format dates with DATE_FORMAT.
- Use string functions.
- Handle NULL with COALESCE.
Dates
NOW() returns the current date and time. DATE_FORMAT presents it in a readable shape.
SELECT id,
title,
DATE_FORMAT(created_at, '%d %M %Y') AS created
FROM courses;
SELECT DATEDIFF(NOW(), created_at) AS days_old
FROM courses;Text Functions
Join, shorten and clean text in the query when it saves work in the application.
SELECT CONCAT(first_name, ' ', last_name) AS full_name,
UPPER(level) AS level,
LEFT(description, 80) AS teaser
FROM students
JOIN courses ON courses.id = students.course_id;NULL Handling
COALESCE returns the first non-NULL value. IFNULL does the same for two arguments.
SELECT name, COALESCE(phone, 'Not provided') AS phone
FROM students;Practice
- Format created_at as 05 Nov 2026.
- Join first and last name with a space.
- Show a placeholder when a phone number is missing.
Hands-on exercise
Write a query that returns:
1. Full name from first_name and last_name.
2. The level in uppercase.
3. created_at formatted as YYYY-MM-DD.
4. A phone column that shows 'None' when NULL.
Recap
Date and text functions make reports readable, and COALESCE removes NULL noise from output.
Example
SELECT id,
title,
DATE_FORMAT(created_at, '%d %M %Y') AS created
FROM courses;
SELECT DATEDIFF(NOW(), created_at) AS days_old
FROM courses;
Exercises
1. Full name from first_name and last_name.
2. The level in uppercase.
3. created_at formatted as YYYY-MM-DD.
4. A phone column that shows 'None' when NULL.
Expected Output
A clean report with no NULL cells.
Lesson Nav
MySQL Course
0/9 lessons completed
- 1 Connecting to MySQL Tutorial
- 2 Creating Databases and Tables Tutorial
- 3 Data Types and Constraints Tutorial
- 4 Reading and Writing Data Tutorial
- 5 Joins and Relationships in MySQL Tutorial
- 6 Functions, Dates and Text Tutorial
- 7 Views and Stored Procedures Tutorial
- 8 Users, Permissions and Backups Tutorial
- 9 Project: A Small School Database Project