Example
Return a string with 10 spaces:
SELECT SPACE(10);
Definition and Usage
The SPACE() function returns a string of the specified number of space characters.
SPACE(number)
Return a string with 10 spaces:
SELECT SPACE(10);
The SPACE() function returns a string of the specified number of space characters.
SPACE(number)
Evaluate the similarity of two strings, and return a four-character code:
SELECT SOUNDEX('Juice'), SOUNDEX('Jucy');
The SOUNDEX() function returns a four-character code to evaluate the similarity of two expressions.
Note: The SOUNDEX() converts the string to a four-character code based on how the string sounds when spoken.
Tip: Also look at the DIFFERENCE() function.
SOUNDEX(expression)
Remove trailing spaces from a string:
SELECT RTRIM('SQL Tutorial ') AS RightTrimmedString;
The RTRIM() function removes trailing spaces from a string.
Note: Also look at the LTRIM() function.
RTRIM(string)
Extract 3 characters from a string (starting from right):
SELECT RIGHT('SQL Tutorial', 3) AS ExtractString;
The RIGHT() function extracts a number of characters from a string (starting from right).
RIGHT(string, number_of_chars)
Reverse a string:
SELECT REVERSE('SQL Tutorial');
The REVERSE() function reverses a string and returns the result.
REVERSE(string)
Repeat a string:
SELECT REPLICATE('SQL Tutorial', 5);
The REPLICATE() function repeats a string a specified number of times.
REPLICATE(string, integer)
Replace “T” with “M”:
SELECT REPLACE('SQL Tutorial', 'T', 'M');
The REPLACE() function replaces all occurrences of a substring within a string, with a new substring.
Note: The search is case-insensitive.
Tip: Also look at the STUFF() function.
REPLACE(string, old_string, new_string)
Return a Unicode string with bracket delimiters (default):
SELECT QUOTENAME('abcdef');
The QUOTENAME() function returns a Unicode string with delimiters added to make the string a valid SQL Server delimited identifier.
QUOTENAME(string, quote_char)
Return the position of a pattern in a string:
SELECT PATINDEX(‘%schools%’, ‘iampsp.com’);
The PATINDEX() function returns the position of a pattern in a string.
If the pattern is not found, this function returns 0.
Note: The search is case-insensitive and the first position in string is 1.
PATINDEX(%pattern%, string)
Return the Unicode character based on the number code 65:
SELECT NCHAR(65) AS NumberCodeToUnicode;
The NCHAR() function returns the Unicode character based on the number code.
NCHAR(number_code)