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
ASCII Returns the ASCII value for the specific character
Example
Return the ASCII value of the first character in "CustomerName":
SELECT ASCII(CustomerName) AS NumCodeOfFirstChar
FROM Customers;
CHAR Returns the character based on the ASCII code
Example
Return the character based on the number code 65:
SELECT CHAR(65) AS CodeToCharacter;
CHARINDEX Returns the position of a substring in a string
Example
Search for "t" in string "Customer", and return position:
SELECT CHARINDEX('t', 'Customer') AS MatchPosition;
DATALENGTH Returns the number of bytes used to represent an
expression
Example
Return the length of an expression (in bytes):
SELECT DATALENGTH('Tobruk');
LEFT Extracts a number of characters from a string (starting
from left)
Example
Extract 9 characters from a string (starting from left):
SELECT LEFT('SQL Tutorial', 9) AS ExtractString;
LEN Returns the length of a string
Example
Return the length of a string:
SELECT LEN('Tobruk University');
LOWER Converts a string to lower-case
Example
Convert the text to lower-case:
SELECT LOWER('SQL Tutorial is FUN!');
LTRIM Removes leading spaces from a string
Example
Remove leading spaces from a string:
SELECT LTRIM(' SQL Tutorial') AS LeftTrimmedString;
NCHAR Returns the Unicode character based on the number
code
Example
Return the Unicode character based on the number code 65:
SELECT NCHAR(65) AS NumberCodeToUnicode;
PATINDEX Returns the position of a pattern in a string
Example
Return the position of a pattern in a string:
SELECT PATINDEX('%University%', 'Tobruk University');
REPLACE Replaces all occurrences of a substring within a string,
with a new substring
Example
Replace "T" with "M":
SELECT REPLACE('SQL Tutorial', 'T', 'M');
REPLICATE Repeats a string a specified number of times
Example
Repeat a string:
SELECT REPLICATE('SQL Tutorial', 4);
REVERSE Reverses a string and returns the result
Example
Reverse a string:
SELECT REVERSE('SQL Tutorial');
RIGHT Extracts a number of characters from a string (starting
from right)
Example
Extract 3 characters from a string (starting from right):
SELECT RIGHT('SQL Tutorial', 3) AS ExtractString;
RTRIM Removes trailing spaces from a string
Example
Remove trailing spaces from a string:
SELECT RTRIM('SQL Tutorial ') AS RightTrimmedString;
STUFF Deletes a part of a string and then inserts another part
into the string, starting at a specified position
Example
Delete 3 characters from a string, starting in position 1, and then
insert "HTML" in position 1:
SELECT STUFF('SQL Tutorial', 1, 3, 'HTML');
HTML Tutorial'
SUBSTRING Extracts some characters from a string
Example
Extract 3 characters from a string, starting in position 1:
SELECT SUBSTRING('SQL Tutorial', 1, 3) AS ExtractString;
UNICODE Returns the Unicode value for the first character of the
input expression
Example
Return an integer value (the Unicode value), for the first character of
the input expression:
SELECT UNICODE('Atlanta');
UPPER Converts a string to upper-case
Example
Convert the text to upper-case:
SELECT UPPER('SQL Tutorial is FUN!');