SQL USER_NAME Function

Example

Return the database user name (will return the current user since no id is specified):

SELECT USER_NAME();

Definition and Usage

The USER_NAME() function returns the database user name based on the specified id.

If no id is specified, this function will return the name of the current user.

Syntax

USER_NAME(id_number)

Continue reading SQL USER_NAME Function

SQL SESSIONPROPERTY Function

Example

Return the session settings for a specified option:

SELECT SESSIONPROPERTY('ANSI_NULLS');

Definition and Usage

The SESSIONPROPERTY() function returns the session settings for a specified option.

Syntax

SESSIONPROPERTY(option)

Continue reading SQL SESSIONPROPERTY Function

SQL NULLIF Function

Example

Compare two expressions:

SELECT NULLIF(25, 25);

Definition and Usage

The NULLIF() function returns NULL if two expressions are equal, otherwise it returns the first expression.

Syntax

NULLIF(expr1, expr2)

Continue reading SQL NULLIF Function

SQL ISNUMERIC Function

Example

Tests whether the expression is numeric:

SELECT ISNUMERIC(4567);

Definition and Usage

The ISNUMERIC() function tests whether an expression is numeric.

This function returns 1 if the expression is numeric, otherwise it returns 0.

Syntax

ISNUMERIC(expression)

Continue reading SQL ISNUMERIC Function

SQL ISNULL Function

Example

Return the specified value IF the expression is NULL, otherwise return the expression:

SELECT ISNULL(NULL, 'iampsp.com');

Definition and Usage

The ISNULL() function returns a specified value if the expression is NULL.

If the expression is NOT NULL, this function returns the expression.

Syntax

ISNULL(expression, value)

Continue reading SQL ISNULL Function

SQL IIF Function

Example

Return “YES” if the condition is TRUE, or “NO” if the condition is FALSE:

SELECT IIF(500<1000, 'YES', 'NO');

Definition and Usage

The IIF() function returns a value if a condition is TRUE, or another value if a condition is FALSE.

Syntax

IIF(condition, value_if_true, value_if_false)

Continue reading SQL IIF Function

SQL CURRENT_USER Function

Example

Return the name of the current user in the SQL Server database:

SELECT CURRENT_USER;

Definition and Usage

The CURRENT_USER function returns the name of the current user in the SQL Server database.

Syntax

CURRENT_USER

Continue reading SQL CURRENT_USER Function

SQL CONVERT Function

Example

Convert an expression to int:

SELECT CONVERT(int, 25.65);

Definition and Usage

The CONVERT() function converts a value (of any type) into a specified datatype.

Tip: Also look at the CAST() function.

Syntax

CONVERT(data_type(length), expression, style)

Continue reading SQL CONVERT Function