SQL Server Tutorial
String functions are used to format data and mostly used with char and varchar data types.
String functions are used for various operations on strings and can be used as a part of character expressions.
SELECT string_function(parameters)
here string_function is the string function name and parameters are the required arguments for the string function.
ASCII function is used to get the ASCII value for left most character in a string.
SELECT ASCII('CDE')Output:
67
returns ASCII value for character 'C'.
CHAR function is used to get the character for the ASCII value.
SELECT CHAR(67)Output:
C
returns character for given ASCII value.
CHARINDEX function is used to get the starting index for a pattern string on the string.
SELECT CHARINDEX('OD', 'CODINGPOINDER')Output:
2
returns the starting index of pattern string 'OD' on string 'CODINGPOINTER'.
CHARINDEX function returns zero if not found a pattern on the string.
SELECT CHARINDEX('CD', 'CODINGPOINDER')Output:
0
LOWER function is used to get the string in lower case letters.
SELECT LOWER('CodingPointer')Output:
codingpointer
returns the string in lower case.
UPPER function is used to get the string in upper case letters.
SELECT UPPER('CodingPointer')Output:
CODINGPOINTER
returns the string in upper case.
LEFT function is used to get the sub string from left based on number of character specified.
SELECT LEFT('CodingPointer', 6)Output:
Coding
returns the 6 characters from left 'Coding' on the string 'CodingPointer'.
RIGHT function is used to get the sub string from right based on number of character specified.
SELECT RIGHT('CodingPointer', 7)Output:
Pointer
returns the 7 characters from right 'Pointer' on the string 'CodingPointer'.
LEN function is used to get the number of characters on the string.
SELECT LTRIM('CodingPointer')Output:
13
returns the 13 which total number of characters on the string 'CodingPointer'.
LTRIM Function is used to remove the leading blanks(empty spaces) from the string.
SELECT LTRIM(' CodingPointer')Output:
CodingPointer
returns the leading empty spaces on the string ' CodingPointer'.
RTRIM Function is used to remove the trailing blanks(empty spaces) from the string.
SELECT RTRIM('CodingPointer ')Output:
CodingPointer
returns the trailing empty spaces on the string 'CodingPointer '.
REVERSE function is used to get the string in reverse which is from right to left.
SELECT REVERSE('CODINGPOINDER')Output:
REDNIOPGNIDOC
PATINDEX function is used to get the starting index of the specified pattern on the string, returns zero if pattern is not found.
SELECT PATINDEX('%GPOI%', 'CODINGPOINDER')Output:
6
SUBSTRING function is used to get the sub string based on specified index and number of characters on the string.
SELECT SUBSTRING('CODINGPOINDER', 2, 5)Output:
ODING
STR function is used to convert the numeric data into string and can also specify number of decimals and total number of characters in the string.
SELECT STR(45233.42378, 9, 3)Output:
45233.424
STUFF function is used to replace the string on the string using starting index and number of characters to be deleted and inserting sub string.
SELECT STUFF('CODINGPOINDER', 7, 7, ' POINTER')Output:
CODING POINTER
SPACE function is used to add number of spaces required on the string.
SELECT 'CODING' + SPACE(1) + 'POINDER'Output:
CODING POINTER
DIFFERNCE Function is used to compare two strings and provides similarity between them (match ranges 0 to 4).
4 -> best match, 0 -> no match
SELECT DIFFERENCE('CODINGPOINDER', 'CODINGPOINDER')Output:
4
SQL Server Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us | Report website issues in Github | Facebook page | Google+ page