The FROM_DAYS() function in MySQL is used to convert a numeric day number (as returned by the TO_DAYS() function) into a DATE value. It is helpful when working with day numbers and you need to retrieve the corresponding calendar date.
Syntax:
FROM_DAYS(n)
nis the numeric day value (e.g., the number of days since the year 0).
Example 1: Basic Usage
SELECT FROM_DAYS(738001) AS Date;
Output:
Date
--------
2020-12-31
Example 2: Converting with TO_DAYS()
You can use TO_DAYS() to convert a date to a numeric day number and then use FROM_DAYS() to convert it back.
SELECT
TO_DAYS('2024-12-23') AS DayNumber,
FROM_DAYS(TO_DAYS('2024-12-23')) AS Date;
Output:
DayNumber Date
---------- ----------
738852 2024-12-23
Example 3: Using in a Query
CREATE TABLE Example (
id INT AUTO_INCREMENT PRIMARY KEY,
days INT
);
INSERT INTO Example (days) VALUES (738852), (738853), (738854);
SELECT id, days, FROM_DAYS(days) AS ConvertedDate FROM Example;
Output:
id days ConvertedDate
-- ------ -------------
1 738852 2024-12-23
2 738853 2024-12-24
3 738854 2024-12-25
Notes:
- The input
nshould be a valid numeric day number; otherwise, the function may returnNULL. - The result is affected by the
sql_modeand timezone settings only if you are dealing withDATETIMEorTIMESTAMPconversions. ForFROM_DAYS(), the output is purely aDATE.
No comments:
Post a Comment