String Functions
String functions take various arguments and return various results.
-
CONCAT(STRING, STRING[, ...])
Returns a string consisting of eachSTRING
in sequence.CONCAT("abc", "def", "ghi")
has a value of"abcdefghi"
. The resultant string is truncated to a maximum of 32767 bytes. -
INDEX(HAYSTACK, NEEDLE)
RINDEX(HAYSTACK, NEEDLE)
Returns a positive integer indicating the position of the first (forINDEX
) or last (forRINDEX
) occurrence ofNEEDLE
in HAYSTACK. Returns 0 if HAYSTACK does not containNEEDLE
. Returns 1 ifNEEDLE
is the empty string. -
INDEX(HAYSTACK, NEEDLES, NEEDLE_LEN)
RINDEX(HAYSTACK, NEEDLE, NEEDLE_LEN)
DividesNEEDLES
into multiple needles, each with lengthNEEDLE_LEN
, which must be a positive integer that evenly divides the length ofNEEDLES
. SearchesHAYSTACK
for the occurrences of each needle and returns a positive integer indicating the byte index of the beginning of the first (forINDEX
) or last (forRINDEX
) needle it finds. Returns 0 ifHAYSTACK
does not contain any of the needles, or ifNEEDLES
is the empty string. -
LENGTH(STRING)
Returns the number of bytes inSTRING
. -
LOWER(STRING)
Returns a string identical toSTRING
except that all uppercase letters are changed to lowercase letters. The definitions of "uppercase" and "lowercase" are encoding-dependent. -
LPAD(STRING, LENGTH[, PADDING])
RPAD(STRING, LENGTH[, PADDING])
IfSTRING
is at leastLENGTH
bytes long, these functions returnSTRING
unchanged. Otherwise, they returnSTRING
padded withPADDING
on the left side (forLPAD
) or right side (forRPAD
) toLENGTH
bytes. These functions report an error and returnSTRING
unchanged ifLENGTH
is missing or bigger than 32767.The
PADDING
argument must not be an empty string and defaults to a space if not specified. If its length does not evenly fit the amount of space needed for padding, the returned string will be shorter thanLENGTH
. -
LTRIM(STRING[, PADDING])
RTRIM(STRING[, PADDING])
These functions returnSTRING
, after removing leading (forLTRIM
) or trailing (forRTRIM
) copies ofPADDING
. IfPADDING
is omitted, these functions remove spaces (but not tabs or other white space). These functions returnSTRING
unchanged ifPADDING
is the empty string. -
NUMBER(STRING, FORMAT)
Returns the number produced whenSTRING
is interpreted according to format specifierFORMAT
. If the format widthW
is less than the length ofSTRING
, then only the firstW
bytes inSTRING
are used, e.g.NUMBER("123", F3.0)
andNUMBER("1234", F3.0)
both have value 123. IfW
is greater thanSTRING
's length, then it is treated as if it were right-padded with spaces. IfSTRING
is not in the correct format forFORMAT
, system-missing is returned. -
REPLACE(HAYSTACK, NEEDLE, REPLACEMENT[, N])
Returns stringHAYSTACK
with instances ofNEEDLE
replaced byREPLACEMENT
. If nonnegative integerN
is specified, it limits the maximum number of replacements; otherwise, all instances ofNEEDLE
are replaced. -
STRING(NUMBER, FORMAT)
Returns a string corresponding toNUMBER
in the format given by format specifierFORMAT
. For example,STRING(123.56, F5.1)
has the value"123.6"
. -
STRUNC(STRING, N)
ReturnsSTRING
, first trimming it to at mostN
bytes, then removing trailing spaces (but not tabs or other white space). Returns an empty string ifN
is zero or negative, orSTRING
unchanged ifN
is missing. -
SUBSTR(STRING, START)
Returns a string consisting of the value ofSTRING
from positionSTART
onward. Returns an empty string ifSTART
is system-missing, less than 1, or greater than the length ofSTRING
. -
SUBSTR(STRING, START, COUNT)
Returns a string consisting of the firstCOUNT
bytes fromSTRING
beginning at positionSTART
. Returns an empty string ifSTART
orCOUNT
is system-missing, ifSTART
is less than 1 or greater than the number of bytes inSTRING
, or ifCOUNT
is less than 1. Returns a string shorter thanCOUNT
bytes ifSTART
+COUNT
- 1 is greater than the number of bytes inSTRING
. Examples:SUBSTR("abcdefg", 3, 2)
has value"cd"
;SUBSTR("nonsense", 4, 10)
has the value"sense"
. -
UPCASE(STRING)
ReturnsSTRING
, changing lowercase letters to uppercase letters.