The SELECT
clause allows you to use the ::
syntax to specify the data type of a field and perform basic cast operations.
Data types
The data type of field values can be FLOAT, INTEGER, STRING, or BOOLEAN. The ::
syntax allows you to specify the data type of field values in a query.
Notes: In most cases, you do not need to specify the data type of field values in the
SELECT
clause. In most cases, TSDB for InfluxDB® rejects the write requests that attempt to write field values to a field that previously stores the field values of a different data type. The data types of field values may be different across shard groups. In these cases, you may need to specify the data type of the field values in theSELECT
clause.
Syntax
SELECT_clause <field_key>::<type> FROM_clause
Description
The valid values of the type
parameter are float
, integer
, string
, and boolean
. In most cases, if field_key
does not store the data of the specified type
, TSDB for InfluxDB® returns no data. For more information, see Cast operations.
Example
> SELECT "water_level"::float FROM "h2o_feet" LIMIT 4
name: h2o_feet
--------------
time water_level
2015-08-18T00:00:00Z 8.12
2015-08-18T00:00:00Z 2.064
2015-08-18T00:06:00Z 8.005
2015-08-18T00:06:00Z 2.116
The query returns the field values of the FLOAT data type for the water_level
field key.
Cast operations
The ::
syntax allows you to perform basic cast operations in queries. TSDB for InfluxDB® allows you to convert the data type of field values from INTEGER to FLOAT or from FLOAT to INTEGER.
Syntax
SELECT_clause <field_key>::<type> FROM_clause
Description
In the syntax, the valid values of the type
parameter are float
and integer
. If a query attempts to convert the data type of field values from INTEGER or FLOAT to STRING or BOOLEAN, TSDB for InfluxDB® returns no data.
Examples
Convert the data type of field values from FLOAT to INTEGER
> SELECT "water_level"::integer FROM "h2o_feet" LIMIT 4
name: h2o_feet
--------------
time water_level
2015-08-18T00:00:00Z 8
2015-08-18T00:00:00Z 2
2015-08-18T00:06:00Z 8
2015-08-18T00:06:00Z 2
The query casts the data type of the field values of water_level
from FLOAT to INTEGER and returns the results.
Cast the data type of field values from FLOAT to STRING (not supported)
> SELECT "water_level"::string FROM "h2o_feet" LIMIT 4
>
The query returns no data because TSDB for InfluxDB® does not allow you to convert the data type of field values from FLOAT to STRING.
InfluxDB® is a trademark registered by InfluxData, which is not affiliated with, and does not endorse, TSDB for InfluxDB®.