what is the bigquery equivalent to mysql variables like?
SET @fromdate = \'2014-01-01 00:00:00\', -- dates for after 2013
@todate=\'2015-01-01 00:00:00\',
Here is a solution using a user defined function. Declaring variables and calling them looks more like Mysql.
You can call your variables by using function var("your variable name")
this way:
#standardSQL
-- Set your variables here
CREATE TEMP FUNCTION var(str STRING)
RETURNS STRING
LANGUAGE js AS """
var result = {
'fromdate': '2014-01-01 00:00:00', // dates for after 2013
'todate': '2015-01-01 00:00:00',
'bfromdate': '2005-01-01 00:00:00', // dates for before 2013
'btodate': '2005-01-01 00:00:00',
'achfromdate': '2013-01-01 00:00:00', // dates for ACH without submit time in 2013
'achtodate': '2013-01-01 00:00:00',
'currency': 'USD',
'minimumamount': '3.50',
'default': 'undefined'
};
return result[str] || result['default'];
""";
-- Then use them by using the function var("your variable name")
SELECT *
FROM your_table
WHERE date_column BETWEEN var("fromdate") AND var("todate")
If your variable is not a string, set it as a string, call it with var and safe_cast it to your type:
#standardSQL
CREATE TEMP FUNCTION var(str STRING)
RETURNS STRING
LANGUAGE js AS """
var result = {
'minimumamount': '3.50',
'default': 'undefined'
};
return result[str] || result['default'];
""";
SELECT *
FROM your_table
WHERE amount > safe_cast(var("minimumamount") AS FLOAT64)
You may want to consider looking into BigQuery's Parameterized Queries. BigQuery supports query parameters to help prevent SQL injection when queries are constructed using user input. This feature is only available with standard SQL syntax.
https://cloud.google.com/bigquery/docs/parameterized-queries
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
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
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.