Redigera

Dela via


String Functions

The following table lists string manipulation functions. An application can determine which string functions are supported by a driver by calling SQLGetInfo with an information type of SQL_STRING_FUNCTIONS.

Remarks

Arguments denoted as string_exp can be the name of a column,a character-string-literal, or the result of another scalar function, where the underlying data type can be represented as SQL_CHAR, SQL_VARCHAR, or SQL_LONGVARCHAR.

Arguments denoted as character_exp are a variable-length character string.

Arguments denoted as start, length, or count can be a numeric-literal or the result of another scalar function, where the underlying data type can be represented as SQL_TINYINT, SQL_SMALLINT, or SQL_INTEGER.

The string functions listed here are 1-based; that is, the first character in the string is character 1.

The BIT_LENGTH, CHAR_LENGTH, CHARACTER_LENGTH, OCTET_LENGTH, and POSITION string scalar functions have been added in ODBC 3.0 to align with SQL-92.

Function Description
ASCII( string_exp ) (ODBC 1.0) Returns the ASCII code value of the leftmost character of string_exp as an integer.
BIT_LENGTH( string_exp ) (ODBC 3.0) Returns the length in bits of the string expression.

Does not work only for string data types, therefore will not implicitly convert string_exp to string but instead will return the (internal) size of whatever datatype it is given.
CHAR( code ) (ODBC 1.0) Returns the character that has the ASCII code value specified by code. The value of code should be between 0 and 255; otherwise, the return value is data source-dependent.
CHAR_LENGTH( string_exp ) (ODBC 3.0) Returns the length in characters of the string expression, if the string expression is of a character data type; otherwise, returns the length in bytes of the string expression (the smallest integer not less than the number of bits divided by 8). (This function is the same as the CHARACTER_LENGTH function.)
CHARACTER_LENGTH( string_exp ) (ODBC 3.0) Returns the length in characters of the string expression, if the string expression is of a character data type; otherwise, returns the length in bytes of the string expression (the smallest integer not less than the number of bits divided by 8). (This function is the same as the CHAR_LENGTH function.)
CONCAT( string_exp1,string_exp2) (ODBC 1.0) Returns a character string that is the result of concatenating string_exp2 to string_exp1. The resulting string is DBMS-dependent. For example, if the column represented by string_exp1 contained a NULL value, DB2 would return NULL but SQL Server would return the non-NULL string.
DIFFERENCE( string_exp1,string_exp2) (ODBC 2.0) Returns an integer value that indicates the difference between the values returned by the SOUNDEX function for string_exp1 and string_exp2.
INSERT( string_exp1, start, length, string_exp2) (ODBC 1.0) Returns a character string where length characters have been deleted from string_exp1, beginning at start, and where string_exp2 has been inserted into string_exp, beginning at start.
LCASE( string_exp ) (ODBC 1.0) Returns a string equal to that in string_exp, with all uppercase characters converted to lowercase.
LEFT( string_exp, count) (ODBC 1.0) Returns the leftmost count characters of string_exp.
LENGTH( string_exp ) (ODBC 1.0) Returns the number of characters in string_exp, excluding trailing blanks.

LENGTH only accepts strings. Therefore will implicitly convert string_exp to a string, and return the length of this string (not the internal size of the datatype).
LOCATE( string_exp1, string_exp2[, start]) (ODBC 1.0) Returns the starting position of the first occurrence of string_exp1 within string_exp2. The search for the first occurrence of string_exp1 begins with the first character position in string_exp2 unless the optional argument, start, is specified. If start is specified, the search begins with the character position indicated by the value of start. The first character position in string_exp2 is indicated by the value 1. If string_exp1 is not found within string_exp2, the value 0 is returned.

If an application can call the LOCATE scalar function with the string_exp1, string_exp2, and start arguments, the driver returns SQL_FN_STR_LOCATE when SQLGetInfo is called with an Option of SQL_STRING_FUNCTIONS. If the application can call the LOCATE scalar function with only the string_exp1 and string_exp2 arguments, the driver returns SQL_FN_STR_LOCATE_2 when SQLGetInfo is called with an Option of SQL_STRING_FUNCTIONS. Drivers that support calling the LOCATE function with either two or three arguments return both SQL_FN_STR_LOCATE and SQL_FN_STR_LOCATE_2.
LTRIM( string_exp ) (ODBC 1.0) Returns the characters of string_exp, with leading blanks removed.
OCTET_LENGTH( string_exp ) (ODBC 3.0) Returns the length in bytes of the string expression. The result is the smallest integer not less than the number of bits divided by 8.

Does not work only for string data types, therefore will not implicitly convert string_exp to string but instead will return the (internal) size of whatever datatype it is given.
POSITION( character_exp IN character_exp) (ODBC 3.0) Returns the position of the first character expression in the second character expression. The result is an exact numeric with an implementation-defined precision and a scale of 0.
REPEAT( string_exp, count) (ODBC 1.0) Returns a character string composed of string_exp repeated count times.
REPLACE( string_exp1, string_exp2, string_exp3) (ODBC 1.0) Search string_exp1 foroccurrences of string_exp2, and replace with string_exp3.
RIGHT( string_exp, count) (ODBC 1.0) Returns the rightmost count characters of string_exp.
RTRIM( string_exp ) (ODBC 1.0) Returns the characters of string_exp with trailing blanks removed.
SOUNDEX( string_exp ) (ODBC 2.0) Returns a data source-dependent character string representing the sound of the words in string_exp. For example, SQL Server returns a 4-digit SOUNDEX code; Oracle returns a phonetic representation of each word.
SPACE( count ) (ODBC 2.0) Returns a character string consisting of count spaces.
SUBSTRING( string_exp, start, length**)** (ODBC 1.0) Returns a character string that is derived from string_exp, beginning at the character position specified by start for length characters.
UCASE( string_exp ) (ODBC 1.0) Returns a string equal to that in string_exp, with all lowercase characters converted to uppercase.