Tip of the Day : Example Uses of the SYSDATETIME Date Function

Welcome to SQL Server Helper !!!

This site is intended for those who are beginning to use SQL Server as part of their day-to-day activities.  You will find in this site a collection of useful functions, triggers, stored procedures and tips and tricks related to SQL Server.

Should you have any comments or questions regarding this site or if you want to ask SQL Server-related questions, e-mail us here.

We hope we are able to help and we are glad to help!!!

SQL Server Tip of the Day - June 23, 2017

Example Uses of the SYSDATETIME Date Function

The SYSDATETIME date function, introduced in SQL Server 2008, returns a DATETIME2 value that contains the date and time of the computer on which the instance of SQL Server is running.  The SYSDATETIME date function have more fractional seconds precision than the GETDATE date function.  The syntax of the SYSDATETIME date function is as follows:


The SYSDATETIME date function is a non-deterministic function and view and expressions that reference this column cannot be indexed.

Here are a few uses of the SYSDATETIME date function:

Usage #1 : Log the Date and Time When a Row is Inserted in a Table

CREATE TABLE [dbo].[Transactions] (
    [TransactionID]        INT NOT NULL IDENTITY(1, 1) PRIMARY KEY,
    [AccountNumber]        VARCHAR(10),
    [Amount]               MONEY,
    [TransactionDate]      DATETIME DEFAULT ( SYSDATETIME() )

INSERT INTO [dbo].[Transactions] ( [AccountNumber], [Amount] )
VALUES ( '123-456-78', 1000.00 )

Usage #2 : Generate a Random Number


Usage #3 : Generate a Random Number Within a Range

DECLARE @MinValue     INT
DECLARE @MaxValue     INT

SET @MinValue = 51
SET @MaxValue = 100

SELECT (DATEPART(MS, SYSDATETIME()) % ( @MaxValue - @MinValue + 1)) + @MinValue AS [Random Number]

Usage #4 : Generate an Account Number, Reference Number or Tracking Number

-- Assuming [dbo].[Tracking] Table Contains All Existing Tracking Numbers
DECLARE @NewNumber        DECIMAL(10, 0)

SELECT @NewNumber = MAX([TrackingNumber]) + 1
FROM [dbo].[Tracking]
WHERE [TrackingNumber] > YEAR(SYSDATETIME()) * 1000000 + 
                         MONTH(SYSDATETIME()) * 10000 + 
                         DAY(SYSDATETIME()) * 100

IF @NewNumber IS NULL
    SET @NewNumber = YEAR(SYSDATETIME()) * 1000000 + 
                     MONTH(SYSDATETIME()) * 10000 + 
                     DAY(SYSDATETIME()) * 100 + 1

Usage #5 : Use as a Seed in the RAND Function


Usage #6 : Return a Random Number of Records

FROM [master].[dbo].[spt_values]

SQL Server 2012

SQL Server 2008

User-Defined Functions

Date Functions

A collection of useful user-defined functions that deal with dates.

String Functions

A collection of useful user-defined functions that deal with strings (varchar/char/nvarchar/nchar).

Tree Functions

A collection of useful user-defined functions that deal with tree or hierarchical design structures.

Table-Valued Functions

A collection of useful table-valued user-defined functions that can be used to join with other tables.

SQL Server Built-in Functions

A reference to all built-in functions available within SQL Server grouped into categories.

Tips and Tricks

A collection of useful SQL Server-related tips and tricks:

SQL Server Error Messages

A list of SQL Server error messages and for certain error messages, discusses ways on how to solve the error or work around them:

Frequently Asked Questions