cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
cancel
Showing results for 
Search instead for 
Did you mean: 

SQL week format issue its not showing result as 01(ww)

Ericsson
New Contributor II

Hi Folks,

I've requirement to show the week number as ww format. Please see the below code

select weekofyear(date_add(to_date(current_date, 'yyyyMMdd'), +35)). also plz refre the screen shot for result.result

2 REPLIES 2

Hubert-Dudek
Esteemed Contributor III

date_add id adding days. Today is 1st December and +35 days it gives 5th January which is first week of 2022 so result 1 is correct as returned by weekofyear function.

If you need just add leading zero the best way is to format it in visualization tool like / BI.

If you need leading zero in spark it will be have to be the string:

SELECT lpad(weekofyear(date_add(current_date, 35)), 2, '0')

It could be nice that date object is just format (date_format) as week number WW/ww but in spark is not included in date formatting: https://spark.apache.org/docs/latest/sql-ref-datetime-pattern.html

Lauri
New Contributor III

You can use lpad() to achieve the 'ww' format.

Welcome to Databricks Community: Lets learn, network and celebrate together

Join our fast-growing data practitioner and expert community of 80K+ members, ready to discover, help and collaborate together while making meaningful connections. 

Click here to register and join today! 

Engage in exciting technical discussions, join a group with your peers and meet our Featured Members.