ToString (Azure Cosmos DB)ToString (Azure Cosmos DB)

返回标量表达式的字符串表示形式。Returns a string representation of scalar expression.

语法Syntax

ToString(<expr>)

参数Arguments

exprexpr
是任意标量表达式。Is any scalar expression.

返回类型Return types

返回一个字符串表达式。Returns a string expression.

示例Examples

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

SELECT 
    ToString(1.0000) AS str1, 
    ToString("Hello World") AS str2, 
    ToString(NaN) AS str3, 
    ToString(Infinity) AS str4,
    ToString(IS_STRING(ToString(undefined))) AS str5, 
    ToString(0.1234) AS str6, 
    ToString(false) AS str7, 
    ToString(undefined) AS str8

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

[{"str1": "1", "str2": "Hello World", "str3": "NaN", "str4": "Infinity", "str5": "false", "str6": "0.1234", "str7": "false"}]  

给定以下输入:Given the following input:

{"Products":[{"ProductID":1,"Weight":4,"WeightUnits":"lb"},{"ProductID":2,"Weight":32,"WeightUnits":"kg"},{"ProductID":3,"Weight":400,"WeightUnits":"g"},{"ProductID":4,"Weight":8999,"WeightUnits":"mg"}]}

以下示例演示 ToString 如何与其他字符串函数(如 CONCAT)一起使用。The following example shows how ToString can be used with other string functions like CONCAT.

SELECT 
CONCAT(ToString(p.Weight), p.WeightUnits) 
FROM p in c.Products 

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

[{"$1":"4lb" },
{"$1":"32kg"},
{"$1":"400g" },
{"$1":"8999mg" }]

给定以下输入。Given the following input.

{"id":"08259","description":"Cereals ready-to-eat, KELLOGG, KELLOGG'S CRISPIX","nutrients":[{"id":"305","description":"Caffeine","units":"mg"},{"id":"306","description":"Cholesterol, HDL","nutritionValue":30,"units":"mg"},{"id":"307","description":"Sodium, NA","nutritionValue":612,"units":"mg"},{"id":"308","description":"Protein, ABP","nutritionValue":60,"units":"mg"},{"id":"309","description":"Zinc, ZN","nutritionValue":null,"units":"mg"}]}

以下示例演示 ToString 如何与其他字符串函数(如 REPLACE)一起使用。The following example shows how ToString can be used with other string functions like REPLACE.

SELECT 
    n.id AS nutrientID,
    REPLACE(ToString(n.nutritionValue), "6", "9") AS nutritionVal
FROM food 
JOIN n IN food.nutrients

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

[{"nutrientID":"305"},
{"nutrientID":"306","nutritionVal":"30"},
{"nutrientID":"307","nutritionVal":"912"},
{"nutrientID":"308","nutritionVal":"90"},
{"nutrientID":"309","nutritionVal":"null"}]

备注Remarks

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

后续步骤Next steps