Skip to content
Advertisement

Snowflake error when using datetime variable in a query

I am trying to query a database table for a specific date range. When the dates are hardcoded, the query works without any problem, however, when I try to use the variables, Snowflake is returning an error message “”, there is no additional information.

I have tried using Date, DateTime, DateTimeLTZ, Varchar datatypes but so far nothing has worked. Snowflake error messages are not as friendly as SQL Server or Oracle, documentation doesn’t offer much help either.

Here is a sample code to replicate the issue, any help or guidance is highly appreciated

Advertisement

Answer

To use variable in this context, it should be prefixed with : and it is documented at Using a Variable in a SQL Statement (Binding):

You can use a variable in a SQL statement. (This is sometimes referred to as binding a variable.) Prefix the variable name with a colon. For example:

Note that if you are using a variable in an expression or with a Snowflake Scripting language element (e.g. RETURN), you do not need to prefix the variable with a colon.

The code:

Output – Snowsight (please note a different color of variables):

enter image description here


User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement