在雅典娜中施法和左转功能



cast和convert函数在Athena中按预期工作:

SELECT code_2 as mydate,  cast( code_2 as varchar) from some_table   

但是如何提取最左边的 8 个字符?这将引发错误:

SELECT code_2 as mydate,  left(cast( code_2 as varchar),8) as date from some_table

这是错误:

无关的输入"左"期望

尝试直接投射到VARCHAR(8)

SELECT
code_2 AS mydate,
CAST(code_2 AS VARCHAR(8))
FROM some_table;

我从未使用过雅典娜,但文档暗示这应该有效。 这个技巧适用于Oracle,SQL Server,Postgres和MySQL。

如果code_2是一个字符串,则使用substr()

select code_2 as mydate, substr(code_2, 1, 8) as date
from some_table;

如果code_2是日期,则使用适当的日期函数:

select code_2 as mydate,
date_format(code_2, '%Y-%m-%d') as date
from some_table;

对数据类型使用适当的函数。 当有内置函数完全按照您想要的方式并为您提供更多控制时,不要将日期转换为字符串。

最新更新