Get week of the year after setting start day as Sunday

I am trying to get the week of the year after setting start of the week as Sunday.

This is required so I can mirror the Teradata logic in my python script. In Teradata the week start from Sunday. So below query returns -

select WEEKNUMBER_OF_YEAR(to_date('2019-06-01'))
21

But when I run that in python it returns 22

import datetime
datetime.date(2019, 6, 1).isocalendar()[1]

I tried to set the Sunday as first day of the Week, but same result

import calendar
import datetime
calendar.setfirstweekday(calendar.SUNDAY)
datetime.date(2019, 6, 1).isocalendar()[1]

Any help?

Upvotes: 0

Views: 70

Answers (1)

dnoeth
dnoeth

Reputation: 60462

You probably want strftime("%U")

Week number of the year (Sunday as the first day of the week) as a zero padded decimal number. All days in a new year preceding the first Sunday are considered to be in week 0.

To match Python's isocalendar() you can switch to ISO in Teradata, too:

WEEKNUMBER_OF_YEAR(to_date('2019-06-01'), 'ISO')

Btw, there's no reason to write to_date('2019-06-01'), simply use a date literal instead: DATE '2019-06-01'

Upvotes: 1

Related Questions