github.com/mithrandie/csvq@v1.18.1/docs/_posts/2006-01-02-cast-functions.md (about)

     1  ---
     2  layout: default
     3  title: Cast Functions - Reference Manual - csvq
     4  category: reference
     5  ---
     6  
     7  # Cast Functions
     8  
     9  | name                  | description                   |
    10  |:----------------------|:------------------------------|
    11  | [STRING](#string)     | Convert a value to a string   |
    12  | [INTEGER](#integer)   | Convert a value to an integer |
    13  | [FLOAT](#float)       | Convert a value to a float    |
    14  | [DATETIME](#datetime) | Convert a value to a datetime |
    15  | [BOOLEAN](#boolean)   | Convert a value to a boolean  |
    16  | [TERNARY](#ternary)   | Convert a value to a ternary  |
    17  
    18  ## Definitions
    19  
    20  ### STRING
    21  {: #string}
    22  
    23  ```
    24  STRING(value)
    25  ```
    26  
    27  _value_
    28  : [value]({{ '/reference/value.html' | relative_url }})
    29  
    30  _return_
    31  : [string]({{ '/reference/value.html#string' | relative_url }})
    32  
    33  Convert _value_ to a string.
    34  
    35  | Type     | Value after conversion                          |
    36  |:---------|:------------------------------------------------|
    37  | Integer  | String representing a decimal integer           |
    38  | Float    | String representing a floating-point decimal    |
    39  | Datetime | String formatted with RFC3339 with Nano Seconds |
    40  | Boolean  | 'true' or 'false'                               |
    41  | Ternary  | 'TRUE', 'FALSE' and 'UNKNOWN'                   |
    42  | Null     | Null                                            |
    43  
    44  
    45  ### INTEGER
    46  {: #integer}
    47  
    48  ```
    49  INTEGER(value)
    50  ```
    51  
    52  _value_
    53  : [value]({{ '/reference/value.html' | relative_url }})
    54  
    55  _return_
    56  : [integer]({{ '/reference/value.html#integer' | relative_url }})
    57  
    58  Convert _value_ to an integer.
    59  
    60  | Type     | Value                                                                  | Value after conversion                   |
    61  |:---------|:-----------------------------------------------------------------------|:-----------------------------------------|
    62  | String   | Representation of a decimal integer                                    | Integer represented by the string        |
    63  |          | Representation of a floating-point decimal or its exponential notation | Integer with decimal places rounded down |
    64  |          | Other values                                                           | Null                                     |
    65  | Float    | +Inf, -Inf, NaN                                                        | Null                                     |
    66  |          | Other values                                                           | Integer with decimal places rounded down |
    67  | Datetime |                                                                        | Integer representing its unix time       |
    68  | Boolean  |                                                                        | Null                                     |
    69  | Ternary  |                                                                        | Null                                     |
    70  | Null     |                                                                        | Null                                     |
    71  
    72  ### FLOAT
    73  {: #float}
    74  
    75  ```
    76  FLOAT(value)
    77  ```
    78  
    79  _value_
    80  : [value]({{ '/reference/value.html' | relative_url }})
    81  
    82  _return_
    83  : [float]({{ '/reference/value.html#float' | relative_url }})
    84  
    85  Convert _value_ to a float.
    86  
    87  | Type     | Value                                                                  | Value after conversion           |
    88  |:---------|:-----------------------------------------------------------------------|:---------------------------------|
    89  | String   | Representation of a floating-point decimal or its exponential notation | Float represented by the string  |
    90  |          | 'Inf', '+Inf'                                                          | +Inf                             |
    91  |          | '-Inf'                                                                 | -Inf                             |
    92  |          | 'NaN'                                                                  | NaN                              |
    93  |          | Other values                                                           | Null                             |
    94  | Integer  |                                                                        | Float equivalent to the integer  |
    95  | Datetime |                                                                        | Float representing its unix time |
    96  | Boolean  |                                                                        | Null                             |
    97  | Ternary  |                                                                        | Null                             |
    98  | Null     |                                                                        | Null                             |
    99  
   100  ### DATETIME
   101  {: #datetime}
   102  
   103  ```
   104  DATETIME(value [, timezone])
   105  ```
   106  
   107  _value_
   108  : [value]({{ '/reference/value.html' | relative_url }})
   109  
   110  _timezone_
   111  : [string]({{ '/reference/value.html#string' | relative_url }})
   112  
   113    _Local_, _UTC_ or a timezone name in the IANA TimeZone database(in the form of _"Area/Location"_. e.g. _"America/Los_Angeles"_).
   114    The default is the timezone set to the flag [_@@TIMEZONE_]({{ '/reference/flag.html' | relative_url }}).
   115  
   116    See: [--timezone option]({{ '/reference/command.html#options' | relative_url }})
   117  
   118  _return_
   119  : [datetime]({{ '/reference/value.html#datetime' | relative_url }})
   120  
   121  Convert _value_ to a datetime.
   122  
   123  | Type    | Value                                                                  | Value after conversion                                   |
   124  |:--------|:-----------------------------------------------------------------------|:---------------------------------------------------------|
   125  | String  | Datetime Formats                                                       | Datetime represented by the string                       |
   126  |         | Representation of a decimal integer                                    | Datetime represented by the integer value as a unix time |
   127  |         | Representation of a floating-point decimal or its exponential notation | Datetime represented by the float value as a unix time   |
   128  |         | Other values                                                           | Null                                                     |
   129  | Integer |                                                                        | Datetime represented by the integer value as a unix time |
   130  | Float   | +Inf, -Inf, NaN                                                        | Null                                                     |
   131  |         | Other values                                                           | Datetime represented by the float value as a unix time   |
   132  | Boolean |                                                                        | Null                                                     |
   133  | Ternary |                                                                        | Null                                                     |
   134  | Null    |                                                                        | Null                                                     |
   135  
   136  #### Format of string to be interpreted as datetime
   137  {: #format-of-string-as-datetime}
   138  
   139  Strings of the form passed by the [--datetime-format option]({{ '/reference/command.html#options' | relative_url }}) and defined in the [configuration files]({{ '/reference/command.html#configurations' | relative_url }}),  or the following forms can be converted to datetime values.
   140  
   141  | DateFormat | Example    |
   142  |:-----------|:-----------|
   143  | YYYY-MM-DD | 2012-03-15 |
   144  | YYYY/MM/DD | 2012/03/15 |
   145  | YYYY-M-D   | 2012-3-15  |
   146  | YYYY/M/D   | 2012/3/15  |
   147  
   148   
   149  
   150  | DatetimeFormat                            | Example                                                |
   151  |:------------------------------------------|:-------------------------------------------------------|
   152  | DateFormat                                | 2012-03-15                                             |
   153  | DateFormat hh:mm:ss(.NanoSecods)          | 2012-03-15 12:03:01<br />2012-03-15 12:03:01.123456789 |
   154  | DateFormat hh:mm:ss(.NanoSecods) ±hh:mm   | 2012-03-15 12:03:01 -07:00                             |
   155  | DateFormat hh:mm:ss(.NanoSecods) ±hhmm    | 2012-03-15 12:03:01 -0700                              |
   156  | DateFormat hh:mm:ss(.NanoSecods) TimeZone | 2012-03-15 12:03:01 PST                                |
   157  | YYYY-MM-DDThh:mm:ss(.NanoSeconds)         | 2012-03-15T12:03:01                                    |
   158  | RFC3339                                   | 2012-03-15T12:03:01-07:00                              |
   159  | RFC3339 with Nano Seconds                 | 2012-03-15T12:03:01.123456789-07:00                    |
   160  | RFC822                                    | 03 Mar 12 12:03 PST                                    |
   161  | RFC822 with Numeric Zone                  | 03 Mar 12 12:03 -0700                                  |
   162  
   163  > Timezone abbreviations such as "PST" may not work properly depending on your environment,
   164  > so you should use timezone offset such as "-07:00" as possible.
   165  
   166  ### BOOLEAN
   167  {: #boolean}
   168  
   169  ```
   170  BOOLEAN(value)
   171  ```
   172  
   173  _value_
   174  : [value]({{ '/reference/value.html' | relative_url }})
   175  
   176  _boolean_
   177  : [boolean]({{ '/reference/value.html#boolean' | relative_url }})
   178  
   179  Convert _value_ to a boolean.
   180  
   181  | Type     | Value             | Value after conversion |
   182  |:---------|:------------------|:-----------------------|
   183  | String   | '1', 't', 'true'  | true                   |
   184  | String   | '0', 'f', 'false' | false                  |
   185  | String   | Other values      | Null                   |
   186  | Integer  | 1                 | true                   |
   187  | Integer  | 0                 | false                  |
   188  | Integer  | Other values      | Null                   |
   189  | Float    | 1                 | true                   |
   190  | Float    | 0                 | false                  |
   191  | Float    | Other values      | Null                   |
   192  | Datetime |                   | Null                   |
   193  | Ternary  | TRUE              | true                   |
   194  |          | FALSE             | false                  |
   195  |          | UNKNOWN           | Null                   |
   196  | Null     |                   | Null                   |
   197  
   198  ### TERNARY
   199  {: #ternary}
   200  
   201  ```
   202  TERNARY(value)
   203  ```
   204  
   205  _value_
   206  : [value]({{ '/reference/value.html' | relative_url }})
   207  
   208  _return_
   209  : [ternary]({{ '/reference/value.html#ternary' | relative_url }})
   210  
   211  Convert _value_ to a ternary.
   212  
   213  | Type     | Value             | Value after conversion |
   214  |:---------|:------------------|:-----------------------|
   215  | String   | '1', 't', 'true'  | TRUE                   |
   216  | String   | '0', 'f', 'false' | FALSE                  |
   217  | String   | Other values      | UNKNOWN                |
   218  | Integer  | 1                 | TRUE                   |
   219  | Integer  | 0                 | FALSE                  |
   220  | Integer  | Other values      | UNKNOWN                |
   221  | Float    | 1                 | TRUE                   |
   222  | Float    | 0                 | FALSE                  |
   223  | Float    | Other values      | UNKNOWN                |
   224  | Datetime |                   | UNKNOWN                |
   225  | Boolean  | true              | TRUE                   |
   226  |          | false             | FALSE                  |
   227  | Null     |                   | UNKNOWN                |