Bigquery:Dateadd和Datediff

时间:2019-03-19 11:04:29

标签: google-bigquery sql-date-functions

我被困在下面:     我可以得到date_diff的子部分,但不能一起。     任何帮助表示赞赏。

SQL Server :
SELECT (DATEADD(DD, DATEDIFF(dd, 0, GETDATE()), 0)) 

Big Query : 
SELECT DATETIME_ADD(CURRENT_DATETIME(), INTERVAL DATE_DIFF(CURRENT_DATE, DATE '1900-01-01', DAY))

2 个答案:

答案 0 :(得分:1)

您可以使用此SQL作为示例

#standardSQL
        SELECT DATE_DIFF(DATE(CURRENT_DATETIME()), DATE_ADD(DATE(CURRENT_DATETIME()) ,INTERVAL 3 DAY), DAY),INTERVAL 3 DAY), DAY)
    FROM `project.dataset.table`

答案 1 :(得分:0)

Without a clear understanding of the use case, I'm going just to correct the query:

  1. You should specify time part when using some of the DATE or DATETIME functions. In this case, you need to specify DAY for DATETIME_ADD function.
  2. You can use DATE_ADD function if you do not need time.

So, this query is working as expected:

#standardSQL   
SELECT DATETIME_ADD(CURRENT_DATETIME(), INTERVAL DATE_DIFF(CURRENT_DATE, DATE '1900-01-01', DAY) DAY) as result_datetime,
       DATE_ADD(CURRENT_DATE, INTERVAL DATE_DIFF(CURRENT_DATE, DATE '1900-01-01', DAY) DAY) as result_date