Bird
Raised Fist0
SnowflakeHow-ToBeginner · 3 min read

How to Use Cron Syntax in Snowflake Tasks for Scheduling

To use cron in a Snowflake Task, specify the schedule using the USING CRON clause with a standard cron expression. This expression defines when the task runs, such as every hour or daily at a specific time, and must be in UTC time zone.
📐

Syntax

The cron schedule for a Snowflake task uses the USING CRON clause followed by a cron expression string. The cron expression has five fields representing minute, hour, day of month, month, and day of week. The schedule runs in UTC time.

  • minute: 0-59
  • hour: 0-23
  • day of month: 1-31
  • month: 1-12
  • day of week: 0-6 (Sunday=0)

Example: USING CRON '0 * * * *' UTC runs the task at the start of every hour.

sql
CREATE OR REPLACE TASK my_task
  WAREHOUSE = my_warehouse
  SCHEDULE = USING CRON '<cron_expression>' UTC
AS
  <sql_statement>;
💻

Example

This example creates a task that runs every day at 2:30 AM UTC and inserts the current timestamp into a table.

sql
CREATE OR REPLACE TABLE task_log (run_time TIMESTAMP);

CREATE OR REPLACE TASK daily_task
  WAREHOUSE = my_warehouse
  SCHEDULE = USING CRON '30 2 * * *' UTC
AS
  INSERT INTO task_log VALUES (CURRENT_TIMESTAMP());

-- To start the task
ALTER TASK daily_task RESUME;
Output
Task daily_task created and scheduled to run daily at 2:30 AM UTC.
⚠️

Common Pitfalls

Common mistakes when using cron in Snowflake tasks include:

  • Not specifying UTC timezone, which causes confusion about run times.
  • Using six-field cron expressions (including seconds), which Snowflake does not support.
  • Incorrect cron syntax causing task creation errors.
  • Forgetting to RESUME the task after creation, so it does not run.
sql
/* Wrong: Using seconds field (6 fields) and missing UTC */
CREATE OR REPLACE TASK wrong_task
  WAREHOUSE = my_warehouse
  SCHEDULE = '0 30 2 * * *'
AS
  SELECT 1;

/* Right: 5 fields and UTC specified */
CREATE OR REPLACE TASK correct_task
  WAREHOUSE = my_warehouse
  SCHEDULE = USING CRON '30 2 * * *' UTC
AS
  SELECT 1;
📊

Quick Reference

Cron FieldAllowed ValuesDescription
Minute0-59Minute of the hour when task runs
Hour0-23Hour of the day (UTC)
Day of Month1-31Day of the month
Month1-12Month of the year
Day of Week0-6Day of the week (Sunday=0)
✅

Key Takeaways

Use the USING CRON clause with a 5-field cron expression and specify UTC timezone.
Cron expressions in Snowflake tasks do not support seconds; use minute to day-of-week fields only.
Always RESUME your task after creation to enable scheduling.
Check your cron syntax carefully to avoid errors during task creation.
Remember Snowflake task schedules run in UTC, adjust your cron accordingly.