Di MariaDB, kita dapat menggunakan LAST_DAY()
berfungsi untuk mengembalikan hari terakhir bulan tertentu.
Ini bisa menjadi hari terakhir bulan ini, atau hari terakhir bulan itu berdasarkan tanggal yang kami tentukan.
Contoh
SELECT LAST_DAY('2030-07-15');
Hasil:
2030-07-31
Dalam hal ini, bulan yang ditentukan (Juli) memiliki 31 hari, jadi kami mendapatkan tanggal 31 bulan itu.
Ini dia dengan berbagai bulan dalam setahun:
SELECT
LAST_DAY('2030-01-15') AS "Jan",
LAST_DAY('2030-02-15') AS "Feb",
LAST_DAY('2030-03-15') AS "Mar",
LAST_DAY('2030-04-15') AS "Apr",
LAST_DAY('2030-05-15') AS "May",
LAST_DAY('2030-06-15') AS "Jun",
LAST_DAY('2030-07-15') AS "Jul",
LAST_DAY('2030-08-15') AS "Aug",
LAST_DAY('2030-09-15') AS "Sep",
LAST_DAY('2030-10-15') AS "Oct",
LAST_DAY('2030-11-15') AS "Nov",
LAST_DAY('2030-12-15') AS "Dec";
Hasil:
+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+ | Jan | Feb | Mar | Apr | May | Jun | Jul | Aug | Sep | Oct | Nov | Dec | +------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+ | 2030-01-31 | 2030-02-28 | 2030-03-31 | 2030-04-30 | 2030-05-31 | 2030-06-30 | 2030-07-31 | 2030-08-31 | 2030-09-30 | 2030-10-31 | 2030-11-30 | 2030-12-31 | +------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+------------+
Contoh Basis Data
Berikut adalah contoh yang menggunakan tanggal dari database:
SELECT
rental_date,
CAST(LAST_DAY(rental_date) AS DATE) AS "End of Month"
FROM rental WHERE customer_id = 459
LIMIT 10;
Hasil:
+---------------------+--------------+ | rental_date | End of Month | +---------------------+--------------+ | 2005-05-24 22:54:33 | 2005-05-31 | | 2005-06-17 02:50:51 | 2005-06-30 | | 2005-06-17 09:38:22 | 2005-06-30 | | 2005-06-17 16:40:33 | 2005-06-30 | | 2005-06-20 02:39:21 | 2005-06-30 | | 2005-06-20 12:35:44 | 2005-06-30 | | 2005-06-20 12:42:00 | 2005-06-30 | | 2005-06-21 02:39:44 | 2005-06-30 | | 2005-07-06 00:22:29 | 2005-07-31 | | 2005-07-08 02:51:23 | 2005-07-31 | +---------------------+--------------+
Dalam hal ini saya juga menggunakan CAST()
berfungsi untuk mentransmisikan datetime
nilai ke date
nilai.