Back to Library

GETDATE() in SQL

Returns the current date and time of the database server.

Syntax

GETDATE() 
-- Some databases use: CURRENT_TIMESTAMP or NOW()
Return type
DATETIME/TIMESTAMP

GETDATE() Function Example

SELECT GETDATE(); 
-- Output: '2024-12-26 14:30:45.123'

What is GETDATE() in SQL?

The GETDATE() function in SQL returns the current date and time based on the database server’s system clock. It is commonly used for timestamping records, logging events, and scheduling tasks. The function returns a datetime value that includes both the date (YYYY-MM-DD) and the time (HH:MI:SS).

GETDATE() is primarily used in SQL Server. For similar functionality in other databases:

  • MySQL & PostgreSQL use NOW().

  • Oracle uses SYSDATE().

  • SQLite uses DATETIME('now').

This function is widely applied in audit trails, real-time data processing, and transaction logging.

Parameters:

  • None - takes no parameters

Example Use Cases:

-- Log transaction time

INSERT INTO transactions (transaction_date) VALUES (GETDATE()); 
-- Output:
1 row(s) affected
transaction_id    transaction_date
--------------   -----------------
1001             2024-12-28 14:30:25.447

-- Find records from today 

SELECT * FROM orders WHERE order_date = CAST(GETDATE() AS DATE); 
-- Output:
order_id    customer_id    order_date    total_amount    status
--------    -----------    ----------    ------------    -------
5001        1234          2024-12-28    299.99          Pending
5002        2345          2024-12-28    145.50          Processing
5003        3456          2024-12-28    789.95          Confirmed
5004        4567          2024-12-28    249.99          Pending
5005        5678          2024-12-28    567.75          Processing

-- Calculate time differences 

SELECT order_id, DATEDIFF(minute, order_time, GETDATE()) AS minutes_elapsed FROM orders;
-- Output:
order_id    order_time              minutes_elapsed
--------    ----------              ---------------
5001        2024-12-28 09:15:00    315
5002        2024-12-28 11:30:00    180
5003        2024-12-28 12:45:00    105
5004        2024-12-28 13:20:00    70
5005        2024-12-28 14:00:00    30

Notes:

  • Behavior: Returns server timestamp, not client time

  • Performance Considerations: Non-deterministic function

  • Version Info: Core function with different names across DBMS

  • Deprecated/Recommended Alternatives: SYSDATETIME() in some DBMS for higher precision

Error Handling:

  • Error: Rarely throws errors as it's a system function

  • Recommendation: Use TRY_CONVERT for type conversions


Supported Databases:

DBMS
Function / Syntax
Example
Behavior with NULL
MySQL

NOW(), CURRENT_TIMESTAMP()

NOW()

Never NULL

PostgreSQL

CURRENT_TIMESTAMP, NOW()

CURRENT_TIMESTAMP

Never NULL

SQL Server

GETDATE(), CURRENT_TIMESTAMP

GETDATE()

Never NULL

SQLite

datetime('now')

datetime('now')

Never NULL

BigQuery

CURRENT_TIMESTAMP()

CURRENT_TIMESTAMP()

Never NULL

Snowflake

CURRENT_TIMESTAMP()

CURRENT_TIMESTAMP()

Never NULL

Athena

CURRENT_TIMESTAMP

CURRENT_TIMESTAMP

Never NULL

Related Functions:

DATEFROMPARTS()

Creates a date value from specified year, month, and day integers.

Creates a date value from specified year, month, and day integers.

Creates a date value from specified year, month, and day integers.

Creates a date value from specified year, month, and day integers.

DATEFORMAT()

Converts a date/timestamp expression into a specified string format.

Converts a date/timestamp expression into a specified string format.

Converts a date/timestamp expression into a specified string format.

Converts a date/timestamp expression into a specified string format.

EOMONTH()

Returns the last day of the month for a specified date with an optional month offset.

Returns the last day of the month for a specified date with an optional month offset.

Returns the last day of the month for a specified date with an optional month offset.

Returns the last day of the month for a specified date with an optional month offset.

DAY()

Extracts the day of the month (1-31) from a date or timestamp expression.

Extracts the day of the month (1-31) from a date or timestamp expression.

Extracts the day of the month (1-31) from a date or timestamp expression.

Extracts the day of the month (1-31) from a date or timestamp expression.

MONTH()

Extracts the month value (1-12) from a date or timestamp expression.

Extracts the month value (1-12) from a date or timestamp expression.

Extracts the month value (1-12) from a date or timestamp expression.

Extracts the month value (1-12) from a date or timestamp expression.

YEAR()

Extracts the year value from a date or timestamp expression.

Extracts the year value from a date or timestamp expression.

Extracts the year value from a date or timestamp expression.

Extracts the year value from a date or timestamp expression.

DATEDIFF()

Calculates the difference between two dates in specified interval units.

Calculates the difference between two dates in specified interval units.

Calculates the difference between two dates in specified interval units.

Calculates the difference between two dates in specified interval units.

GETDATE()

Returns the current date and time of the database server.

Returns the current date and time of the database server.

Returns the current date and time of the database server.

Returns the current date and time of the database server.

One place for all your queries,
directly on your SQL editor