SQL Server CHARINDEX Function

Example

Search for “t” in string “Customer”, and return position:

SELECT CHARINDEX('t', 'Customer') AS MatchPosition;

Definition and Usage

The CHARINDEX() function searches for a substring in a string, and returns the position.

If the substring is not found, this function returns 0.

Note: This function performs a case-insensitive search. Continue reading SQL Server CHARINDEX Function

SQL Server CHAR Function

Example

Return the character based on the number code 65:

SELECT CHAR(65) AS CodeToCharacter;

Definition and Usage

The CHAR() function returns the character based on the ASCII code.

Syntax

CHAR(code)

Continue reading SQL Server CHAR Function

SQL Server Functions

SQL Server has many built-in functions.

This reference contains string, numeric, date, conversion, and some advanced functions in SQL Server.


SQL Server String Functions

Function Description
ASCII Returns the ASCII value for the specific character
CHAR Returns the character based on the ASCII code
CHARINDEX Returns the position of a substring in a string
CONCAT Adds two or more strings together
Concat with + Adds two or more strings together
CONCAT_WS Adds two or more strings together with a separator
DATALENGTH Returns the number of bytes used to represent an expression
DIFFERENCE Compares two SOUNDEX values, and returns an integer value
FORMAT Formats a value with the specified format
LEFT Extracts a number of characters from a string (starting from left)
LEN Returns the length of a string
LOWER Converts a string to lower-case
LTRIM Removes leading spaces from a string
NCHAR Returns the Unicode character based on the number code
PATINDEX Returns the position of a pattern in a string
QUOTENAME Returns a Unicode string with delimiters added to make the string a valid SQL Server delimited identifier
REPLACE Replaces all occurrences of a substring within a string, with a new substring
REPLICATE Repeats a string a specified number of times
REVERSE Reverses a string and returns the result
RIGHT Extracts a number of characters from a string (starting from right)
RTRIM Removes trailing spaces from a string
SOUNDEX Returns a four-character code to evaluate the similarity of two strings
SPACE Returns a string of the specified number of space characters
STR Returns a number as string
STUFF Deletes a part of a string and then inserts another part into the string, starting at a specified position
SUBSTRING Extracts some characters from a string
TRANSLATE Returns the string from the first argument after the characters specified in the second argument are translated into the characters specified in the third argument.
TRIM Removes leading and trailing spaces (or other specified characters) from a string
UNICODE Returns the Unicode value for the first character of the input expression
UPPER Converts a string to upper-case

Continue reading SQL Server Functions

SQL Server ASCII Function

Example

Return the ASCII value of the first character in “CustomerName”:

SELECT ASCII(CustomerName) AS NumCodeOfFirstChar
FROM Customers;

Definition and Usage

The ASCII() function returns the ASCII value for the specific character.

Syntax

ASCII(character)

Continue reading SQL Server ASCII Function

MySQL VERSION Function

Example

Return the current version of the MySQL database:

SELECT VERSION();

Definition and Usage

The VERSION() function returns the current version of the MySQL database, as a string.

Syntax

VERSION()

Continue reading MySQL VERSION Function

MySQL USER Function

Example

Return the current user name and host name for the MySQL connection:

SELECT USER();

Definition and Usage

The USER() function returns the current user name and host name for the MySQL connection.

Syntax

USER()

Continue reading MySQL USER Function

MySQL SYSTEM_USER Function

Example

Return the current user name and host name for the MySQL connection:

SELECT SYSTEM_USER();

Definition and Usage

The SYSTEM_USER() function returns the current user name and host name for the MySQL connection.

 

Syntax

SYSTEM_USER()

Continue reading MySQL SYSTEM_USER Function

MySQL SESSION_USER Function

Example

Return the current user name and host name for the MySQL connection:

SELECT SESSION_USER();

Definition and Usage

The SESSION_USER() function returns the current user name and host name for the MySQL connection.

 

Syntax

SESSION_USER()

Continue reading MySQL SESSION_USER Function

MySQL NULLIF Function

Example

Compare two expressions:

SELECT NULLIF(25, 25);

Definition and Usage

The NULLIF() function compares two expressions and returns NULL if they are equal. Otherwise, the first expression is returned.

Syntax

NULLIF(expr1, expr2)

Continue reading MySQL NULLIF Function

MySQL LAST_INSERT_ID Function

Example

Return the AUTO_INCREMENT id of the last row that has been inserted in a table:

SELECT LAST_INSERT_ID();

Definition and Usage

The LAST_INSERT_ID() function returns the AUTO_INCREMENT id of the last row that has been inserted in a table.

Syntax

LAST_INSERT_ID(expression)

Continue reading MySQL LAST_INSERT_ID Function