'how to use date_add,Date_sub in bigquery

Query error: Cannot coerce expression (select DATE_ADD(C.DOB, interval 25 year) from statfinity-1.statfinity_sql_case_1.Customer C) to type STRING at [2:37]

declare greater_date string default (select DATE_ADD(C.DOB, interval 25 year) from statfinity-1.statfinity_sql_case_1.Customer C);
declare lower_date string default (select DATE_ADD(C.DOB, interval 35 year) from statfinity-1.statfinity_sql_case_1.Customer C);

SELECT SUM(A.total_amt) as net_total_revenue
from statfinity-1.statfinity_sql_case_1.Transaction A 
inner join statfinity-1.statfinity_sql_case_1.Customer C 
ON A.cust_id = C.customer_Id
where A.tran_date >= DATE_sub((max_date), interval 30 Day) AND 
(Select cast(A.tran_date as Date)) >= greater_date AND 
(Select cast(A.tran_date as Date)) <= lower_date;


Solution 1:[1]

Declare the variables as date, because you are comparing it as a date. Also, it should just be one value for those variables.

declare greater_date date default (select DATE_ADD(C.DOB, interval 25 year) from statfinity-1.statfinity_sql_case_1.Customer C limit 1);
declare lower_date date default (select DATE_ADD(C.DOB, interval 35 year) from statfinity-1.statfinity_sql_case_1.Customer C limit 1);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 khan