find_cyber_week - BigQuery

Function

find_cyber_week(date)

Description

Find Cyber Week - Determines if a <date> falls within Cyber Week and identifies the specific day (e.g., Thanksgiving, Black Friday).

Example Query


SELECT `justfunctions.eu.find_cyber_week`("2023-11-24")
                                            
/*--Output--
1 | Black Friday
*/

Statement

CREATE OR REPLACE FUNCTION `your_project_id.your_dataset_id.find_cyber_week`(`date` DATE) 
  RETURNS STRUCT<is_cyber_week INT64, day_name STRING> AS ((
  WITH calculated_dates AS (
    SELECT 
      DATE_ADD(DATE_TRUNC(DATE(EXTRACT(YEAR FROM date), 11, 1), MONTH), 
        INTERVAL (26 - EXTRACT(DAYOFWEEK FROM DATE(EXTRACT(YEAR FROM date), 11, 1))) DAY) AS thanksgiving
  )
  SELECT 
    STRUCT(
    IF(date BETWEEN thanksgiving AND DATE_ADD(thanksgiving, INTERVAL 4 DAY), 1, 0),
    CASE 
      WHEN date = thanksgiving THEN 'Thanksgiving'
      WHEN date = DATE_ADD(thanksgiving, INTERVAL 1 DAY) THEN 'Black Friday'
      WHEN date = DATE_ADD(thanksgiving, INTERVAL 2 DAY) OR date = DATE_ADD(thanksgiving, INTERVAL 3 DAY) THEN 'Thanksgiving Weekend'
      WHEN date = DATE_ADD(thanksgiving, INTERVAL 4 DAY) THEN 'Cyber Monday'
      ELSE NULL
    END)
  FROM calculated_dates
))
  OPTIONS ( description = '''Determines if a <date> falls within Cyber Week and identifies the specific day (e.g., Thanksgiving, Black Friday).''')

Regions

justfunctions.eu.find_cyber_week(date),
justfunctions.us.find_cyber_week(date)

Type

SQL User Defined Function (SQL UDF)

How to Use

Frequently Asked Questions

User-Defined Functions (UDFs) in Google BigQuery are custom functions that you can create to perform operations that aren't available through the standard SQL functions. These UDFs allow you to extend BigQuery's SQL capabilities to suit your specific data processing needs. JustFunctions is a collection of open-source user-defined functions (UDFs).

JustFunctions is a collection of open-source User-Defined Functions (UDFs) designed to extend the capabilities of Google BigQuery. These functions cover a wide range of applications, including text manipulation, URL processing, date processing, email handling, similarity measures, and more. Moreover, JustFunctions is frequently updated to include more use cases.

We welcome any feedback or questions you may have. You can Contact us or report an issue on Github.

Functions and procedures from JustFunctions can be used directly in any of your projects.
To start, simply click 👆 on any function,  Copy  the 'Example Query' and run it in your BigQuery console.
You can also  Copy  the 'Statement' to create your own private user-defined function.

Yes, JustFunctions is completely free to use.

Yes, currently JustFunctions is only available for Google BigQuery. In the future, we will also support PostgreSQL.

See something wrong? Contact us or report an issue on Github.