cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results for 
Search instead for 
Did you mean: 

Convert Week of Year to Month in SQL?

ST
New Contributor II

Hi all,

Was wondering if there was any built in function or code that I could utilize to convert a singular week of year integer (i.e. 1 to 52), into a value representing month (i.e. 1-12)?

The assumption is that a week start on a Monday and end on a Sunday.

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions

Hubert-Dudek
Esteemed Contributor III

we need old parser as new doesn't support weeks. Than we can map what we need using w - year of year and u - first day of the week:

spark.sql("set spark.sql.legacy.timeParserPolicy=LEGACY")
spark.sql("""
SELECT 
    extract(
        month from to_date("2022-12-1", "y-w-u")
     ) as month_of_year
 """)

image

View solution in original post

1 REPLY 1

Hubert-Dudek
Esteemed Contributor III

we need old parser as new doesn't support weeks. Than we can map what we need using w - year of year and u - first day of the week:

spark.sql("set spark.sql.legacy.timeParserPolicy=LEGACY")
spark.sql("""
SELECT 
    extract(
        month from to_date("2022-12-1", "y-w-u")
     ) as month_of_year
 """)

image

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you won’t want to miss the chance to attend and share knowledge.

If there isn’t a group near you, start one and help create a community that brings people together.

Request a New Group