SQL Server Functions - String Functions
Syntax
ASCII('character')
Example
SELECT ASCII('A') as Code_of_character
Output
Code_of_character
65
65
If more than one characters are specified, the ASCII functions will return the code for the first character of the word , other characters will be ignored.
Example
SELECT ASCII('Tech') as Code_of_character
Output
Code_of_character
84
84
Syntax
CHAR('number')
Example
select char('65') as Character_Of_Code
Output
Character_of_code
A
A
Syntax
CHARINDEX(substring,string,start_position)
Parameters
substring-The substring to find in string.This is required.
string- The string to find in. This is required.
start_position- The is the position in string from the search will start- first position is 1. It is an optional parameter.
Example
select CHARINDEX('v','vishal') as Position_Of_Character
Output
Position_Of_Character
1
1
Syntax
CONCAT(string_1,string_2,........string_n)
Parameters
string_1,string_2...string_n- strings to concat.
Example
select CONCAT('google','.com') as Concatenated_string
Output
Concatenated_string
google.com
google.com
Syntax
DATALENGTH(expression)
Parameters
expression. string of which the length to calculate.
Example
select DATALENGTH('google') as Length_Of_String
Output
Length_Of_String
6
Note- DATALENGTH function also calculate the space.6
Example
select DATALENGTH('google ') as Length_Of_String
Output
Length_Of_String
7
7
Syntax
LEFT(string,no_of_characters)
Parameters
string- strings from which the characters need to extract.
no_of_characters- no of characters to extract.
Example
select LEFT('google',2) as Extracted_String
Output
Extracted_String
go
Note- LEFT function also takes space into consideration if provided.go
Example
select LEFT(' google',2) as Extracted_String
Output
Extracted_String
g
g
Syntax
LEN(expression)
Parameters
string - to calculate length.
Example
select LEN('google') as Length_Of_String
Output
Length_Of_String
6
Note- The LEN function does not count trailing spaces but it does count leading spaces in expression.
6
Syntax
LOWER (expression)
Parameters
string - to lowercase.
Example
select LOWER('GOOGLE') as Lowercase_String
Output
Lowercase_String
google
Syntax
UPPER (expression)
Parameters
string - to uppercase.
Example
select UPPER('google') as Uppercase_String
Output
Uppercase_String
GOOGLE
Syntax
LTRIM(expression)
Parameters
string - to remove leading spaces from.
Example
select LTRIM(' google' ) as removedleadingspace
Output
removedleadingspace
google
Syntax
RTRIM(expression)
Parameters
string - to remove leading spaces from.
Example
select RTRIM('google ' ) as removedtrailingspace
Output
removedtrailingspace
google
Syntax
PATINDEX (expression)
Parameters
pattern- the pattern to find. The pattern must be provided with wildcard characters
string- the string to search in.
Wildcard characters
%- matches string with any length including zero.
_ - matches single character.
[]- matches any characters provided in the bracket.
[^]- matches any characters which are not in the bracket.
Example
select PATINDEX('%goo%','google') as location_Of_Searched_Pattern
Output
location_Of_Searched_Pattern
google
Note- If supplied pattern not found , the function returns zero.
Syntax
REPLACE (string, string_to_replace,replacement_string)
Parameters
string - in which the characters to replace.
string_to_replace- the string to replace in string.
replacement_string- string to replace for string_to_replace.
Example
select REPLACE('KGS','K','S') as replaced_string
Output
replaced_string
SGS
SGS
Syntax
RIGHT(string,no_of_characters)
Parameters
string - the string from which the characters need to extract from .
no_of_characters - number characters to extract.
Example
select RIGHT('google',3) as Extracted_From_Right
Output
Extracted_From_Right
gle
gle
Syntax
SPACE(number)
Parameters
number- number of spaces to return.
Example
select SPACE(10) as SpaceReturn
Output
SpaceReturn
Syntax
STR(number,length,decimal_places)
Parameters
number- number to convert to string.
length- the length of the returned string. Default length is 10
decimal_places- the number of decimals to display.
Example
select STR(54,4,1) as returnednumber
Output
returnednumber
54.0
54.0
Syntax
STUFF(source_string,start,length,another_string)
Parameters
source_string- the string to modify.
start- the position in source string from which to delete characters and start inserting another characters.
length- the number of character to delete from source string.
another_string- the string to insert in place of deleted characters.
Example
select STUFF('DJJEE',1,3,'ggg') as returnedstring
Output
returnedstring
gggEE
gggEE
Syntax
SUBSTRING(source_string,start,number_of_characters)
Parameters
source_string- the string to modify.
start- the position to start from.
number_of_characters- the number of character to extract.
Example
select SUBSTRING('google',1,3) as returnedstring
Output
returnedstring
goo
goo
SQL Server Functions - String Functions
Reviewed by LanguageExpert
on
June 24, 2018
Rating:
No comments