Skip to content
Related Articles

Related Articles

Improve Article

USER_NAME() Function in SQL Server

  • Last Updated : 21 Jan, 2021

USER_NAME() :

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

Features :

  • This function is used to find the username of the database used.
  • This function comes under Advanced Functions.
  • This function accepts only one parameter i.e, id number.
  • This function returns the current user’s name in case the id number is not specified.

Syntax :

USER_NAME(id_number)

Parameter :



This method accepts only one parameter.

  • id_number – Specified id number in the database of the user. It is optional.

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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :