7.15.2. cast_loose — Groonga v14.0.9 documentation

Skip to main content
`Ctrl`+`K`

# 7.15.2. `cast_loose`

## 7.15.2.1. Summary

Added in version 8.0.8.

`cast_loose` cast loosely a string to the type specified. If the target string can cast, `cast_loose` has cast the string to the type specified by the argument. If the target string can’t cast, `cast_loose` set the default value specified by the argument.

## 7.15.2.2. Syntax

`cast_loose` has three parameters:

    cast_loose(type, value, defaul_value)

`type` : Specify the type of after casted value.

`value` : Specify the target of a cast.

`default_value` : Speficy the value of setting when failed a cast.

## 7.15.2.3. Usage

Here are a schema definition and sample data to show usage.

Execution example:

    table_create Data TABLE_HASH_KEY ShortText
    # [[0,1337566253.89858,0.000355720520019531],true]
    load --table Data
    [
    {"_key": "100abc"}
    {"_key": "100"}
    ]
    # [[0,1337566253.89858,0.000355720520019531],2]

The following example is cast “100” and “100abc” with `cast_loose`.

Execution example:

    select Data   --output_columns '_key, cast_loose(Int64, _key, 10)'
    # [
    #   [
    #     0,
    #     1337566253.89858,
    #     0.000355720520019531
    #   ],
    #   [
    #     [
    #       [
    #         2
    #       ],
    #       [
    #         [
    #           "_key",
    #           "ShortText"
    #         ],
    #         [
    #           "cast_loose",
    #           null
    #         ]
    #       ],
    #       [
    #         "100abc",
    #         10
    #       ],
    #       [
    #         "100",
    #         100
    #       ]
    #     ]
    #   ]
    # ]

`cast_loose` cast “100” to 100 and “100abc” to 10. Because “100” can cast to the Int64 and “100abc” can’t cast to the Int64.

## 7.15.2.4. Return value

`cast_loose` returns the casted value or default value.

## 7.15.2.5. See also

- select