A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P  Q  R  S  T  U  V  W  X  Y  Z  special characters  sybase-tech-blog


Category: ASE: Functions: String Functions

substring(), ASE String Function

The string function substring() returns a fragment of a string The length and position of the return string (return value) is specified in the parameters "start" and "length".

Syntax of the String Function substring()

    substring ( expression, start, length )
    

The parameters of the String Function substring()

"expression"
A part or substring of the value specified in the parameter "expression" is extracted and returned. The parameter must be set in single or double quotes. It can be a column name, variable or constant and must be of one of the datatypes char, varchar, nchar, nvarchar, binary or varbinary If the value of the parameter "expression" is NULL then the return value is also NULL.

"start"
The parameter "start" specifies the position of the first character of the string to be returned. A negative value for the parameter "start" returns NULL.

"length"
The parameter "length" specifies the length of the fragment to be returned, i.e. it specifies the length of the substring. A negative value for the parameter "length" returns NULL.

Example for the String Function substring()

    select substring("abcdefg", 2, 3)
go
------
bcd

Returns the string fragment "bcd".

    select substring("abcdefg", -2, 3)
go
------
NULL

Returns NULL.

    select substring("abcdefg", 2, NULL)
go
------
NULL

Returns NULL.

    select substring("abcdefg", NULL, 3)
go
------
NULL

Returns NULL.

    select substring(NULL, 2, 3)
go
------
NULL

Returns NULL.

    select substring("abcdefg", 2, -3)
go
Msg 536, Level 16, State 1:
Server 'ASE_SERVER_NAME', Line 1:
Invalid length parameter with value of -3 passed to the substring function.
Command has been aborted.
------
(0 rows affected)

Returns an error message.

See also:

ASE T-SQL - Aggregate Functions,
ascii(),
char(),
char(n),
charindex(),
char_length(),
compare(),
difference(),
lower(),
ltrim(),
patindex(),
replicate(),
reverse(),
right(),
rtrim(),
sortkey(),
soundex(),
space(),
str(),
String Functions,
stuff(),
to_unichar(),
uhighsurr(),
ulowsurr(),
upper(),
uscalar().