Monday 27 January 2020

Built-in string functions in SQL Server

Hi All,

Hope you all are doing good.

In this article, we will know about various T-SQL string functions and their uses.
Please go through below table to understand each string function.

Method NameDescriptionSyntaxExampleOutput
ASCIIReturns the ASCII code of the given character.ASCII(Character_Expression)SELECT ASCII('A')65
CHARConverts an int ASCII code to a character. Note: integer should be between 0 and 255.CHAR(Integer_Expression)SELECT CHAR(255)ÿ
REVERSEReverses all the characters in the given string.REVERSE('Any_String_Expression')SELECT REVERSE('SUNEEL')LEENUS
LENReturns the count of total characters in the given string expressionLEN(String_Expression)SELECT LEN('SUNEEL KUMAR')12
LOWERConverts all the characters in the given Character_Expression into lowercase letters.LOWER(Character_Expression)SELECT LOWER('SUNEEL')suneel
UPPERConverts all the characters in the given Character_Expression intto uppercase letters.UPPER(Character_Expression)SELECT UPPER('suneel')SUNEEL
RTRIMRemoves blanks on the right hand side of the given character expression.RTRIM(Character_Expression)SELECT RTRIM('SUNEEL ')SUNEEL
LTRIMRemoves blanks on the left handside of the given character expression.LTRIM(Character_Expression)SELECT LTRIM(' SUNEEL')SUNEEL
LEFTReturns the specified number of characters from the left hand side of the given character expression.LEFT(Character_Expression, Integer_Expression)SELECT LEFT('ABCDE', 3)ABC
RIGHTReturns the specified number of characters from the right hand side of the given character expression.RIGHT(Character_Expression, Integer_Expression)SELECT RIGHT('ABCDE', 3)CDE
CHARINDEXReturns the starting position of the specified expression in a character string. Start_Location parameter is optional.CHARINDEX('Expression_To_Find', 'Expression_To_Search', 'Start_Location')SELECT CHARINDEX('@','suneel@suneel.com',1)7
SUBSTRINGThis function returns substring (part of the string), from the given expression.SUBSTRING('Expression', 'Start', 'Length')SELECT SUBSTRING('suneel@suneel.com',6, 7)l@sunee
REPLICATERepeats the given string, for the specified number of times.REPLICATE(String_To_Be_Replicated, Number_Of_Times_To_Replicate)SELECT REPLICATE('Suneel ', 3)Suneel Suneel Suneel
SPACEReturns number of spaces, specified by the Number_Of_Spaces argument.SPACE(Number_Of_Spaces)SELECT 'Suneel' + SPACE(5) + ' Kumar'Suneel Kumar
PATINDEXReturns the starting position of the first occurrence of a pattern in a specified expression. It takes two arguments, the pattern to be searched and the expression. PATINDEX() is simial to CHARINDEX(). With CHARINDEX() we cannot use wildcards, where as PATINDEX() provides this capability. If the specified pattern is not found, PATINDEX() returns ZERO.PATINDEX('%Pattern%', Expression)SELECT PATINDEX('%@suneel.com', 'suneel@suneel.com')7
REPLACEReplaces all occurrences of a specified string value with another string value.REPLACE(String_Expression, Pattern , Replacement_Value)SELECT REPLACE('suneel@suneel.com', '.com', '.net')suneel@suneel.net
STUFFSTUFF() function inserts Replacement_expression, at the start position specified, along with removing the charactes specified using Length parameter.STUFF(Original_Expression, Start, Length, Replacement_expression)SELECT STUFF('suneel@suneel.com', 3, 4, '*****')su*****@suneel.com
Thanks for reading my article.

No comments:

Post a Comment

Intoduction to Flutter

Hi All, I hope every one is doing good In this article, we will know about the introduction of Flutter.