Conversion and casting functions
Conversion functions convert the expression or value from one data type to another type.
Converting data types
Functions prefixed with as(Type)
ensure that a value is of a target type. If the data type is incompatible, they return null
. These functions are applicable after parsing.
1...2| parse content, "LD'DQL 'KVP{LD:key'='(LONG:valueLong| BOOLEAN:valueBoolean| [!;]*:valueStr)';'?}:q"3| fields timestamp, asLong(q[workTime])
asArray
asBoolean
asDouble
asDuration
asLong
asString
asSummaryStats
asTimeframe
asTimestamp
asIp()
The as*T
will try to cast the input to T
but won't perform any conversions, it will cause an exception for strongly typed fields (marked as ) and null for incompatible data types (empty cell).
asT(arg) => if(typeOf(arg) == ’T’, arg)
as*T | asNumber | asInteger | asLong | asDouble | asString | asBoolean | asTimestamp | asDuration | asTimeframe | asGeopoint | asIpAddress | asSummaryStats | asBinary | asArray | asRecord |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Integer | Integer | ||||||||||||||
Long | Long | ||||||||||||||
Double | Double | ||||||||||||||
String | |||||||||||||||
Boolean | |||||||||||||||
Timestamp | |||||||||||||||
Duration | |||||||||||||||
Timeframe | |||||||||||||||
Geopoint | |||||||||||||||
IpAddress | |||||||||||||||
Binary | |||||||||||||||
Array | |||||||||||||||
Record | |||||||||||||||
V<Integer> | V<Integer> | ||||||||||||||
V<Long> | V<Long> | ||||||||||||||
V<Double> | V<Double> | ||||||||||||||
V<String> | |||||||||||||||
V<Boolean> | |||||||||||||||
V<Timestamp> | |||||||||||||||
V<Duration> | |||||||||||||||
V<Timeframe> | |||||||||||||||
V<Geopoint> | |||||||||||||||
V<IpAddress> | |||||||||||||||
V<SummaryStats> | |||||||||||||||
V<Binary> | |||||||||||||||
V<Array> | |||||||||||||||
V<Record> | V<Record> |
asBoolean
Returns boolean value if the value is boolean
, otherwise, returns null.
-
Syntax
asBoolean(value)
-
Parameters
Name Type Mandatory Default Constraints Description value
boolean
yes
-
Example
1data record(a = true),2 record(a = "true"),3 record(a = 1)4| fieldsAdd type(a), asBoolean(a)Query result
a type(a) asBoolean(a) true
boolean
true
true
string
null
1
long
null
asDouble
Returns double
value if the value is double
, otherwise, returns null
.
-
Syntax
asDouble(value)
-
Parameters
Name Type Mandatory Default Constraints Description value
double
yes
-
Example
1data record(a = 3),2 record(a = 3.14),3 record(a = "3.14")4| fieldsAdd type(a), asDouble(a)Query result
a type(a) asDouble(a) 3
long
null
3.14
double
3.14
3.14
string
null
asIp
You can use this function to cast to an IP address.
-
Syntax
asIp(expression)
-
Parameters
Name Type Mandatory Default Constraints Description expression
string expression, ip address
yes
The expression to cast an expression to an IP address.
-
Example
1data record(a = ip("127.0.0.1")),2 record(a = "10.0.0.1")3| fieldsAdd type(a), asIp(a)Query result
a type(a) asIp(a) 127.0.0.1
ip
127.0.0.1
10.0.0.1
string
null
asLong
Returns long
value if the value is long
, otherwise null
.
-
Syntax
asLong(value)
-
Parameters
Name Type Mandatory Default Constraints Description value
long
yes
-
Example
1data record(a = 3),2 record(a = 3.14),3 record(a = "3")4| fieldsAdd type(a), asLong(a)Query result
a type(a) asLong(a) 3
long
3
3.14
double
null
3
string
null
asTimeframe
Returns timeframe
value if the value is timeframe
, otherwise returns null
.
-
Syntax
asTimeframe(value)
-
Parameters
Name Type Mandatory Default Constraints Description value
timeframe
yes
-
Example
1data record(timeframe = timeframe(from: "2019-08-01T09:30:00.000-0400", to: "2019-08-01T16:00:00.000-0400")),2 record(timeframe = timeframe(from: - 24h, to: - 2h)),3 record(timeframe = "2019-08-01T15:30:00.000-0400/2019-08-01T22:00:00.000-0400")4| fieldsAdd type(timeframe), asTimeframe(timeframe)Query result
timeframe type(timeframe) asTimeframe(timeframe) start:
2019-08-01T13:30:00.000Z
end:2019-08-01T20:00:00.000Z
timeframe
start:
2019-08-01T13:30:00.000Z
end:2019-08-01T20:00:00.000Z
start:
2023-11-16T10:05:42.927Z
end:2023-11-17T08:05:42.927Z
timeframe
start:
2023-11-16T10:05:42.927Z
end:2023-11-17T08:05:42.927Z
2019-08-01T15:30:00.000-0400/2019-08-01T22:00:00.000-0400
string
null
asTimestamp
Returns timestamp
value if the value is timestamp
, otherwise, returns null
.
-
Syntax
asTimestamp(value)
-
Parameters
Name Type Mandatory Default Constraints Description value
timestamp
yes
-
Example
1data record(timestamp = timestamp(2019, 8, 1, 13, 30, 0)),2 record(timestamp = 1564666200000000000),3 record(timestamp = "2019-08-01T09:30:00.000-0400")4| fieldsAdd type(timestamp), asTimestamp(timestamp)Query result
timestamp type(timestamp) asTimestamp(timestamp) 2019-08-01T13:30:00.000Z
timestamp
2019-08-01T13:30:00.000Z
1,564,666,200,000,000,000
long
null
2019-08-01T09:30:00.000-0400
string
null
asUid
Returns a uid
value if the value is uid
, otherwise returns null
.
-
Syntax
asUid(value)
-
Parameters
Name Type Mandatory Default Constraints Description value
uid
yes
The value to cast.
-
Example
1data record(a = uid64(123)), record(a = 123)2| fields asUid(a)Query result
asUid(a) asUid(a) 000000000000007b
null
decode
The decode function allows encoding binary data and strings into a string representation, and the opposite way. There are two types of decode functions, BASE64 and BASE16.
-
Syntax
decodeBase64ToBinary(expression)
decodeBase64ToString(expression)
decodeBase16ToBinary(expression)
decodeBase16ToString(expression)
-
Parameters
Name Type Mandatory Default Constraint Description expression
string
yes
An encoded string that needs to be decoded to a plain string or binary. Retrieves
NULL
if the encoding format does not match the outcome format. -
Example 1
1data record(content = "RFFMIGlzIGF3ZXNvbWUh"),2 record(content = "RHluYXRyYWNlIFF1ZXJ5IExhbmd1YWdl")3| fieldsAdd decodeBase64ToString(content)Query result
content decodeBase64ToString(content) RFFMIGlzIGF3ZXNvbWUh
DQL is awesome!
RHluYXRyYWNlIFF1ZXJ5IExhbmd1YWdl
Dynatrace Query Language
-
Example 2
1data record(content = "44514c20697320617765736f6d6521"),2 record(content = "44796e617472616365205175657279204c616e6775616765")3| fieldsAdd decodeBase16ToString(content)Query result
content decodeBase16ToString(content) 44514c20697320617765736f6d6521
DQL is awesome!
44796e617472616365205175657279204c616e6775616765
Dynatrace Query Language
encode
The encode function allows encoding binary data and strings into a string representation, and the opposite way. There are two types of encode functions, BASE64 and BASE16.
-
Syntax
encodeBase64(expression)
encodeBase16(expression)
-
Parameters: encode
Name Type Mandatory Default Constraint Description expression
string, binary
yes
A string or binary expression to encode.
-
Example
1data record(content = "DQL is awesome!"),2 record(content = "Dynatrace Query Language")3| fieldsAdd encodeBase16(content),4 encodeBase64(content)Query result
content encodeBase16(content) encodeBase64(content) DQL is awesome!
44514c20697320617765736f6d6521
RFFMIGlzIGF3ZXNvbWUh
Dynatrace Query Language
44796e617472616365205175657279204c616e6775616765
RHluYXRyYWNlIFF1ZXJ5IExhbmd1YWdl
hexStringToNumber
Converts a hexadecimal string to a number.
-
Syntax
hexStringToNumber(expression)
-
Parameters
Name Type Mandatory Default Constraints Description expression
string expression
yes
The string expression that will be converted to a number.
-
Example
1data record(a = "0x7f"),2 record(a = "100"),3 record(a = "0X80000000")4| fieldsAdd hexStringToNumber(a)Query result
a hexStringToNumber(a) 0x7f
127
100
256
0X80000000
2,147,483,648
numberToHexString
Converts a number to a hexadecimal string.
-
Syntax
numberToHexString(expression)
-
Parameters
Name Type Mandatory Default Consraints Descriptiont expression
numeric expression
yes
The numeric expression that will be converted to a hexadecimal string.
-
Example
1data record(a = 127),2 record(a = 256),3 record(a = 2147483648)4| fieldsAdd numberToHexString(a)Query result
a numberToHexString(a) 127
7f
256
100
2,147,483,648
80000000
toArray
Returns the value if it is an array
. Otherwise, converts a value to the single element array holding that value.
-
Syntax
toArray(value)
-
Parameters
Name Type Mandatory Default Constraints Description value
array, boolean, double, duration, ip, long, record, string, timeframe, timestamp
yes
-
Example
1data record(a = array(2, 3, 7, 7, 1)),2 record(a = "DQL is awesome!"),3 record(a = 3.14)4| fieldsAdd type(a), toArray(a)Query result
a type(a) toArray(a) [2, 3, 7, 7, 1]
array
[2, 3, 7, 7, 1]
DQL is awesome!
string
[DQL is awesome!]
3.14
double
[3.14]
toBoolean
Converts a value to Boolean if the value is of a suitable type. If the argument is an array, the element at position 0 is converted.
Use asBoolean(value)
function to return if the value is boolean
or variant<boolean>
, otherwise NULL.
Converts string values true
or TRUE
to a Boolean true
.The comparison is case insensitive. Converts other values to Boolean false
.
Converts numeric value 0 to Boolean false
. Converts other numeric values to Boolean true
.
-
Syntax
toBoolean(value)
-
Parameters
Name Type Mandatory Default Constraints Description value
boolean, double, long, string, array
yes
-
Example
1data record(a = true),2 record(a = "true"),3 record(a = "yes"),4 record(a = 0),5 record(a = 1),6 record(a = 2)7| fieldsAdd type(a), toBoolean(a)Query result
a type(a) toBoolean(a) true
boolean
true
true
string
true
yes
string
null
0
long
false
1
long
true
2
long
true
toDouble
Converts a value to double
if the value is of a suitable type. If the argument is an array
, the element at position 0 is converted.
Use asDouble(value)
function to return if the value is double
or variant<double>
, otherwise NULL.
-
Syntax
toDouble(value)
-
Parameters
Name Type Mandatory Default Constraints Description value
double, long, string, boolean, ip, timestamp, duration, array
yes
-
Example
1data record(a = 3),2 record(a = 3.14),3 record(a = "3.14"),4 record(a = true),5 record(a = toTimestamp("2019-08-01T09:30:00.000-0400")),6 record(a = 15s),7 record(a = ip("10.0.0.1"))8| fieldsAdd type(a), toDouble(a)Query result
a type(a) toDouble(a) 3
long
3
3.14
double
3.14
3.14
string
3.14
true
boolean
1
2019-08-01T13:30:00.000Z
timestamp
1,564,666,200,000,000,000
15 s
duration
15,000,000,000
10.0.0.1
ip
167,772,161
toDuration
Converts a value to duration
if the value is of a suitable type. If the argument is an array
, the element at position 0 is converted.
-
Syntax
toDuration(value)
-
Parameters
Name Type Mandatory Default Constraints Description value
duration, double, long, string, timeframe, array
yes
-
Example
1data record(a = 15s),2 record(a = 3.14),3 record(a = timeframe(from: "2019-08-01T09:30:00.000-0400", to: "2019-08-01T16:00:00.000-0400")),4 record(a = "42"),5 record(a = "42s")6| fieldsAdd type(a), toDuration(a)Query result
a type(a) toDuration(a) 15 s
duration
15 s
3.14
double
3 ns
start:
2019-08-01T13:30:00.000Z
end:2019-08-01T20:00:00.000Z
timeframe
6.5 h
42
string
42 ns
42s
string
null
toIp
You can use this function to convert an expression to an IP address.
-
Syntax
toIp(expression)
-
Parameters
Name Type Mandatory Default Constraints Description expression
string expression, ip address
yes
The expression to convert an expression to an IP address.
-
Example
1data record(a = ip("127.0.0.1")),2 record(a = "10.0.0.1"),3 record(a = "300.0.0.1"),4 record(a = 1234567890)5| fieldsAdd type(a), toIp(a)Query result
a type(a) toIp(a) 127.0.0.1
ip
127.0.0.1
10.0.0.1
string
10.0.0.1
300.0.0.1
string
null
1,234,567,890
long
73.150.2.210
toLong
Converts a value to long
if the value is of a suitable type. If the argument is an array
, the element at position 0 is converted.
-
Syntax
toLong(value)
-
Parameters
Name Type Mandatory Default Constraints Description value
long, double, string, boolean, ip, timestamp, duration, array
yes
-
Example
1data record(a = 3),2 record(a = 3.14),3 record(a = "3"),4 record(a = true),5 record(a = false),6 record(a = ip("10.0.0.1")),7 record(a = 15s),8 record(a = toTimestamp("2019-08-01T09:30:00.000-0400"))9| fieldsAdd type(a), toLong(a)Query result
a type(a) toLong(a) 3
long
3
3.14
double
3
3
string
3
true
boolean
1
false
boolean
0
10.0.0.1
ip
167,772,161
15 s
duration
15,000,000,000
2019-08-01T13:30:00.000Z
timestamp
1,564,666,200,000,000,000
toString
Returns the string representation of a value.
-
Syntax
toString(value)
-
Parameters
Name Type Mandatory Default Constraints Description value
double, boolean, timestamp, timeframe, duration, ip, array, record
yes
Parameter that should be transformed into text form.
-
Example
1data record(a = array(1, 2, 3)),2 record(a = true),3 record(a = 3),4 record(a = 3.14),5 record(a = 5m),6 record(a = toIp("127.0.0.1")),7 record(a = "DQL is awesome!"),8 record(a = timeframe(from: now() - 5m, to: now())),9 record(a = toTimestamp("2019-08-01T09:30:00.000-0400")),10 record(a = record(content = "A nested record"))11| fieldsAdd type(a), toString(a)Query result
a type(a) toString(a) [1, 2, 3]
array
[1, 2, 3]
true
boolean
true
3
long
3
3.14
double
3.14
5 min
duration
"05:00.000000000"
127.0.0.1
ip
"127.0.0.1"
DQL is awesome!
string
DQL is awesome!
start:
2023-11-17T10:00:43.724Z
end:2023-11-17T10:05:43.724Z
timeframe
"2023-11-17T10:00:43.724031180 +0000/2023-11-17T10:05:43.724031180 +0000"
2019-08-01T13:30:00.000Z
timestamp
"2019-08-01T13:30:00.000000000 +0000"
content:
A nested record
record
{"content":"A nested record"}
toTimeframe
Converts a value to timeframe
if the value is of a suitable type. If the argument is an array
, the element at position 0 is converted.
-
Syntax
toTimeframe(value)
-
Parameters
Name Type Mandatory Default Constraints Description value
timeframe, string, array
yes
-
Example
1data record(timeframe = timeframe(from: "2019-08-01T09:30:00.000-0400", to: "2019-08-01T16:00:00.000-0400")),2 record(timeframe = timeframe(from: - 24h, to: - 2h)),3 record(timeframe = "2019-08-01T09:30:00.000-0400/2019-08-01T16:00:00.000-0400")4| fieldsAdd type(timeframe), toTimeframe(timeframe)Query result
timeframe type(timeframe) toTimeframe(timeframe) start:
2019-08-01T13:30:00.000Z
end:2019-08-01T20:00:00.000Z
timeframe
start:
2019-08-01T13:30:00.000Z
end:2019-08-01T20:00:00.000Z
start:
2023-11-16T10:05:43.784Z
end:2023-11-17T08:05:43.784Z
timeframe
start:
2023-11-16T10:05:43.784Z
end:2023-11-17T08:05:43.784Z
2019-08-01T09:30:00.000-0400/2019-08-01T16:00:00.000-0400
string
start:
2019-08-01T13:30:00.000Z
end:2019-08-01T20:00:00.000Z
toTimestamp
Converts a value to timestamp
if the value is of a suitable type. If the argument is an ARRAY, the element at position 0 is converted.
Use asTimestamp(<value>)
function to return if the value is timestamp
or variant<timestamp>
, otherwise NULL.
-
Syntax
toTimestamp(value)
-
Parameters
Name Type Mandatory Default Constraints Description value
timestamp, double, long , string, array
yes
-
Example
1data record(timestamp = toTimestamp("2019-08-01T09:30:00.000-0400")),2 record(timestamp = 1564666200000000000),3 record(timestamp = "2019-08-01T09:30:00.000-0400")4| fieldsAdd type(timestamp), toTimestamp(timestamp)Query result
timestamp type(timestamp) toTimestamp(timestamp) 2019-08-01T13:30:00.000Z
timestamp
2019-08-01T13:30:00.000Z
1,564,666,200,000,000,000
long
2019-08-01T13:30:00.000Z
2019-08-01T09:30:00.000-0400
string
2019-08-01T13:30:00.000Z
toUid
Converts a value to uid
if the value is of a suitable type.
-
Syntax
toUid(value)
-
Parameters
Name Type Mandatory Default Constraints Description value
number, string, uid, array
yes
Any convertible value. If the argument is an
array
, the element at position 0 is converted. -
Example
1data record(a = "550e8400-e29b-41d4-a716-446655440000", b = 123, c = uid64(456), d=array(123))2| fields toUid(a), toUid(b), toUid(c), toUid(d)Query result
toUid(a) toUid(b) toUid(c) toUid(d) 550e8400-e29b-41d4-a716-446655440000
000000000000007bt
00000000000001c8
000000000000007b
type
Returns the type of a value as a string
.
-
Syntax
type(expression)
-
Parameters
Name Type Mandatory Default Constraints Description expression
array, boolean, double, duration, ip, long, record, string, timeframe, timestamp
yes
-
Example
1data record(a = array(1, 2, 3)),2 record(a = true),3 record(a = 3),4 record(a = 3.14),5 record(a = 5m),6 record(a = toIp("127.0.0.1")),7 record(a = "DQL is awesome!"),8 record(a = timeframe(from: now() - 5m, to: now())),9 record(a = toTimestamp("2019-08-01T09:30:00.000-0400")),10 record(a = record(content = "A nested record"))11| fieldsAdd type(a)Query result
a type(a) [1, 2, 3]
array
true
boolean
3
long
3.14
double
5 min
duration
127.0.0.1
ip
DQL is awesome!
string
start:
2023-11-17T10:00:43.967Z
end:2023-11-17T10:05:43.967Z
timeframe
2019-08-01T13:30:00.000Z
timestamp
content:
A nested record
record
uid128
Creates a uid
from the given two long expressions.
-
Syntax
uid128(firstExpression, secondExpression)
-
Parameters
Name Type Mandatory Default Constraints Description expression
long
yes
The first long expression for a uid.
expression
long
yes
The second long expression for a uid.
-
Example
1data record(a = 123, b = 456)2| fields uid128(a, b)Query result
uid128(a, b) 000000000000007b00000000000001c8
uid64
Creates a uid
from the given long expression.
-
Syntax
uid64(expression)
-
Parameters
Name Type Mandatory Default Constraints Description expression
long
yes
The long expression for a uid.
-
Example
1data record(a = 123)2| fields uid64(a)Query result
uid128(a, b) 000000000000007b00000000000001c8