Skip to main content

Posts

Showing posts from December, 2014

SQL Server: PATINDEX String Functions in SQL Server

PATINDEX: The PATINDEX functions return the starting position of a pattern you specify.
You must include percent signs before and after the pattern,
Unless you are looking for the pattern as the first (omit the first %) or last (omit the last %) characters in a column or text.
Syntax:
PATINDEX ( '%pattern%' , expression )
Pattern: Pattern is a character expression that contains sequence of the characters. Wildcard characters can be used.
Expression: The expression that is searched for the specified pattern.
Example:
SELECTPATINDEX('%e____le%','This example of the Patindex');


Note: PATINDEX works just like LIKE, so you can use any of the wildcards. You do not have to enclose the pattern between percents. PATINDEX('a%', 'abc') returns 1 and PATINDEX('%a', 'cba') returns 3.
Unlike LIKE, PATINDEX returns a position, similar to what CHARINDEX does.

SQL Server: RIGHT String Functions in SQL Server

RIGHT: This function use when we want to select a Right part of the character string with the specified number of characters.
Syntax:
RIGHT ( character_expression , integer_expression )
character_expression: Character expression is the expression of character or binary data, except text or ntext, character_expression can be of any data type.
integer_expression: integer_expressionis a positive integer that specifies how many characters of the character_expression will be returned.
For example:
DECLARE@strVARCHAR(60)
SET@str='dilip Kumar Singh'
selectRIGHT(@str,5)ASRightFiveChar
Result:
RightFiveChar
-------------
Singh
(1 row(s) affected)