| 1 |
.. -*- rst -*- |
| 2 |
|
| 3 |
.. groonga-command |
| 4 |
.. database: functions_cast_loose |
| 5 |
|
| 6 |
``cast_loose`` |
| 7 |
================ |
| 8 |
|
| 9 |
Summary |
| 10 |
------- |
| 11 |
|
| 12 |
.. versionadded:: 8.0.8 |
| 13 |
|
| 14 |
``cast_loose`` cast loosely a string to the type specified. |
| 15 |
If the target string can cast, ``cast_loose`` has cast the string to the type specified by the argument. |
| 16 |
If the target string can't cast, ``cast_loose`` set the default value specified by the argument. |
| 17 |
|
| 18 |
Syntax |
| 19 |
------ |
| 20 |
|
| 21 |
``cast_loose`` has three parameters:: |
| 22 |
|
| 23 |
cast_loose(type, value, defaul_value) |
| 24 |
|
| 25 |
``type`` : Specify the type of after casted value. |
| 26 |
|
| 27 |
``value`` : Specify the target of a cast. |
| 28 |
|
| 29 |
``default_value`` : Specify the value of setting when failed a cast. |
| 30 |
|
| 31 |
Usage |
| 32 |
----- |
| 33 |
|
| 34 |
Here are a schema definition and sample data to show usage. |
| 35 |
|
| 36 |
.. groonga-command |
| 37 |
.. include:: ../../example/reference/functions/cast_loose/usage_setup.log |
| 38 |
.. table_create Data TABLE_HASH_KEY ShortText |
| 39 |
.. load --table Data |
| 40 |
.. [ |
| 41 |
.. {"_key": "100abc"} |
| 42 |
.. {"_key": "100"} |
| 43 |
.. ] |
| 44 |
|
| 45 |
|
| 46 |
The following example is cast "100" and "100abc" with ``cast_loose``. |
| 47 |
|
| 48 |
.. groonga-command |
| 49 |
.. include:: ../../example/reference/functions/cast_loose/usage_basic.log |
| 50 |
.. select Data --output_columns '_key, cast_loose(Int64, _key, 10)' |
| 51 |
|
| 52 |
``cast_loose`` cast "100" to 100 and "100abc" to 10. |
| 53 |
Because "100" can cast to the Int64 and "100abc" can't cast to the Int64. |
| 54 |
|
| 55 |
Return value |
| 56 |
------------ |
| 57 |
|
| 58 |
``cast_loose`` returns the casted value or default value. |
| 59 |
|
| 60 |
See also |
| 61 |
-------- |
| 62 |
|
| 63 |
* :doc:`/reference/commands/select` |