How To Extract Month From Date In Bigquery

Hey there, data wranglers! Ever found yourself staring at a BigQuery table overflowing with dates, desperately needing to pluck out just the month? Like, maybe you want to analyze sales trends by month, or track website traffic patterns, or just figure out which month everyone's birthday is (for optimal cake planning, obviously!). Don't worry, it's easier than untangling headphones – promise!
Let's dive in! BigQuery's got your back with a few nifty functions that'll make extracting the month a piece of cake. We're going to explore two main approaches:
The EXTRACT Function: A Classic Approach
Think of EXTRACT as your universal Swiss Army knife for date parts. It's super versatile and lets you grab almost anything from a date or timestamp. To get the month, you simply tell it what you want (the month!) and where to get it from (your date column!).
Must Read
Here's the basic syntax:
SELECT EXTRACT(MONTH FROM your_date_column) AS month_number FROM your_table;
See? Simple as pie! Let's break it down:

EXTRACT(MONTH FROM your_date_column): This is where the magic happens. We're telling BigQuery to extract the month from theyour_date_column. Make sureyour_date_columnis actually aDATEorTIMESTAMPtype, or you might get a grumpy error.AS month_number: This renames the resulting column tomonth_number. Because "EXTRACT(MONTH FROM your_date_column)" is not the snappiest column name. Help your future self out!FROM your_table: Tells BigQuery which table to get the data from. Pretty straightforward.
The EXTRACT function returns the month as a number (1 for January, 2 for February, and so on). If you want the month name instead, hold on to your hat – we're getting there!
The FORMAT_DATE Function: For Style and Substance
If you want the month name (e.g., "January", "February"), FORMAT_DATE is your go-to function. It lets you specify a format string to control how the date is displayed. It’s like giving your date a makeover!

Here's the basic syntax:
SELECT FORMAT_DATE('%B', your_date_column) AS month_name FROM your_table;
Let's dissect this beauty:
FORMAT_DATE('%B', your_date_column): This formats theyour_date_columnaccording to the format string'%B'. The%Bis the key – it tells BigQuery to return the full month name. Think of it as a secret code!AS month_name: Again, renaming the column for clarity. Because who wants a column named after a format function? No one, that's who.FROM your_table: Same as before, specifying the table.
You can use different format codes to get variations of the month name:

%b: Abbreviated month name (e.g., "Jan", "Feb"). Perfect if you're short on space or just feeling concise.%m: Month as a zero-padded number (e.g., "01", "02"). Useful for sorting purposes.
Important Note: FORMAT_DATE returns a string, while EXTRACT returns an integer. Keep this in mind when using the results in calculations or comparisons. If you need to do math, stick with EXTRACT.
Putting it all together
Let's say you have a table called orders with a column named order_date. Here's how you'd extract both the month number and the month name:

SELECT
EXTRACT(MONTH FROM order_date) AS month_number,
FORMAT_DATE('%B', order_date) AS month_name
FROM
orders;
Boom! You now have a table with both the numeric month and the month name. Go forth and analyze!
Pro Tip: Remember to always double-check the data type of your date column. If it's stored as text, you might need to use PARSE_DATE or SAFE_PARSE_DATE to convert it to a proper date format first. But that's a story for another day!
So there you have it! Extracting the month from a date in BigQuery is a breeze. With these functions in your toolbox, you're ready to conquer any date-related challenge that comes your way. Now go forth, analyze, and create something amazing! And don't forget to treat yourself to some cake. You've earned it!
