SQL Server provides a variety of string functions that allow you to manipulate and work with string data. Here’s a summary of some of the most commonly used string functions in SQL Server, along with brief descriptions and examples for each.
LEN()
- Description: Returns the number of characters in a string.
- Example:
sql1SELECT LEN('Hello, World!') AS StringLength; -- Returns 13
UPPER()
- Description: Converts a string to uppercase.
- Example:
sql1SELECT UPPER('Hello, World!') AS UpperCaseString; -- Returns 'HELLO, WORLD!'
LOWER()
- Description: Converts a string to lowercase.
- Example:
sql1SELECT LOWER('Hello, World!') AS LowerCaseString; -- Returns 'hello, world!'
SUBSTRING()
- Description: Extracts a substring from a string starting at a specified position.
- Example:
sql1SELECT SUBSTRING('Hello, World!', 1, 5) AS Substring; -- Returns 'Hello'
CHARINDEX()
- Description: Returns the starting position of a specified substring within a string.
- Example:
sql1SELECT CHARINDEX('World', 'Hello, World!') AS Position; -- Returns 8
REPLACE()
- Description: Replaces all occurrences of a specified substring with another substring.
- Example:
sql1SELECT REPLACE('Hello, World!', 'World', 'SQL Server') AS ReplacedString; -- Returns 'Hello, SQL Server!'
LTRIM()
- Description: Removes leading spaces from a string.
- Example:
sql1SELECT LTRIM(' Hello, World!') AS TrimmedString; -- Returns 'Hello, World!'
RTRIM()
- Description: Removes trailing spaces from a string.
- Example:
sql1SELECT RTRIM('Hello, World! ') AS TrimmedString; -- Returns 'Hello, World!'
TRIM()
- Description: Removes both leading and trailing spaces from a string (available in SQL Server 2017 and later).
- Example:
sql1SELECT TRIM(' Hello, World! ') AS TrimmedString; -- Returns 'Hello, World!'
CONCAT()
- Description: Concatenates two or more strings into one string.
- Example:
sql1SELECT CONCAT('Hello', ', ', 'World!') AS ConcatenatedString; -- Returns 'Hello, World!'
FORMAT()
- Description: Formats a value based on a specified format.
- Example:
sql1SELECT FORMAT(GETDATE(), 'yyyy-MM-dd') AS FormattedDate; -- Returns current date in 'YYYY-MM-DD' format
STRING_AGG()
- Description: Concatenates values from multiple rows into a single string with a specified separator (available in SQL Server 2017 and later).
- Example:
sql1SELECT STRING_AGG(Name, ', ') AS AllNames
2FROM Employees; -- Returns a comma-separated list of employee names
ASCII()
- Description: Returns the ASCII value of the leftmost character of a string.
- Example:
sql1SELECT ASCII('A') AS AsciiValue; -- Returns 65
CHAR()
- Description: Returns the character corresponding to the specified ASCII value.
- Example:
sql1SELECT CHAR(65) AS Character; -- Returns 'A'
REVERSE()
- Description: Reverses the order of characters in a string.
- Example:
sql1SELECT REVERSE('Hello, World!') AS ReversedString; -- Returns '!dlroW ,olleH'
LEN()
- Description: Returns the number of characters in a string.
- Example:sql
1SELECT LEN('Hello, World!') AS StringLength; -- Returns 13
UPPER()
- Description: Converts a string to uppercase.
- Example:sql
1SELECT UPPER('Hello, World!') AS UpperCaseString; -- Returns 'HELLO, WORLD!'
LOWER()
- Description: Converts a string to lowercase.
- Example:sql
1SELECT LOWER('Hello, World!') AS LowerCaseString; -- Returns 'hello, world!'
SUBSTRING()
- Description: Extracts a substring from a string starting at a specified position.
- Example:sql
1SELECT SUBSTRING('Hello, World!', 1, 5) AS Substring; -- Returns 'Hello'
CHARINDEX()
- Description: Returns the starting position of a specified substring within a string.
- Example:sql
1SELECT CHARINDEX('World', 'Hello, World!') AS Position; -- Returns 8
REPLACE()
- Description: Replaces all occurrences of a specified substring with another substring.
- Example:sql
1SELECT REPLACE('Hello, World!', 'World', 'SQL Server') AS ReplacedString; -- Returns 'Hello, SQL Server!'
LTRIM()
- Description: Removes leading spaces from a string.
- Example:sql
1SELECT LTRIM(' Hello, World!') AS TrimmedString; -- Returns 'Hello, World!'
RTRIM()
- Description: Removes trailing spaces from a string.
- Example:sql
1SELECT RTRIM('Hello, World! ') AS TrimmedString; -- Returns 'Hello, World!'
TRIM()
- Description: Removes both leading and trailing spaces from a string (available in SQL Server 2017 and later).
- Example:sql
1SELECT TRIM(' Hello, World! ') AS TrimmedString; -- Returns 'Hello, World!'
CONCAT()
- Description: Concatenates two or more strings into one string.
- Example:sql
1SELECT CONCAT('Hello', ', ', 'World!') AS ConcatenatedString; -- Returns 'Hello, World!'
FORMAT()
- Description: Formats a value based on a specified format.
- Example:sql
1SELECT FORMAT(GETDATE(), 'yyyy-MM-dd') AS FormattedDate; -- Returns current date in 'YYYY-MM-DD' format
STRING_AGG()
- Description: Concatenates values from multiple rows into a single string with a specified separator (available in SQL Server 2017 and later).
- Example:sql
1SELECT STRING_AGG(Name, ', ') AS AllNames 2FROM Employees; -- Returns a comma-separated list of employee names
ASCII()
- Description: Returns the ASCII value of the leftmost character of a string.
- Example:sql
1SELECT ASCII('A') AS AsciiValue; -- Returns 65
CHAR()
- Description: Returns the character corresponding to the specified ASCII value.
- Example:sql
1SELECT CHAR(65) AS Character; -- Returns 'A'
REVERSE()
- Description: Reverses the order of characters in a string.
- Example:sql
1SELECT REVERSE('Hello, World!') AS ReversedString; -- Returns '!dlroW ,olleH'