Use APP_NAME() to Get the Application Name of the Current Session in SQL Server

In SQL Server, you can use the APP_NAME() function to get the application name for the current session. This assumes the application sets that name value.

You can use this function to distinguish between different applications, as a way to perform different actions for those applications.

Note that the client provides the application name, and so the result returned by this function simply reflects whatever name the client provides. For this reason, Microsoft advises that this function should not be used for security checks.

Syntax

The function doesn’t require any arguments, so its syntax goes like this:

APP_NAME  ( )

Example 1 – Result in Azure Data Studio

Here’s the result I get when using Azure Data Studio.

SELECT APP_NAME( ) AS Result;

Result:

+--------------+
| Result       |
|--------------|
| azdata-Query |
+--------------+

Example 2 – Result in mssql-cli

Here’s the result I get when using the mssql-cli command line interface.

SELECT APP_NAME( ) AS Result;

Result:

+-----------------------------------+
| Result                            |
|-----------------------------------|
| Core .Net SqlClient Data Provider |
+-----------------------------------+

Example 3 – Using APP_NAME() in a Conditional Statement

Here’s an example of using APP_NAME() in a conditional statement to provide a different date format depending on the application being used.

IF APP_NAME() = 'azdata-Query'  
  PRINT 'Application: ' + APP_NAME() + char(10) + 'Date: ' + CONVERT ( varchar(100) , GETDATE(), 111);
ELSE IF APP_NAME() = 'Core .Net SqlClient Data Provider' 
  PRINT 'Application: ' + APP_NAME() + char(10) + 'Date: ' + CONVERT ( varchar(100) , GETDATE(), 103);

Result in Azure Data Studio:

Application: azdata-Query
Date: 2019/12/06

Result in mssql-cli:

Application: Core .Net SqlClient Data Provider
Date: 06/12/2019