String functions

Preview:

Citation preview

Kumar Krishnan

Thanks to

Shivakumar .U

Sunil V.M

Lokesh

Spoorthi

Job John

Vinod Raj

Perform operations on a string (char or varchar) input value and return a string or numeric value.

In simple Terms the STRING FUNCTIONS are used to used primarily for string manipulation

ASCII

CHAR

SUBSTR-(SUBSTRING)

REPLACE

REPLICATE

UPPER

LEN-(LENGTH)

CHARINDEX

PATINDEX

LEFT

RIGHT

LOWER

LTRIM

STUFF

DIFFERENCE

CONCAT

UNICODE

SPACE

STR

UNICODE

SOUNDEX

QOUTENAME

NCHAR

RTRIM

REVERSE

ASCII : ASCII (character_expression)

Arguments: Is an expression of the type char or varchar.

Returns: INT

Ex: SELECT ASCII (‘A’) ------ Returns: 97

CHAR : CHAR (character_expression)

Arguments: Is an integer from 0 through 255. NULL is returned if the integer expression is not in this range.

Returns : CHAR

Ex: SELECT CHAR (101) ----- Returns: E.

CHARINDEX ( expressionToFind ,expressionToSearch , start location )

Arguments:

expressionToFind

Is a character expression that contains the sequence to be found. expressionToFind is limited to 8000 characters.

expressionToSearch

Is a character expression to be searched.

start location

Is an integer or bigint expression at which the search starts. If start location is not specified, is a negative number, or is 0, the search starts at the beginning of expressionToSearch.

Return Types:

bigint if expressionToSearch is of the varchar(max), nvarchar(max), or varbinary(max) data types; otherwise, int.

Ex: SELECT CHARINDEX ('k','kumar',1) ------ 1

CONCAT : CONCAT(string_value1,string_value2,[string value])

Arguments:

String value

A string value to concatenate to the other values.

Return:

String, the length and type of which depend on the input.

Ex : SELECT CONCAT ( 'Happy ', 'Birthday ', 11, '/', '25‘)

Note : CONCAT is Available in the SQL Server 2012

DIFFERENCE ( character_expression , character_expression)

Arguments:

character_expression

Is an alphanumeric expression of character data. character_expression can be a constant, variable, or column.

Returns:

INT

Ex: SELECT DIFFERENCE ( ‘Kumar’ , ‘umar’ ) ----- 3

The return value ranges from 0 through 4: 0 indicates weak or no similarity, and 4 indicates strong similarity or the same values.

LEFT : (character_expression , integer_expression)

Arguments:

character_expression

Is an expression of character or binary data.

integer_expression

Is a positive integer that specifies how many characters of the character_expression will be return

Return Types:

Returns varchar when character_expression is a non-Unicode character data type.

Returns nvarchar when character_expression is a Unicode character data type.

Ex : SELECT LEFT(‘KUMAR’,3) ----- KUM

RIGHT : RIGHT (character_expression , integer_expression)

Arguments

character_expression

Is an expression of character or binary data. character_expression can be a constant, variable, or column.

Integer_expression

Is a positive integer that specifies how many characters of character_expression will be returned

Return Types

Returns varchar when character_expression is a non-Unicode character data type.

Returns nvarchar when character_expression is a Unicode character data type.

Ex: SELECT RIGHT(‘KUMAR’,3) ----- MAR

LEN : LEN(( string expression )

Arguments

string expression

Is the string expression to be evaluated. string expression can be a constant, variable, or column of either character or binary data.

Return Types

bigint if expression is of the varchar(max), nvarchar(max) or varbinary(max) data types; otherwise, int.

EX: SELECT LEN ( ‘KUMAR’) ------ 5

LTRIM : LTRIM ( character_expression )

Arguments

character_expression

Is an expression of character or binary data. character_expression can be a constant, variable, or column.

Return Type

varchar or nvarchar

Ex: SELECT LTRIM (‘ KUMAR’) --- KUMAR

REPLACE: REPLACE( string,string_pattern,string_replacement)

Arguments

string_expression

Is the string expression to be searched. string_expression can be of a character or binary data type.

string pattern

Is the substring to be found. string pattern can be of a character or binary data type.

string replacement

Is the replacement string. string replacement can be of a character or binary data type.

Return Types

Returns nvarchar if one of the input arguments is of the nvarchar data type.

Ex: SELECT (‘KUMAR’,’U’,’UU’) ---- KUUMAR

SUBSTRING : SUBSTRING ( expression ,start , length )

Arguments:

Expression Is a character, binary, text, ntext, or image expression.

start

Is an integer or bigint expression that specifies where the returned characters start. If start is less than 1,

length

Is a positive integer or bigint expression that specifies how many characters of the expression will be returned.

Return Types

Returns character data if expression is one of the supported character data types

Ex: SELECT SUBSTRING ( ‘MY NAME IS KUMAR’ ,3,3) ---- NAM

STUFF:STUFF (character_expression, start, length, replace_exprn)

Arguments

Is an expression of character data. character_expression can be a constant, variable, or column of either character or binary data.

start

Is an integer value that specifies the location to start deletion and insertion

length

Is an integer that specifies the number of characters to delete

Replace expression

Is an expression of character data. character_expression can be a constant, variable, or column of either character or binary data

Ex: SELECT STUFF( ‘KUMAR’,1,3,’NAMASK’ ) ------NAMASKAR

Recommended