{"appId":"sql-app","version":1551,"selectedAngularVersion":20,"item":{"id":"lesson-date-and-time-functions","conceptKey":"date-and-time-functions","subjectId":"subject-date-and-time-functions","title":"Date and Time Functions","summary":"Date and Time Functions is a practical SQL concept used to design, query, secure, operate, or analyze relational data correctly.","baseContent":"<h2>Date and Time Functions</h2><p>Date and Time Functions is a practical SQL concept used to design, query, secure, operate, or analyze relational data correctly.</p><h3>Example</h3><pre><code>SELECT UPPER(name), ROUND(price, 2), CURRENT_DATE FROM products;</code></pre><h3>Key point</h3><p>Use the smallest correct statement, test it with representative data, and verify constraints and performance before production use.</p><h3>Real-life example</h3><p>A sales manager needs a monthly report showing revenue, order counts, rankings, and changes over time without exporting raw data to a spreadsheet.</p><h3>Advanced example</h3><pre><code>BEGIN;\n-- Preview the exact target set first.\nSELECT id FROM orders WHERE status = 'pending';\n-- Apply the Date and Time Functions operation, verify affected rows, then commit.\nCOMMIT;</code></pre><h3>Expected result</h3><p>The query returns only the intended rows and columns, with deterministic ordering where order matters.</p><h3>Production check</h3><ul><li>Test with empty, duplicate, null, and boundary values.</li><li>Use a transaction for related writes.</li><li>Inspect the execution plan before adding an index.</li><li>Use parameterized queries for application input.</li></ul><h3>Continue with the PicoStore database</h3><p>This lesson reuses <strong>picostore</strong>. Relevant tables: <code>customers, products, orders, order_items</code>. Keep the starter rows from the Introduction lesson so results remain comparable.</p><h3>Another practical example</h3><pre><code>SELECT category,\n       COUNT(*) AS products,\n       ROUND(AVG(price), 2) AS average_price,\n       SUM(stock) AS units_available\nFROM products\nGROUP BY category\nHAVING SUM(stock) &gt; 0;</code></pre><h3>Check the result</h3><p>Run the verification query, compare the returned rows with the starter data, and explain why every included or excluded row is correct.</p><section data-sql-reference-catalog=\"true\"><h2>SQL reference catalog: Date and Time Functions</h2><p>This catalog covers the practical public SQL surface. Check the exact server-version documentation before using a vendor-specific item.</p><h3>Portable/current values</h3><ul><li><code>CURRENT_DATE</code> — current date</li><li><code>CURRENT_TIME</code> — current time</li><li><code>CURRENT_TIMESTAMP</code> — current timestamp</li><li><code>LOCALTIME</code> — local time</li><li><code>LOCALTIMESTAMP</code> — local timestamp</li><li><code>EXTRACT</code> — extract year, month, day, and other fields</li></ul><h3>MySQL date/time functions</h3><ul><li><code>NOW / SYSDATE</code> — current date and time</li><li><code>CURDATE / CURTIME</code> — current date or time</li><li><code>DATE</code> — date portion</li><li><code>TIME</code> — time portion</li><li><code>YEAR / MONTH / DAY</code> — individual date fields</li><li><code>DATE_ADD / DATE_SUB</code> — date arithmetic</li><li><code>DATEDIFF</code> — days between dates</li><li><code>TIMESTAMPDIFF</code> — difference in selected unit</li><li><code>DATE_FORMAT</code> — format date as text</li><li><code>STR_TO_DATE</code> — parse text as date</li><li><code>LAST_DAY</code> — last date of month</li><li><code>DAYOFWEEK / WEEKDAY / WEEK</code> — calendar helpers</li><li><code>UNIX_TIMESTAMP / FROM_UNIXTIME</code> — Unix time conversion</li><li><code>CONVERT_TZ</code> — timezone conversion</li></ul><h3>PostgreSQL date/time functions</h3><ul><li><code>NOW / TRANSACTION_TIMESTAMP</code> — transaction start timestamp</li><li><code>STATEMENT_TIMESTAMP</code> — statement start timestamp</li><li><code>CLOCK_TIMESTAMP</code> — actual changing clock time</li><li><code>DATE_TRUNC</code> — truncate to a time unit</li><li><code>DATE_PART</code> — extract a field</li><li><code>AGE</code> — symbolic interval</li><li><code>MAKE_DATE / MAKE_TIME / MAKE_TIMESTAMP</code> — construct values</li><li><code>TO_DATE / TO_TIMESTAMP</code> — parse formatted text</li><li><code>TO_CHAR</code> — format date or number</li><li><code>AT TIME ZONE</code> — timezone conversion</li><li><code>JUSTIFY_DAYS / JUSTIFY_HOURS</code> — normalize intervals</li><li><code>GENERATE_SERIES</code> — generate date/time rows</li></ul></section><section data-nonversioned-curriculum=\"1\"><h3>Easy example</h3><p>Start with a small customer table and retrieve active customers in a predictable order.</p><pre><code>SELECT customer_id, name, email\nFROM customers\nWHERE status = 'active'\nORDER BY name;</code></pre><h3>How to verify the easy example</h3><ul><li>Run it with representative input.</li><li>Confirm the expected output.</li><li>Try one missing, invalid, or boundary value.</li></ul><h3>Advanced example</h3><p>Use a CTE and a window function to rank customer revenue while keeping the query readable and testable.</p><pre><code>WITH customer_revenue AS (\n  SELECT customer_id, SUM(total_amount) AS revenue\n  FROM orders\n  WHERE order_status = 'completed'\n  GROUP BY customer_id\n)\nSELECT customer_id, revenue,\n       DENSE_RANK() OVER (ORDER BY revenue DESC) AS revenue_rank\nFROM customer_revenue\nORDER BY revenue_rank, customer_id;</code></pre><h3>Advanced review</h3><ul><li>Explain the tradeoffs and assumptions.</li><li>Test failure, scale, security, and recovery behavior.</li><li>Capture evidence from tests, execution plans, logs, or review output.</li></ul><h3>Additional practical guidance</h3><div>Function names and interval syntax differ. PostgreSQL commonly uses date_trunc and INTERVAL; MySQL commonly uses DATE_FORMAT and DATE_ADD.</div></section>","detailId":"lesson-date-and-time-functions-39347c02","versions":[],"isActive":true,"detailIsActive":true,"lessonVersions":[],"selectedVersion":20,"content":"","updatedAt":"2026-08-01T09:43:04.474Z","details":[{"id":"lesson-date-and-time-functions","conceptKey":"date-and-time-functions","subjectId":"subject-date-and-time-functions","title":"Date and Time Functions","summary":"Date and Time Functions is a practical SQL concept used to design, query, secure, operate, or analyze relational data correctly.","baseContent":"<h2>Date and Time Functions</h2><p>Date and Time Functions is a practical SQL concept used to design, query, secure, operate, or analyze relational data correctly.</p><h3>Example</h3><pre><code>SELECT UPPER(name), ROUND(price, 2), CURRENT_DATE FROM products;</code></pre><h3>Key point</h3><p>Use the smallest correct statement, test it with representative data, and verify constraints and performance before production use.</p><h3>Real-life example</h3><p>A sales manager needs a monthly report showing revenue, order counts, rankings, and changes over time without exporting raw data to a spreadsheet.</p><h3>Advanced example</h3><pre><code>BEGIN;\n-- Preview the exact target set first.\nSELECT id FROM orders WHERE status = 'pending';\n-- Apply the Date and Time Functions operation, verify affected rows, then commit.\nCOMMIT;</code></pre><h3>Expected result</h3><p>The query returns only the intended rows and columns, with deterministic ordering where order matters.</p><h3>Production check</h3><ul><li>Test with empty, duplicate, null, and boundary values.</li><li>Use a transaction for related writes.</li><li>Inspect the execution plan before adding an index.</li><li>Use parameterized queries for application input.</li></ul><h3>Continue with the PicoStore database</h3><p>This lesson reuses <strong>picostore</strong>. Relevant tables: <code>customers, products, orders, order_items</code>. Keep the starter rows from the Introduction lesson so results remain comparable.</p><h3>Another practical example</h3><pre><code>SELECT category,\n       COUNT(*) AS products,\n       ROUND(AVG(price), 2) AS average_price,\n       SUM(stock) AS units_available\nFROM products\nGROUP BY category\nHAVING SUM(stock) &gt; 0;</code></pre><h3>Check the result</h3><p>Run the verification query, compare the returned rows with the starter data, and explain why every included or excluded row is correct.</p><section data-sql-reference-catalog=\"true\"><h2>SQL reference catalog: Date and Time Functions</h2><p>This catalog covers the practical public SQL surface. Check the exact server-version documentation before using a vendor-specific item.</p><h3>Portable/current values</h3><ul><li><code>CURRENT_DATE</code> — current date</li><li><code>CURRENT_TIME</code> — current time</li><li><code>CURRENT_TIMESTAMP</code> — current timestamp</li><li><code>LOCALTIME</code> — local time</li><li><code>LOCALTIMESTAMP</code> — local timestamp</li><li><code>EXTRACT</code> — extract year, month, day, and other fields</li></ul><h3>MySQL date/time functions</h3><ul><li><code>NOW / SYSDATE</code> — current date and time</li><li><code>CURDATE / CURTIME</code> — current date or time</li><li><code>DATE</code> — date portion</li><li><code>TIME</code> — time portion</li><li><code>YEAR / MONTH / DAY</code> — individual date fields</li><li><code>DATE_ADD / DATE_SUB</code> — date arithmetic</li><li><code>DATEDIFF</code> — days between dates</li><li><code>TIMESTAMPDIFF</code> — difference in selected unit</li><li><code>DATE_FORMAT</code> — format date as text</li><li><code>STR_TO_DATE</code> — parse text as date</li><li><code>LAST_DAY</code> — last date of month</li><li><code>DAYOFWEEK / WEEKDAY / WEEK</code> — calendar helpers</li><li><code>UNIX_TIMESTAMP / FROM_UNIXTIME</code> — Unix time conversion</li><li><code>CONVERT_TZ</code> — timezone conversion</li></ul><h3>PostgreSQL date/time functions</h3><ul><li><code>NOW / TRANSACTION_TIMESTAMP</code> — transaction start timestamp</li><li><code>STATEMENT_TIMESTAMP</code> — statement start timestamp</li><li><code>CLOCK_TIMESTAMP</code> — actual changing clock time</li><li><code>DATE_TRUNC</code> — truncate to a time unit</li><li><code>DATE_PART</code> — extract a field</li><li><code>AGE</code> — symbolic interval</li><li><code>MAKE_DATE / MAKE_TIME / MAKE_TIMESTAMP</code> — construct values</li><li><code>TO_DATE / TO_TIMESTAMP</code> — parse formatted text</li><li><code>TO_CHAR</code> — format date or number</li><li><code>AT TIME ZONE</code> — timezone conversion</li><li><code>JUSTIFY_DAYS / JUSTIFY_HOURS</code> — normalize intervals</li><li><code>GENERATE_SERIES</code> — generate date/time rows</li></ul></section><section data-nonversioned-curriculum=\"1\"><h3>Easy example</h3><p>Start with a small customer table and retrieve active customers in a predictable order.</p><pre><code>SELECT customer_id, name, email\nFROM customers\nWHERE status = 'active'\nORDER BY name;</code></pre><h3>How to verify the easy example</h3><ul><li>Run it with representative input.</li><li>Confirm the expected output.</li><li>Try one missing, invalid, or boundary value.</li></ul><h3>Advanced example</h3><p>Use a CTE and a window function to rank customer revenue while keeping the query readable and testable.</p><pre><code>WITH customer_revenue AS (\n  SELECT customer_id, SUM(total_amount) AS revenue\n  FROM orders\n  WHERE order_status = 'completed'\n  GROUP BY customer_id\n)\nSELECT customer_id, revenue,\n       DENSE_RANK() OVER (ORDER BY revenue DESC) AS revenue_rank\nFROM customer_revenue\nORDER BY revenue_rank, customer_id;</code></pre><h3>Advanced review</h3><ul><li>Explain the tradeoffs and assumptions.</li><li>Test failure, scale, security, and recovery behavior.</li><li>Capture evidence from tests, execution plans, logs, or review output.</li></ul><h3>Additional practical guidance</h3><div>Function names and interval syntax differ. PostgreSQL commonly uses date_trunc and INTERVAL; MySQL commonly uses DATE_FORMAT and DATE_ADD.</div></section>","detailId":"lesson-date-and-time-functions-39347c02","versions":[],"isActive":true,"detailIsActive":true,"lessonVersions":[],"selectedVersion":20,"content":"","updatedAt":"2026-08-01T09:43:04.474Z"}]}}