New! Explore our Programming Academy and AI Tutor - learn to code from scratch, free to start. Explore Now
Programming› MySQL› Functions, Dates and Text

Functions, Dates and Text

💻 MySQL

Use built-in functions for formatting, dates and text so reports read well.

Lesson 6 of 9 Tutorial
0 of 9 completed

MySQL can format and transform values before they reach your application. Use it for reporting, not for business rules that belong in code.

By the end of this lesson you can:
  • 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;
Tip Format dates in the application when the user can change language or timezone. Use SQL formatting for fixed reports.

Practice

  1. Format created_at as 05 Nov 2026.
  2. Join first and last name with a space.
  3. 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

Exercise 1. Readable report Easy
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.
MySQL exercise-1.txt
Expected Output
A clean report with no NULL cells.

Lesson Nav

Lesson sections