Setting Big Query variables like mysql
You could use a WITH clause. It's not ideal, but it gets the job done.
-- Set your variables here
WITH vars AS (
SELECT '2018-01-01' as from_date,
'2018-05-01' as to_date
)
-- Then use them by pulling from vars with a SELECT clause
SELECT *
FROM your_table
WHERE date_column BETWEEN
CAST((SELECT from_date FROM vars) as date)
AND
CAST((SELECT to_date FROM vars) as date)
Or even less wordy:
#standardSQL
-- Set your variables here
WITH vars AS (
SELECT DATE '2018-01-01' as from_date,
DATE '2018-05-01' as to_date
)
-- Then use them by pulling from vars with a SELECT clause
SELECT *
FROM your_table, vars
WHERE date_column BETWEEN from_date AND to_date
You can now use variables in BigQuery. To run the statements that you provided, you need to use DECLARE
:
DECLARE fromdate TIMESTAMP DEFAULT '2014-01-01 00:00:00'; -- dates for after 2013
DECLARE todate TIMESTAMP DEFAULT '2015-01-01 00:00:00';
DECLARE bfromdate TIMESTAMP DEFAULT '2005-01-01 00:00:00'; -- dates for before 2013
DECLARE btodate TIMESTAMP DEFAULT '2005-01-01 00:00:00';
DECLARE achfromdate TIMESTAMP DEFAULT '2013-01-01 00:00:00'; -- dates for ACH without submit time in 2013
DECLARE achtodate TIMESTAMP DEFAULT '2013-01-01 00:00:00';
DECLARE currency STRING DEFAULT "USD";
You can use variables in statements after declaring them, e.g.:
DECLARE fromdate TIMESTAMP DEFAULT '2014-01-01 00:00:00'; -- dates for after 2013
DECLARE todate TIMESTAMP DEFAULT '2015-01-01 00:00:00';
SELECT FORMAT('From %t to %t', fromdate, todate);
See also the scripting documentation.
There are no 'variables' to be set in BigQuery, but you could add a feature request: https://code.google.com/p/google-bigquery/issues/list?q=label:Feature-Request