Notification

This Help Center is moving to Cloud. Learn more about the migration.

Trang bạn đã yêu cầu hiện không có sẵn bằng ngôn ngữ của bạn. Bạn có thể chọn ngôn ngữ khác ở cuối trang hoặc dịch nhanh mọi trang web sang ngôn ngữ mà bạn chọn bằng cách sử dụng tính năng dịch được tích hợp sẵn trong Google Chrome.

CAST

Converts a field or expression from one type to another type.

Sample usage

CAST(Number_field AS TEXT)

CAST(Text_field AS NUMBER)

CAST(Date_field as DATETIME)

Syntax

CAST(field_expression AS type)

Parameters

field_expression - a field or expression.

type - a data type literal.

CAST data type literals

You can cast from/to the following types:

From type To type
Text
  • DATE
  • DATETIME
  • NUMBER
Number
  • TEXT
Date
  • DATETIME
  • TEXT
Date & Time
  • DATETIME
  • TEXT

Examples

Goal: count website sessions longer than N seconds.

In a Google Analytics data source, Session Duration is a text dimension. The first step to counting sessions longer than N is to convert this to a number using CAST. The second step is to use the CASE statement to identify sessions longer than a certain value—we'll say 60 seconds for this example. We'll combine CASE with SUM to do the counting.

Step 1: Create a new calculated field Session Duration (number) with the following formula:

CAST(Session Duration AS NUMBER)

Step 2: Create a new calculated field Long Sessions:

SUM(CASE WHEN Session Duration > 60 THEN 1 ELSE 0 END)

Notes

You cannot CAST aggregated fields.

  • Casts from [DATE|DATETIME] to TEXT return the canonical string formats below. To return a different format, use the FORMAT_DATETIME function.
  • Casts from TEXT to [DATE|DATETIME] accept only the canonical string format. To parse a different format, use the PARSE_* functions.
Type Canonical STRING format
DATE YYYY-[M]M-[D]D
DATETIME YYYY-[M]M-[D]D [[H]H:[M]M:[S]S[.DDDDDD]]

Coercion to text

When an expression requires a text string, Looker Studio coerces (implicitly converts) non-string values to a string data type.

Type Canonical STRING format
DATE YYYY-MM-DD
DATETIME YYYY-MM-DD HH:MM:SS[.DDD[DDD]]

For example, suppose we have a created_datetime field of type DATETIME and the following formula:

CONCAT("Created on ", created_datetime)

This formula returns the following:

Created on 2019-11-22 16:30:00.739182

This formula doesn’t require an explicit CAST(created_datetime AS TEXT).

Thông tin này có hữu ích không?

Chúng tôi có thể cải thiện trang này bằng cách nào?

Bạn cần trợ giúp thêm?

Hãy thử các bước tiếp theo sau:

true
What's new in Looker Studio?

Learn about new features and recent changes. Looker Studio release notes live on Google Cloud.

Search
Clear search
Close search
Main menu
16051456753232825310
true
Tìm kiếm trong Trung tâm trợ giúp
true
true
true
true
true
102097
false
false