Bigquery比较日期和使用case语句时

时间:2016-04-11 23:15:16

标签: datetime case google-bigquery

我目前有以下案例陈述:

CASE WHEN TIMESTAMP_TO_USEC(TIMESTAMP)>=TIMESTAMP_TO_USEC(TIMESTAMP('03-14-2016 00:00:00')) THEN 'afterChanges' ELSE 'beforeChanges' END AS beforeOrAfter

我也试过各种变体:

CASE WHEN TIMESTAMP>TIMESTAMP('03-14-2016 00:00:00') THEN 'afterChanges' ELSE 'beforeChanges' END AS beforeOrAfter

CASE WHEN ROUND(DATEDIFF(TIMESTAMP, TIMESTAMP('03-14-2016 00:00:00')),0) >0 THEN 'afterChanges' ELSE 'beforeChanges' END AS beforeOrAfter

然而,无论最初的TIMESTAMP如何,我总是得到其他的(" beforeChanges")。有什么建议吗?

1 个答案:

答案 0 :(得分:2)

所有应该如下工作 不使用CURRENT_TIMESTAMP()而不仅仅使用TIMESTAMP

SELECT 
  CASE 
    WHEN TIMESTAMP_TO_USEC(CURRENT_TIMESTAMP()) >= TIMESTAMP_TO_USEC(TIMESTAMP('2016-03-04 00:00:00')) THEN 'afterChanges' 
    ELSE 'beforeChanges' 
  END AS beforeOrAfter1,
  CASE 
    WHEN CURRENT_TIMESTAMP() >= TIMESTAMP('2016-03-04 00:00:00') THEN 'afterChanges' 
    ELSE 'beforeChanges' 
  END AS beforeOrAfter2,
  CASE 
    WHEN CURRENT_DATE() >= DATE('2016-03-04 00:00:00') THEN 'afterChanges' 
    ELSE 'beforeChanges' 
  END AS beforeOrAfter3,