Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Date Functions in SQL

Date Functions in SQL

? Date Functions in SQL

Date functions help you work with dates and times — extracting parts, calculating differences, formatting, etc. Exact functions vary by SQL dialect, but here are the common ones found in most databases (MySQL, SQL Server, PostgreSQL, Oracle).


? Common Date Functions

FunctionDescriptionExampleOutput Example
NOW() / CURRENT_TIMESTAMPReturns the current date and timeSELECT NOW();2025-06-06 11:30:45
CURDATE() / CURRENT_DATEReturns the current date (no time)SELECT CURDATE();2025-06-06
DATE()Extract date part from datetimeSELECT DATE('2025-06-06 11:30');2025-06-06
YEAR()Extract year from dateSELECT YEAR('2025-06-06');2025
MONTH()Extract month number (1-12)SELECT MONTH('2025-06-06');6
DAY() / DAYOFMONTH()Extract day of monthSELECT DAY('2025-06-06');6
HOUR()Extract hour from timeSELECT HOUR('11:30:45');11
MINUTE()Extract minute from timeSELECT MINUTE('11:30:45');30
SECOND()Extract second from timeSELECT SECOND('11:30:45');45
DATEDIFF(date1, date2)Difference in days between two datesSELECT DATEDIFF('2025-06-06', '2025-06-01');5
DATEADD() / ADDDATE()Add interval to a date (days, months, etc.)SELECT DATEADD(day, 7, '2025-06-01'); (SQL Server)SELECT ADDDATE('2025-06-01', INTERVAL 7 DAY); (MySQL)2025-06-08
FORMAT()Format date/time into stringSELECT FORMAT(NOW(), 'yyyy-MM-dd'); (SQL Server)2025-06-06

? Example Queries

Get current date and time:

SELECT NOW();

Find age in days between two dates:

SELECT DATEDIFF('2025-06-06', '2025-01-01') AS days_diff;

Add 10 days to a date:

-- MySQLSELECT DATE_ADD('2025-06-01', INTERVAL 10 DAY);-- SQL ServerSELECT DATEADD(day, 10, '2025-06-01');

Extract year, month, day:

SELECT YEAR('2025-06-06'), MONTH('2025-06-06'), DAY('2025-06-06');

If you want, I can provide date function examples specific to your database (MySQL, PostgreSQL, SQL Server, Oracle), or explain advanced date/time operations!

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql