

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 间隔文本
<a name="Interval_literals"></a>

以下是使用 AWS Clean Rooms Spark SQL 支持的区间文字的规则。

使用间隔文本标识特定时间段（如 `12 hours` 或 `6 weeks`）。您可在涉及日期时间表达式的条件和计算中使用这些间隔文本。

**注意**  
不能对 AWS Clean Rooms 表中的列使用 INTERVAL 数据类型。

 间隔用 INTERVAL 关键字与数量和支持的日期部分的组合表示；例如 `INTERVAL '7 days'` 或 `INTERVAL '59 minutes'`。您可以将许多数量和单位连接在一起以形成更精确的间隔；例如：`INTERVAL '7 days, 3 hours, 59 minutes'`。还支持每个单位的缩写和复数；例如：`5 s`、`5 second` 和 `5 seconds` 是等效的间隔。

如果您未指定日期部分，则间隔值表示秒。您可指定数量值作为小数（例如：`0.5 days`）。

## 示例
<a name="section_interval-literals-examples"></a>

以下示例显示了具有不同间隔值的一系列计算。

以下示例向指定日期添加 1 秒。

```
select caldate + interval '1 second' as dateplus from date
where caldate='12-31-2008';
dateplus
---------------------
2008-12-31 00:00:01
(1 row)
```

以下示例向指定日期添加 1 分钟。

```
select caldate + interval '1 minute' as dateplus from date
where caldate='12-31-2008';
dateplus
---------------------
2008-12-31 00:01:00
(1 row)
```

以下示例向指定日期添加 3 小时 35 分钟。

```
select caldate + interval '3 hours, 35 minutes' as dateplus from date
where caldate='12-31-2008';
dateplus
---------------------
2008-12-31 03:35:00
(1 row)
```

以下示例向指定日期添加 52 周。

```
select caldate + interval '52 weeks' as dateplus from date
where caldate='12-31-2008';
dateplus
---------------------
2009-12-30 00:00:00
(1 row)
```

以下示例向指定日期添加 1 周、1 小时、1 分钟和 1 秒。

```
select caldate + interval '1w, 1h, 1m, 1s' as dateplus from date
where caldate='12-31-2008';
dateplus
---------------------
2009-01-07 01:01:01
(1 row)
```

以下示例向指定日期添加 12 小时（半天）。

```
select caldate + interval '0.5 days' as dateplus from date
where caldate='12-31-2008';
dateplus
---------------------
2008-12-31 12:00:00
(1 row)
```

以下示例将从 2023 年 3 月 31 日减去 4 个月，结果为 2022 年 11 月 30 日。计算时会考虑一个月中的天数。

```
select date '2023-03-31' - interval '4 months';

?column?
---------------------
2022-11-30 00:00:00
```

