String Functions
String functions take various arguments and return various results.
-
CONCAT(STRING, STRING[, ...])
Returns a string consisting of eachSTRINGin 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 ofNEEDLEin HAYSTACK. Returns 0 if HAYSTACK does not containNEEDLE. Returns 1 ifNEEDLEis the empty string. -
INDEX(HAYSTACK, NEEDLES, NEEDLE_LEN)
RINDEX(HAYSTACK, NEEDLE, NEEDLE_LEN)
DividesNEEDLESinto multiple needles, each with lengthNEEDLE_LEN, which must be a positive integer that evenly divides the length ofNEEDLES. SearchesHAYSTACKfor 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 ifHAYSTACKdoes not contain any of the needles, or ifNEEDLESis the empty string. -
LENGTH(STRING)
Returns the number of bytes inSTRING. -
LOWER(STRING)
Returns a string identical toSTRINGexcept 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])
IfSTRINGis at leastLENGTHbytes long, these functions returnSTRINGunchanged. Otherwise, they returnSTRINGpadded withPADDINGon the left side (forLPAD) or right side (forRPAD) toLENGTHbytes. These functions report an error and returnSTRINGunchanged ifLENGTHis missing or bigger than 32767.The
PADDINGargument 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. IfPADDINGis omitted, these functions remove spaces (but not tabs or other white space). These functions returnSTRINGunchanged ifPADDINGis the empty string. -
NUMBER(STRING, FORMAT)
Returns the number produced whenSTRINGis interpreted according to format specifierFORMAT. If the format widthWis less than the length ofSTRING, then only the firstWbytes inSTRINGare used, e.g.NUMBER("123", F3.0)andNUMBER("1234", F3.0)both have value 123. IfWis greater thanSTRING's length, then it is treated as if it were right-padded with spaces. IfSTRINGis not in the correct format forFORMAT, system-missing is returned. -
REPLACE(HAYSTACK, NEEDLE, REPLACEMENT[, N])
Returns stringHAYSTACKwith instances ofNEEDLEreplaced byREPLACEMENT. If nonnegative integerNis specified, it limits the maximum number of replacements; otherwise, all instances ofNEEDLEare replaced. -
STRING(NUMBER, FORMAT)
Returns a string corresponding toNUMBERin 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 mostNbytes, then removing trailing spaces (but not tabs or other white space). Returns an empty string ifNis zero or negative, orSTRINGunchanged ifNis missing. -
SUBSTR(STRING, START)
Returns a string consisting of the value ofSTRINGfrom positionSTARTonward. Returns an empty string ifSTARTis system-missing, less than 1, or greater than the length ofSTRING. -
SUBSTR(STRING, START, COUNT)
Returns a string consisting of the firstCOUNTbytes fromSTRINGbeginning at positionSTART. Returns an empty string ifSTARTorCOUNTis system-missing, ifSTARTis less than 1 or greater than the number of bytes inSTRING, or ifCOUNTis less than 1. Returns a string shorter thanCOUNTbytes 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.