Skip to content

Commit 4aacfd4

Browse files
committed
Fix datetime extract week issue
1 parent dba58e0 commit 4aacfd4

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

sql_server/pyodbc/operations.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ def date_trunc_sql(self, lookup_type, field_name):
129129
CONVERT_YEAR = 'CONVERT(varchar, DATEPART(year, %s))' % field_name
130130
CONVERT_QUARTER = 'CONVERT(varchar, 1+((DATEPART(quarter, %s)-1)*3))' % field_name
131131
CONVERT_MONTH = 'CONVERT(varchar, DATEPART(month, %s))' % field_name
132+
CONVERT_WEEK = "DATEADD(DAY, (DATEPART(weekday, %s) + 5) %%%% 7 * -1, %s)" % (field_name, field_name)
132133

133134
if lookup_type == 'year':
134135
return "CONVERT(datetime2, %s + '/01/01')" % CONVERT_YEAR
@@ -137,8 +138,7 @@ def date_trunc_sql(self, lookup_type, field_name):
137138
if lookup_type == 'month':
138139
return "CONVERT(datetime2, %s + '/' + %s + '/01')" % (CONVERT_YEAR, CONVERT_MONTH)
139140
if lookup_type == 'week':
140-
CONVERT = "CONVERT(datetime2, CONVERT(varchar(12), %s, 112))" % field_name
141-
return "DATEADD(DAY, (DATEPART(weekday, %s) + 5) %%%% 7 * -1, %s)" % (CONVERT, field_name)
141+
return "CONVERT(datetime2, CONVERT(varchar, %s, 112))" % CONVERT_WEEK
142142
if lookup_type == 'day':
143143
return "CONVERT(datetime2, CONVERT(varchar(12), %s, 112))" % field_name
144144

0 commit comments

Comments
 (0)