SQL string functions

The table is created relative to MS SQL Server string functions set.

Function Name Description
ASCII() Returns the ASCII code value of the leftmost character of a string.
SELECT ASCII('A') AS AsciiValue;
-- 65
CHAR() Returns a character based on its ASCII code value.
SELECT CHAR(65) AS Character;
-- A
CONCAT() Concatenates two or more strings together.
SELECT CONCAT('John', ' ', 'Doe') AS FullName;
-- John Doe
CONCAT_WS() Concatenates multiple strings together with a specified separator.
SELECT CONCAT_WS(', ', 'John', 'Doe', '123 Main St.') AS Address;
-- John, Doe, 123 Main St.
DIFFERENCE() Compares two strings and returns an integer that represents the difference between them.
SELECT DIFFERENCE('apple', 'apples') AS Difference;
-- 2
FORMAT() Formats a value with a specified format mask.
SELECT FORMAT(12345.6789, '#,##0.00') AS FormattedValue;
-- 12,345.68
LEFT() Returns a specified number of characters from the left side of a string.
SELECT LEFT('John Doe', 4) AS FirstName;
-- John
LEN() Returns the length of a string.
SELECT LEN('John Doe') AS NameLength;
-- 8
LOWER() Returns a string in lowercase.
SELECT LOWER('JOHN DOE') AS LowerName;
-- john doe
LTRIM() Removes leading spaces from a string.
SELECT LTRIM(' John Doe ') AS TrimmedName;
-- 'John Doe '
NCHAR() Returns a Unicode character based on its Unicode code value.
SELECT NCHAR(65) AS UnicodeChar;
-- A
PATINDEX() Returns the starting position of a pattern in a string.
SELECT PATINDEX('%do%', 'John Doe') AS Position;
-- 6
QUOTENAME() Encloses a string in square brackets and escapes any embedded brackets within the string.
SELECT QUOTENAME('MyTable') AS TableName;
-- [MyTable]
REPLACE() Replaces all occurrences of a specified string with another specified string.
SELECT REPLACE('Hello, World!', 'World', 'SQL') AS Result;
-- Hello, SQL!
REPLICATE() Repeats a string a specified number of times.
SELECT REPLICATE('SQL ', 3) AS Result;
-- SQL SQL SQL
REVERSE() Returns the reverse of a string.
SELECT REVERSE('Hello, World!') AS Result;
-- !dlroW ,olleH
RIGHT() Returns a specified number of characters from the right side of a string.
SELECT RIGHT('Hello, World!', 6) AS Result;
World!
RTRIM() Removes all trailing spaces from a string.
SELECT RTRIM(' SQL ') AS Result;
-- SQL
SPACE() Returns a string of spaces of the specified length.
SELECT SPACE(5) + 'SQL' AS Result;
--      SQL
STUFF() Deletes a specified length of characters from a string and inserts another string at the specified starting position.
-- SELECT STUFF('Hello, World!', 7, 5, 'SQL') AS Result;
Hello, SQL!
SUBSTRING() Returns a specified part of a string.
SELECT SUBSTRING('Hello, World!', 1, 5) AS Result;
-- Hello
TRANSLATE() Replaces a set of characters in a string with another set of characters.
SELECT TRANSLATE('Hello, World!', 'oe', 'EO') AS Result;
-- HEllo, WOrld!
TRIM() Removes all leading and trailing spaces from a string.
SELECT TRIM(' SQL  ') AS Result;
-- SQL
UNICODE() Returns the Unicode value of the first character in a string.
SELECT UNICODE('A') AS Result;
-- 65
UPPER() Converts all characters in a string to uppercase.
SELECT UPPER('Hello, World!') AS Result;
-- HELLO, WORLD!

Комментарии

Популярные сообщения из этого блога

Today's activity report #17