Open In App

USER_NAME() Function in SQL Server

USER_NAME() :

This function in SQL Server is used to return the username of the database that is based on the id stated.



Features :

Syntax :



USER_NAME(id_number)

Parameter :

This method accepts only one parameter.

Returns :

It returns the username of the specified id number. Moreover, if the id number is not specified then it returns the current username.

Example-1 :

Using the USER_NAME() function and getting the username with no id number.

SELECT USER_NAME();

Output :

nidhi

Here, the id number is not provided so, the current user’s name is returned.

Example-2 :

Using the USER_NAME() function and getting the username of the specified id number.

SELECT USER_NAME(2);

Output :

Geek

Example-3 :

Using the USER_NAME() function and getting the username of the specified id number using a variable.

DECLARE @id INT;
SET @id = 3;
SELECT USER_NAME(@id);

Output :

INFORMATION_SCHEMA

Example-4 :

Using the USER_NAME() function and getting the username of the specified id number using the CAST() function.

SELECT USER_NAME(CAST(2.2 as int));

Output :

Geek

Application :

This function is used to find the username of the database used.

Article Tags :
SQL