0% found this document useful (0 votes)
4 views4 pages

Date Time Function MYSQL

Uploaded by

garako2050
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)
4 views4 pages

Date Time Function MYSQL

Uploaded by

garako2050
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/ 4

✅ Converting Oracle Date Functions to MySQL Equivalents

MySQL and Oracle use different functions for handling dates. Here is how you can replace
common Oracle date functions with MySQL equivalents:

🚀 1. TO_DATE() (Oracle) ➡️ STR_TO_DATE() (MySQL)


✅ Oracle:

SELECT TO_DATE('15-JUN-2012', 'DD-MON-YYYY') FROM DUAL;

✅ MySQL:

SELECT STR_TO_DATE('15-JUN-2012', '%d-%b-%Y');

• %d: Day (01, 15, 31)


• %b: Abbreviated month (JAN, FEB, JUN)
• %Y: 4-digit year (2023)

🚀 2. SYSDATE (Oracle) ➡️ NOW() or CURRENT_TIMESTAMP (MySQL)


✅ Oracle:

SELECT SYSDATE FROM DUAL;

✅ MySQL:

SELECT NOW(); -- Current date and time


SELECT CURRENT_TIMESTAMP; -- Current date and time (same as NOW)

🚀 3. TRUNC() (Oracle) ➡️ DATE() (MySQL)


• In Oracle, TRUNC() is used to remove the time part of a date.

✅ Oracle:

SELECT TRUNC(SYSDATE) FROM DUAL;

✅ MySQL:

SELECT DATE(NOW()); -- Removes time, keeps only date


🚀 4. TO_CHAR() (Oracle) ➡️ DATE_FORMAT() (MySQL)
• TO_CHAR() in Oracle is used to format dates or numbers as text.

✅ Oracle:

SELECT TO_CHAR(SYSDATE, 'YYYY-MM-DD HH24:MI:SS') FROM DUAL;

✅ MySQL:

SELECT DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%s');

• %Y: Year (4-digit)


• %m: Month (2-digit)
• %d: Day (2-digit)
• %H: Hour (24-hour)
• %i: Minutes
• %s: Seconds

🚀 5. ADD_MONTHS() (Oracle) ➡️ DATE_ADD() (MySQL)


• Adds a specified number of months to a date.

✅ Oracle:

SELECT ADD_MONTHS(SYSDATE, 3) FROM DUAL;

✅ MySQL:

SELECT DATE_ADD(NOW(), INTERVAL 3 MONTH);

🚀 6. LAST_DAY() (Oracle) ➡️ LAST_DAY() (MySQL)


• Gets the last day of the month.

✅ Oracle:

SELECT LAST_DAY(SYSDATE) FROM DUAL;

✅ MySQL:

SELECT LAST_DAY(NOW());
🚀 7. MONTHS_BETWEEN() (Oracle) ➡️ Custom Calculation
(MySQL)
• Oracle can directly calculate months between two dates.

✅ Oracle:

SELECT MONTHS_BETWEEN('2023-12-01', '2023-01-01') FROM DUAL;

✅ MySQL (Manual Calculation):

SELECT
(YEAR('2023-12-01') - YEAR('2023-01-01')) * 12 +
(MONTH('2023-12-01') - MONTH('2023-01-01')) AS Months_Between;

🚀 8. ROUND() with Dates (Oracle) ➡️ Custom Logic


(MySQL)
• In Oracle, ROUND() can round a date to the nearest day, month, or year.
• In MySQL, this must be done with logic.

✅ Oracle:

SELECT ROUND(SYSDATE, 'MONTH') FROM DUAL;

✅ MySQL (Rounding Date to Nearest Month):

SELECT
CASE
WHEN DAY(NOW()) >= 15 THEN DATE_FORMAT(NOW() + INTERVAL 1 MONTH, '%Y-
%m-01')
ELSE DATE_FORMAT(NOW(), '%Y-%m-01')
END AS Rounded_Month;

🚀 9. EXTRACT() (Oracle) ➡️ EXTRACT() (MySQL)


• Both databases support EXTRACT(), but syntax is slightly different.

✅ Oracle:

SELECT EXTRACT(YEAR FROM SYSDATE) FROM DUAL;

✅ MySQL:

SELECT EXTRACT(YEAR FROM NOW());


🚀 10. NEXT_DAY() (Oracle) ➡️ Custom Logic (MySQL)
• Oracle can directly find the next specified weekday.
• In MySQL, this requires a custom query.

✅ Oracle:

SELECT NEXT_DAY(SYSDATE, 'MONDAY') FROM DUAL;

✅ MySQL (Next Monday):

SELECT DATE_ADD(NOW(), INTERVAL (9 - WEEKDAY(NOW())) % 7 DAY) AS


Next_Monday;

✅ Would you like me to show a detailed cheat sheet of all


these Oracle to MySQL date conversions in a single table
(for quick reference)? 🚀

You might also like