SQL System Functions

System functions are built-in functions that perform operations based on the system, database, or user context. They provide information about the current session, database, or system settings.

General System Functions

DB_NAME() SQL Function

Returns the name of the current database or a specified database ID.

Syntax: DB_NAME ( [ database_id ] )

Description: If an integer is specified, DB_NAME() returns the database name corresponding to that database ID. If no integer is specified, it returns the name of the current database. If the specified ID does not have a name, this function returns NULL.

SELECT DB_NAME() AS CurrentDatabaseName;

USER_NAME() SQL Function

Returns the user ID associated with the specified user name or the user name associated with the specified user ID.

Syntax: USER_NAME ( [ user_id ] )

Description: If no argument is specified, USER_NAME() returns the name of the current user. If a user ID is specified, it returns the user name for that ID. If the specified ID does not exist, it returns NULL.

SELECT USER_NAME() AS CurrentUserName;

HOST_NAME() SQL Function

Returns the network login name of the client.

Syntax: HOST_NAME ( )

Description: HOST_NAME() returns the name of the workstation that is currently running the client application. This is not necessarily the computer name of the client.

SELECT HOST_NAME() AS ClientHostName;

SYSTEM_USER SQL Variable

Returns the login name of the user and the name of the server or database to which the user is connected.

Syntax: SYSTEM_USER

Description: This global variable contains the effective login name. For SQL Server logins, this is typically the login name. For Windows users, it is in the format 'DOMAIN\User'.

SELECT SYSTEM_USER AS ServerLoginInfo;

Metadata and Information Functions

@@ROWCOUNT SQL Variable

Returns the number of rows affected by the last statement.

Syntax: @@ROWCOUNT

Description: @@ROWCOUNT is a global variable that holds the number of rows affected by the previous Transact-SQL statement.

UPDATE Employees SET Salary = Salary * 1.10 WHERE Department = 'Sales';
SELECT @@ROWCOUNT AS RowsAffected;

@@SERVERNAME SQL Variable

Returns the name of the local server.

Syntax: @@SERVERNAME

Description: @@SERVERNAME returns the name of the server as it is registered in SQL Server. This can be the local server name or the fully qualified domain name (FQDN).

SELECT @@SERVERNAME AS ServerInstanceName;

OBJECT_ID() SQL Function

Returns the object identification number of a schema-scoped object.

Syntax: OBJECT_ID ( '[ schema_name. ] object_name' [ , 'object_type' ] )

Description: Returns the ID for a database object, such as a table, view, or stored procedure. If the object does not exist or the user does not have permission to view it, it returns NULL.

SELECT OBJECT_ID('Customers', 'U') AS CustomerTableID;

Date and Time System Functions

GETDATE() SQL Function

Returns the current database system timestamp.

Syntax: GETDATE ( )

Description: GETDATE() returns the current date and time as a datetime value. The value is the current date and time when the statement is executed.

SELECT GETDATE() AS CurrentSystemDateTime;

SYSDATETIME() SQL Function

Returns the date and time of the server. This value is a server-level value.

Syntax: SYSDATETIME ( )

Description: SYSDATETIME() returns a date and time value that is the current date and time of the computer where the SQL Server instance is running. The precision is up to the hundred-nanosecond.

SELECT SYSDATETIME() AS CurrentServerDateTimeWithHighPrecision;

Explore the specific categories of system functions for detailed information and usage examples.