SQL Server GETDATE() Function

Definition and Usage

The GETDATE() function returns the current date and time from SQL Server.

Syntax

GETDATE()

Instance

Example 1

Use the following SELECT statement:

SELECT GETDATE() AS CurrentDateTime

Result:

CurrentDateTime
2008-12-29 16:25:46.635

Note:The time part is accurate to milliseconds.

Example 2

The following SQL creates the "Orders" table with a datetime column (OrderDate):

CREATE TABLE Orders 
(
OrderId int NOT NULL PRIMARY KEY,
ProductName varchar(50) NOT NULL,
OrderDate datetime NOT NULL DEFAULT GETDATE()
)

Please note that OrderDate specifies GETDATE() as the default value. As a result, when you insert a new row in the table, the current date and time are automatically inserted into the column.

Now, we want to insert a record into the "Orders" table:

INSERT INTO Orders (ProductName) VALUES ('Computer')

"Orders" table will be like this:

OrderId ProductName OrderDate
1 'Computer' 2008-12-29 16:25:46.635