How to retrieve the Local Date and Time in sql server ?
A common task in SQL Server is the retrieval of the local date and time from the server.
Usually, it is more appropriate for a database-linked application to obtain the time from the server than from the local machine, as this ensures that all users are using the same basis for the time.
If a user's local clock is set incorrectly, the use of the server's clock ensures that an invalid date or time is not stored.
Using the CURRENT_TIMESTAMP
SQL Server defines a value named CURRENT_TIMESTAMP.
This value can be included in aquery using a select statement or can be assigned to a DateTime variable to obtain the current local time. To demonstrate, try the following statements using a SQL Server Management Studio query window. The first will output the local time to the results window, the second will hold the time in a variable before printing the variable's value in the messages window.
NB: When the current date and time is retrieved in a select statement, the same date and time is included in every resultant row, even if the query takes a long time to run.
-- Select the current local time
SELECT CURRENT_TIMESTAMP
-- Print the current local time
DECLARE @CurrentTime DATETIME
SET @CurrentTime = CURRENT_TIMESTAMP
PRINT @CurrentTime
Using the GetDate Function
The GetDate function is equivalent to CURRENT_TIMESTAMP. When you execute the function, the local date and time from the SQL Server's internal clock is returned.
-- Select the current local time
SELECT getdate()
-- Print the current local time
DECLARE @CurrentTime DATETIME
SET @CurrentTime = getdate()
PRINT @CurrentTime
Retrieving the UTC Date and Time
The methods described in the previous section allow you to obtain the current local date and time from a SQL Server. If you are using the information to mark the time that a row was modified in a database, especially if you are working with a system that is used in many time zones, the local time may not be appropriate. Instead, you may want to record the Co-ordinated Universal Time (UTC).
This time is the same in every world time zone and can be easily converted to and from any user's local date and time. It is also unaffected by daylight savings time changes, which can appear to change the order of events in a database when activities occurs just before and after an adjustment to the local time.
To obtain the current UTC date and time, you can use the GetUtcDate function as follows:
-- Select the current local time
SELECT getutcdate()
-- Print the current local time
DECLARE @CurrentTime DATETIME
SET @CurrentTime = getutcdate()
PRINT @CurrentTime
A common task in SQL Server is the retrieval of the local date and time from the server.
Usually, it is more appropriate for a database-linked application to obtain the time from the server than from the local machine, as this ensures that all users are using the same basis for the time.
If a user's local clock is set incorrectly, the use of the server's clock ensures that an invalid date or time is not stored.
Using the CURRENT_TIMESTAMP
SQL Server defines a value named CURRENT_TIMESTAMP.
This value can be included in aquery using a select statement or can be assigned to a DateTime variable to obtain the current local time. To demonstrate, try the following statements using a SQL Server Management Studio query window. The first will output the local time to the results window, the second will hold the time in a variable before printing the variable's value in the messages window.
NB: When the current date and time is retrieved in a select statement, the same date and time is included in every resultant row, even if the query takes a long time to run.
-- Select the current local time
SELECT CURRENT_TIMESTAMP
-- Print the current local time
DECLARE @CurrentTime DATETIME
SET @CurrentTime = CURRENT_TIMESTAMP
PRINT @CurrentTime
Using the GetDate Function
The GetDate function is equivalent to CURRENT_TIMESTAMP. When you execute the function, the local date and time from the SQL Server's internal clock is returned.
-- Select the current local time
SELECT getdate()
-- Print the current local time
DECLARE @CurrentTime DATETIME
SET @CurrentTime = getdate()
PRINT @CurrentTime
Retrieving the UTC Date and Time
The methods described in the previous section allow you to obtain the current local date and time from a SQL Server. If you are using the information to mark the time that a row was modified in a database, especially if you are working with a system that is used in many time zones, the local time may not be appropriate. Instead, you may want to record the Co-ordinated Universal Time (UTC).
This time is the same in every world time zone and can be easily converted to and from any user's local date and time. It is also unaffected by daylight savings time changes, which can appear to change the order of events in a database when activities occurs just before and after an adjustment to the local time.
To obtain the current UTC date and time, you can use the GetUtcDate function as follows:
-- Select the current local time
SELECT getutcdate()
-- Print the current local time
DECLARE @CurrentTime DATETIME
SET @CurrentTime = getutcdate()
PRINT @CurrentTime
Comments
Post a Comment