string()

Converts a value to a string, or formats a timestamp value using a specified format.

Syntax

string(EXPR)
string(DATE_EXPR, DATE_FMT[, TIMEZONE])

The str() function provides the same functionality.

Parameters

EXPR
The value or expression to convert to a string.
DATE_EXPR
A value or expression of timestamp type.
DATE_FMT
A date format string as defined by Java's SimpleDateFormat class. The following date pattern letters are supported.

Date pattern letters

LetterDescriptionExample
GEra designatorAD
yYear in calendar2025 (yyyy); 25 (yy)
MMonth in yearJuly (MMMM); Jul (MMM); 07 (MM), 7 (M)
wWeek in year27 (27th week of the year)
WWeek in month2 (second week of the month)
DDay in year10
dDay in month189
EDay of weekTuesday (EEEE); Tue (E, EE, EEE)
FDay of week in month2 (second occurrence of the weekday in the month)
uDay number of week (1=Monday, …, 7=Sunday)1
aAM/PM markerPM
HHour in day (0-23)0
kHour in day (1-24)24
KHour in AM/PM (0-11)0
hHour in AM/PM (1-12)12
mMinute in hour30
sSecond in minute55
SMillisecond978
zTime zone (general form)Pacific Standard Time; PST
ZTime zone (RFC 822 form)-0800
XTime zone (ISO 8601 form)-08;-0800;08:00
TIMEZONE
The time zone to use for output. Accepts offset formats such as "GMT+09", "GMT+0900", "GMT+09:00", abbreviations such as "KST", "UTC", "PST", or IANA time zone IDs. Refer to Time Zone Abbreviations – Worldwide List for time zone abbreviations.

Time zone abbreviation examples

AbbreviationGMT offsetDescription
UTCGMT+0Coordinated Universal Time
KSTGMT+9Korea Standard Time
CESTGMT+2Central European Summer Time
MSKGMT+3Moscow Standard Time
PSTGMT-7Pacific Standard Time
ESTGMT-5Eastern Standard Time

Description

The string() function converts the argument to a string and returns it. If the argument is null, returns null.

Conversion behavior by type:

  • Timestamp: Formatted using DATE_FMT and TIMEZONE. If DATE_FMT is not specified, the format "yyyy-MM-dd HH:mm:ss.SSS" is used.
  • IP address: Converted to a string by calling getHostAddress().
  • Array or list: Each element is recursively converted and returned in [value1,value2,...] format.
  • All other types: toString() is called.

Specifying an invalid date format string or an invalid time zone causes an error during the parsing phase.

Error codes

Error codeDescription
90841An invalid time zone was specified.
90842An invalid date format string was specified.

Usage examples

To prepare the WEB_APACHE_SAMPLE table used in these examples, refer to Preparing sample data.

  1. Converting the HTTP status code (integer) to a string

    table limit=5 WEB_APACHE_SAMPLE | eval result = string(status) | fields status, result
    | # result: "200", "404", etc.
    
  2. Formatting a timestamp in a specified format

    table limit=5 WEB_APACHE_SAMPLE | eval result = string(_time, "yyyy-MM-dd HH:mm:ss") | fields _time, result
    | # result: "2024-11-18 17:40:25", etc.
    
  3. Formatting a timestamp in a specific time zone

    table limit=5 WEB_APACHE_SAMPLE | eval result = string(_time, "yyyy-MM-dd HH:mm:ssZ", "UTC") | fields _time, result
    | # result: a date string in the specified time zone
    
  4. NULL input

    json "{'val': null}" | eval result = string(val)
    | # result: null
    

Compatibility

The string() function has been available since before Sonar 4.0.