totn SQL Server Functions

SQL Server: USER_NAME Function

This SQL Server tutorial explains how to use the USER_NAME function in SQL Server (Transact-SQL) with syntax and examples.

Description

In SQL Server (Transact-SQL), the USER_NAME function returns the user name in the SQL Server database.

Syntax

The syntax for the USER_NAME function in SQL Server (Transact-SQL) is:

USER_NAME( [ identification_number ] )

Parameters or Arguments

identification_number
Optional. The identification number associated with a user in SQL Server.

Note

  • If the identification_number is specified, the USER_NAME function will return the user name for that identification_number.
  • If the identification_number is not provided, the USER_NAME function will return the current user in SQL Server in the given context.
  • See also the CURRENT_USER, SESSION_USER, SYSTEM_USER functions.

Applies To

The USER_NAME function can be used in the following versions of SQL Server (Transact-SQL):

  • SQL Server 2017, SQL Server 2016, SQL Server 2014, SQL Server 2012, SQL Server 2008 R2, SQL Server 2008, SQL Server 2005

Example

Let's look at some SQL Server USER_NAME function examples and explore how to use the USER_NAME function in SQL Server (Transact-SQL).

For example:

SELECT USER_NAME();
Result: 'dbo'

SELECT USER_NAME(12);
Result: 'jsmith'