SQL Server Functions
SQL Server Functions
Functions
SQL Server has many built-in functions.
Example
Return the ASCII value of the first character in "CustomerName":
SELECT ASCII(CustomerName) AS NumCodeOfFirstChar
FROM Customers;
Example
SELECT CHAR(65) AS CodeToCharacter;
Example
SELECT CHARINDEX('t', 'Customer') AS MatchPosition;
DATALENGTH Returns the number of bytes used to represent an
expression
Example
SELECT DATALENGTH('Tobruk');
Example
Extract 9 characters from a string (starting from left):
SELECT LEFT('SQL Tutorial', 9) AS ExtractString;
Example
Example
Convert the text to lower-case:
Example
Remove leading spaces from a string:
Example
Return the Unicode character based on the number code 65:
SELECT NCHAR(65) AS NumberCodeToUnicode;
Example
Return the position of a pattern in a string:
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);
Example
Reverse a string:
SELECT REVERSE('SQL Tutorial');
Example
Extract 3 characters from a string (starting from right):
SELECT RIGHT('SQL Tutorial', 3) AS ExtractString;
Example
Remove trailing spaces from a string:
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'
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');
Example
Convert the text to upper-case: