StringToNumber (Azure Cosmos DB)StringToNumber (Azure Cosmos DB)

适用于: SQL API

返回已转换为数字值的表达式。Returns expression translated to a Number. 如果表达式无法转换,则返回未定义的表达式。If expression cannot be translated, returns undefined.

语法Syntax

StringToNumber(<str_expr>)  

参数Arguments

str_exprstr_expr
是要解析为 JSON Number 表达式的字符串表达式。Is a string expression to be parsed as a JSON Number expression. JSON 中的数字必须是整数或浮点数。Numbers in JSON must be an integer or a floating point. 有关 JSON 格式的详细信息,请参阅 json.orgFor details on the JSON format, see json.org

返回类型Return types

返回一个数字表达式或未定义的表达式。Returns a Number expression or undefined.

示例Examples

以下示例演示 StringToNumber 在不同类型中的行为方式。The following example shows how StringToNumber behaves across different types.

只能在 Number 之前或之后使用空格。Whitespace is allowed only before or after the Number.

SELECT 
    StringToNumber("1.000000") AS num1, 
    StringToNumber("3.14") AS num2,
    StringToNumber("   60   ") AS num3, 
    StringToNumber("-1.79769e+308") AS num4

下面是结果集。Here is the result set.

{{"num1": 1, "num2": 3.14, "num3": 60, "num4": -1.79769e+308}}

在 JSON 中,有效的 Number 必须是整数或浮点数。In JSON a valid Number must be either be an integer or a floating point number.

SELECT   
    StringToNumber("0xF")

下面是结果集。Here is the result set.

{{}}

传递的表达式将会解析为 Number 表达式;以下输入不会计算为 Number 类型,因此会返回未定义。The expression passed will be parsed as a Number expression; these inputs do not evaluate to type Number and thus return undefined.

SELECT 
    StringToNumber("99     54"),   
    StringToNumber(undefined),
    StringToNumber("false"),
    StringToNumber(false),
    StringToNumber(" "),
    StringToNumber(NaN)

下面是结果集。Here is the result set.

{{}}

备注Remarks

此系统函数不会使用索引。This system function will not utilize the index.

后续步骤Next steps