Skip to content

Commit e8410ef

Browse files
added date time
1 parent 958b828 commit e8410ef

File tree

1 file changed

+48
-6
lines changed

1 file changed

+48
-6
lines changed

docs/Cheat-Sheets/SQL.md

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -558,17 +558,59 @@ SET TRANSACTION ISOLATION LEVEL READ COMMITTED; -- Example
558558

559559
## Date and Time Functions
560560

561+
561562
* `NOW()`, `CURRENT_TIMESTAMP`: Returns the current date and time.
562563
* `CURDATE()`, `CURRENT_DATE`: Returns the current date.
563564
* `CURTIME()`, `CURRENT_TIME`: Returns the current time.
564565
* `DATE(expression)`: Extracts the date part of a date or datetime expression.
565566
* `TIME(expression)`: Extracts the time part of a time or datetime expression.
566-
* `YEAR(date)`, `MONTH(date)`, `DAY(date)`: Extracts the year, month, or day.
567-
* `HOUR(time)`, `MINUTE(time)`, `SECOND(time)`: Extracts the hour, minute, or second.
568-
* `DATE_ADD(date, INTERVAL expr unit)`, `DATE_SUB(date, INTERVAL expr unit)`: Adds or subtracts a time interval.
569-
* `DATEDIFF(date1, date2)`: Returns the difference between two dates (in days).
570-
* `TIMESTAMPDIFF(unit, datetime1, datetime2)`: Returns the difference between two datetimes in a specified unit.
571-
* `DATE_FORMAT(date, format)`: Formats a date.
567+
* `YEAR(date)`, `MONTH(date)`, `DAY(date)`: Extracts the year, month, or day from a date.
568+
* `HOUR(time)`, `MINUTE(time)`, `SECOND(time)`: Extracts the hour, minute, or second from a time.
569+
* `EXTRACT(unit FROM datetime)`: Extracts a specific unit (e.g., `YEAR`, `MONTH`, `DAY`, `HOUR`, `MINUTE`, `SECOND`) from a date or timestamp.
570+
* `DATE_ADD(date, INTERVAL expr unit)`, `DATE_SUB(date, INTERVAL expr unit)`: Adds or subtracts a time interval (units: `DAY`, `WEEK`, `MONTH`, `YEAR`, etc.).
571+
* `DATEDIFF(date1, date2)`: Returns the difference between two dates (result unit varies by database, often days).
572+
* `TIMESTAMPDIFF(unit, datetime1, datetime2)`: Returns the difference between two datetimes in a specified unit (units: `MINUTE`, `HOUR`, `SECOND`, `DAY`, `MONTH`, `YEAR`).
573+
* `DATE_FORMAT(date, format)`: Formats a date according to the specified format string (format codes vary by database).
574+
* `DAYOFWEEK(date)`: Returns the day of the week as a number (e.g., 1=Sunday, 2=Monday...).
575+
* `WEEKOFYEAR(date)`: Returns the week number of the year.
576+
* `QUARTER(date)`: Returns the quarter of the year (1-4).
577+
* `WEEK(date)`: Returns the week number (behavior can vary based on mode/database).
578+
579+
```sql
580+
-- Get current date, time, timestamp
581+
SELECT CURRENT_DATE();
582+
SELECT CURRENT_TIME();
583+
SELECT CURRENT_TIMESTAMP();
584+
585+
-- Extract parts of a date/time
586+
SELECT DATE(CURRENT_TIMESTAMP());
587+
SELECT EXTRACT(YEAR FROM CURRENT_TIMESTAMP());
588+
SELECT EXTRACT(MONTH FROM CURRENT_TIMESTAMP());
589+
SELECT EXTRACT(DAY FROM CURRENT_TIMESTAMP());
590+
SELECT EXTRACT(HOUR FROM CURRENT_TIMESTAMP());
591+
SELECT EXTRACT(MINUTE FROM CURRENT_TIMESTAMP());
592+
SELECT EXTRACT(SECOND FROM CURRENT_TIMESTAMP());
593+
594+
-- Get week/day information
595+
SELECT DAYOFWEEK(CURRENT_TIMESTAMP()); -- 1=Sunday, 2=Monday, ..., 7=Saturday (common convention)
596+
SELECT WEEKOFYEAR(CURRENT_TIMESTAMP());
597+
SELECT QUARTER(CURRENT_DATE());
598+
SELECT WEEK(CURRENT_DATE()); -- Behavior might depend on mode
599+
600+
-- Date arithmetic
601+
SELECT DATE_ADD(CURRENT_DATE(), INTERVAL 4 DAY) AS four_days_from_today;
602+
SELECT DATE_ADD(CURRENT_DATE(), INTERVAL 1 DAY);
603+
SELECT DATE_ADD(CURRENT_DATE(), INTERVAL 2 WEEK);
604+
SELECT DATE_ADD(CURRENT_DATE(), INTERVAL 3 MONTH);
605+
SELECT DATE_ADD(CURRENT_DATE(), INTERVAL 4 YEAR);
606+
607+
-- Date differences
608+
SELECT DATEDIFF(CURRENT_DATE(), '2023-01-01'); -- Difference in days (example)
609+
SELECT TIMESTAMPDIFF(HOUR, '2023-01-01 10:00:00', CURRENT_TIMESTAMP()); -- Difference in hours
610+
611+
-- Formatting
612+
SELECT DATE_FORMAT(CURRENT_DATE(), '%Y-%m-%d'); -- Common format codes
613+
```
572614

573615
## Conditional Expressions
574616

0 commit comments

Comments
 (0)