{"id":4003,"date":"2025-06-07T09:33:22","date_gmt":"2025-06-07T09:33:22","guid":{"rendered":"https:\/\/w3buddy.com\/?post_type=cposts&#038;p=4003"},"modified":"2026-09-26T13:15:22","modified_gmt":"2026-09-26T07:45:22","slug":"sql-date-time-functions","status":"publish","type":"post","link":"https:\/\/w3buddy.com\/blog\/sql-date-time-functions\/","title":{"rendered":"SQL Date &amp; Time Functions"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">SQL provides powerful functions to handle dates and times \u2014 essential for filtering, formatting, and calculating time-based data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83d\udd39 <strong>Common Date Functions (DBMS-Agnostic Examples)<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>-- Get current date and time\nSELECT CURRENT_DATE;        -- PostgreSQL \/ MySQL\nSELECT GETDATE();           -- SQL Server\n\n-- Extract parts of a date\nSELECT EXTRACT(YEAR FROM order_date) FROM orders;      -- PostgreSQL\nSELECT YEAR(order_date) FROM orders;                   -- MySQL \/ SQL Server\n\n-- Add or subtract dates\nSELECT order_date + INTERVAL '7 day' FROM orders;      -- PostgreSQL\nSELECT DATE_ADD(order_date, INTERVAL 7 DAY) FROM orders;  -- MySQL\nSELECT DATEADD(DAY, 7, order_date) FROM orders;         -- SQL Server\n\n-- Difference between dates\nSELECT DATEDIFF(CURDATE(), order_date);     -- MySQL\nSELECT DATEDIFF(DAY, order_date, GETDATE());-- SQL Server\n\n-- Format dates\nSELECT TO_CHAR(order_date, 'YYYY-MM-DD') FROM orders; -- PostgreSQL\nSELECT DATE_FORMAT(order_date, '%Y-%m-%d') FROM orders; -- MySQL\nSELECT FORMAT(order_date, 'yyyy-MM-dd') FROM orders;  -- SQL Server<\/code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83d\udd39 <strong>Examples<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>-- Find orders placed in last 30 days\nSELECT * FROM orders\nWHERE order_date >= CURRENT_DATE - INTERVAL '30 day';  -- PostgreSQL\n\n-- Find orders placed in a specific month\nSELECT * FROM orders\nWHERE MONTH(order_date) = 5;  -- May<\/code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83d\udd39 <strong>Key Date Functions by DBMS<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table has-small-font-size\"><table><thead><tr><th>Function<\/th><th>MySQL<\/th><th>PostgreSQL<\/th><th>SQL Server<\/th><\/tr><\/thead><tbody><tr><td>Current Date<\/td><td><code>CURDATE()<\/code><\/td><td><code>CURRENT_DATE<\/code><\/td><td><code>GETDATE()<\/code><\/td><\/tr><tr><td>Extract Year<\/td><td><code>YEAR(date_col)<\/code><\/td><td><code>EXTRACT(YEAR FROM \u2026)<\/code><\/td><td><code>YEAR(date_col)<\/code><\/td><\/tr><tr><td>Add Days<\/td><td><code>DATE_ADD()<\/code><\/td><td><code>+ INTERVAL<\/code> or <code>DATE +<\/code><\/td><td><code>DATEADD()<\/code><\/td><\/tr><tr><td>Date Difference<\/td><td><code>DATEDIFF()<\/code><\/td><td><code>AGE()<\/code><\/td><td><code>DATEDIFF()<\/code><\/td><\/tr><tr><td>Format Date<\/td><td><code>DATE_FORMAT()<\/code><\/td><td><code>TO_CHAR()<\/code><\/td><td><code>FORMAT()<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83e\udde0 <strong>Quick Recap<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table has-small-font-size\"><table><thead><tr><th>Task<\/th><th>Function Example<\/th><\/tr><\/thead><tbody><tr><td>Current Date\/Time<\/td><td><code>CURRENT_DATE<\/code>, <code>GETDATE()<\/code><\/td><\/tr><tr><td>Extract Date Part<\/td><td><code>YEAR(order_date)<\/code><\/td><\/tr><tr><td>Add Days<\/td><td><code>DATE_ADD(order_date, INTERVAL 7 DAY)<\/code><\/td><\/tr><tr><td>Date Difference<\/td><td><code>DATEDIFF()<\/code><\/td><\/tr><tr><td>Format Date<\/td><td><code>TO_CHAR()<\/code>, <code>DATE_FORMAT()<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83d\udca1 Use the right function depending on your DBMS, and always format or extract dates smartly for reports and filters.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>SQL provides powerful functions to handle dates and times \u2014 essential for filtering, formatting, and calculating time-based data. \ud83d\udd39 Common Date Functions (DBMS-Agnostic Examples) \ud83d\udd39 Examples \ud83d\udd39 Key Date Functions by DBMS Function MySQL PostgreSQL SQL Server Current Date CURDATE() CURRENT_DATE GETDATE() Extract Year YEAR(date_col) EXTRACT(YEAR FROM \u2026) YEAR(date_col) Add Days DATE_ADD() + INTERVAL or [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"googlesitekit_rrm_CAowu461DA:productID":"","footnotes":""},"categories":[1225,979],"tags":[],"class_list":["post-4003","post","type-post","status-publish","format-standard","hentry","category-database","category-sql"],"_links":{"self":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/posts\/4003","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/comments?post=4003"}],"version-history":[{"count":1,"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/posts\/4003\/revisions"}],"predecessor-version":[{"id":4004,"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/posts\/4003\/revisions\/4004"}],"wp:attachment":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/media?parent=4003"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/categories?post=4003"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/tags?post=4003"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}