什么是“:: date”在redshift中的应用?

时间:2022-04-07 01:52:06

I see somewhere in the sql code in Amazon redshift that ::date is used when comparing two dates. I am wondering is there any difference between these three lines of code:

我在Amazon redshift的sql代码中找到了某个地方::比较两个日期时使用了date。我想知道这三行代码之间有什么区别:

start_date < '2016-01-01'
start_date < '2016-01-01'::date
start_date < date('2016-01-01')

1 个解决方案

#1


3  

The result is the same in all three cases.

结果在所有三种情况下都是相同的。

Specifically:

特别:

  • start_date < '2016-01-01' is trying to compare a date with a varchar, but Amazon Redshift is smart enough to convert the varchar into a date format for comparison purposes.

    start_date <'2016-01-01'正在尝试将日期与varchar进行比较,但Amazon Redshift足够聪明,可以将varchar转换为日期格式以进行比较。

  • start_date < '2016-01-01'::date is doing a proper comparison between two date fields. This would be equivalent to date '2016-01-01'.

    start_date <'2016-01-01':: date正在两个日期字段之间进行适当的比较。这相当于日期'2016-01-01'。

  • start_date < date('2016-01-01') appears to also be comparing date fields, although that syntax isn't in the Date and Time Functions documentation.

    start_date ('2016-01-01')似乎也在比较日期字段,尽管该语法不在日期和时间函数文档中。

A more useful example of using ::date is when comparing two timestamps, and you only wish to compare the date, for example:

使用:: date的一个更有用的例子是比较两个时间戳,你只想比较日期,例如:

select end::date - start::date as days FROM table

#1


3  

The result is the same in all three cases.

结果在所有三种情况下都是相同的。

Specifically:

特别:

  • start_date < '2016-01-01' is trying to compare a date with a varchar, but Amazon Redshift is smart enough to convert the varchar into a date format for comparison purposes.

    start_date <'2016-01-01'正在尝试将日期与varchar进行比较,但Amazon Redshift足够聪明,可以将varchar转换为日期格式以进行比较。

  • start_date < '2016-01-01'::date is doing a proper comparison between two date fields. This would be equivalent to date '2016-01-01'.

    start_date <'2016-01-01':: date正在两个日期字段之间进行适当的比较。这相当于日期'2016-01-01'。

  • start_date < date('2016-01-01') appears to also be comparing date fields, although that syntax isn't in the Date and Time Functions documentation.

    start_date ('2016-01-01')似乎也在比较日期字段,尽管该语法不在日期和时间函数文档中。

A more useful example of using ::date is when comparing two timestamps, and you only wish to compare the date, for example:

使用:: date的一个更有用的例子是比较两个时间戳,你只想比较日期,例如:

select end::date - start::date as days FROM table