SQL Server GETDATE()
SQL Server GETDATE() Function
The SQL Server GETDATE()
function returns the current date and time from the system's clock. This function is useful for retrieving the current date and time in SQL Server.
Syntax
SELECT GETDATE();
The GETDATE()
function does not take any arguments.
Example SQL Server GETDATE() Function Queries
Let's look at some examples of SQL Server GETDATE()
function queries:
1. Basic GETDATE() Example
SELECT GETDATE() AS current_datetime;
This query returns the current date and time. The result will be:
current_datetime
-----------------------
2024-06-03 12:34:56.789
2. GETDATE() with Other Functions
SELECT GETDATE() AS current_datetime, DATEADD(day, 7, GETDATE()) AS next_week;
This query returns the current date and time along with the date and time for one week in the future. The result will be:
current_datetime next_week
----------------------- -----------------------
2024-06-03 12:34:56.789 2024-06-10 12:34:56.789
3. GETDATE() in a Table
CREATE TABLE orders (
id INT PRIMARY KEY,
order_date DATETIME DEFAULT GETDATE()
);
INSERT INTO orders (id) VALUES (1);
SELECT * FROM orders;
This example creates a table named orders
with a column that defaults to the current date and time. The result will be:
id order_date
--- -----------------------
1 2024-06-03 12:34:56.789
4. GETDATE() with a Variable
DECLARE @current_datetime DATETIME;
SET @current_datetime = GETDATE();
SELECT @current_datetime AS current_datetime;
This query uses a variable to store the current date and time and then returns it. The result will be:
current_datetime
-----------------------
2024-06-03 12:34:56.789
Full Example
Let's go through a complete example that includes creating a table, inserting data, and using the GETDATE()
function.
Step 1: Creating a Table
This step involves creating a new table named logs
to store some sample data with timestamps.
CREATE TABLE logs (
id INT PRIMARY KEY,
log_message VARCHAR(255),
log_time DATETIME DEFAULT GETDATE()
);
In this example, we create a table named logs
with columns for id
, log_message
, and log_time
.
Step 2: Inserting Data into the Table
This step involves inserting some sample data into the logs
table.
INSERT INTO logs (id, log_message) VALUES (1, 'Log entry 1');
INSERT INTO logs (id, log_message) VALUES (2, 'Log entry 2');
INSERT INTO logs (id, log_message) VALUES (3, 'Log entry 3');
Here, we insert data into the logs
table.
Step 3: Using the GETDATE() Function
This step involves using the GETDATE()
function to retrieve the current date and time for each log entry.
SELECT id, log_message, log_time
FROM logs;
This query retrieves the id
, log_message
, and log_time
for each row in the logs
table. The result will be:
id log_message log_time
--- ------------- -----------------------
1 Log entry 1 2024-06-03 12:34:56.789
2 Log entry 2 2024-06-03 12:34:56.789
3 Log entry 3 2024-06-03 12:34:56.789
Conclusion
The SQL Server GETDATE()
function is a powerful tool for retrieving the current date and time from the system's clock. Understanding how to use the GETDATE()
function and its syntax is essential for effective timestamp management and data processing in SQL Server.