Skip to content

Commit 04c27a2

Browse files
maikhanhbuimShan0
authored andcommitted
set has_case_insensitive_like to True
fix date_trunc_sql to work with < 4-digit year set has_case_insensitive_like to True Revert "set has_case_insensitive_like to True" This reverts commit 2bc4f8e.
1 parent 4d29c60 commit 04c27a2

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

mssql/features.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class DatabaseFeatures(BaseDatabaseFeatures):
1717
can_use_chunked_reads = False
1818
for_update_after_from = True
1919
greatest_least_ignores_nulls = True
20+
has_case_insensitive_like = True
2021
has_json_object_function = False
2122
has_json_operators = False
2223
has_native_json_field = False

mssql/operations.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,10 @@ def date_interval_sql(self, timedelta):
175175
if DJANGO41:
176176
def date_trunc_sql(self, lookup_type, sql, params, tzname=None):
177177
sql, params = self._convert_sql_to_tz(sql, params, tzname)
178-
CONVERT_YEAR = 'CONVERT(varchar, DATEPART(year, %s))' % sql
178+
179+
# Python formats year with leading zeroes. This preserves that format for
180+
# compatibility with SQL Server's date since DATEPART drops the leading zeroes.
181+
CONVERT_YEAR = 'CONVERT(varchar(4), %s)' % sql
179182
CONVERT_QUARTER = 'CONVERT(varchar, 1+((DATEPART(quarter, %s)-1)*3))' % sql
180183
CONVERT_MONTH = 'CONVERT(varchar, DATEPART(month, %s))' % sql
181184
CONVERT_WEEK = "DATEADD(DAY, (DATEPART(weekday, %s) + 5) %%%% 7 * -1, %s)" % (sql, sql)
@@ -194,7 +197,10 @@ def date_trunc_sql(self, lookup_type, sql, params, tzname=None):
194197
else:
195198
def date_trunc_sql(self, lookup_type, field_name, tzname=None):
196199
field_name = self._convert_field_to_tz(field_name, tzname)
197-
CONVERT_YEAR = 'CONVERT(varchar, DATEPART(year, %s))' % field_name
200+
201+
# Python formats year with leading zeroes. This preserves that format for
202+
# compatibility with SQL Server's date since DATEPART drops the leading zeroes.
203+
CONVERT_YEAR = 'CONVERT(varchar(4), %s)' % field_name
198204
CONVERT_QUARTER = 'CONVERT(varchar, 1+((DATEPART(quarter, %s)-1)*3))' % field_name
199205
CONVERT_MONTH = 'CONVERT(varchar, DATEPART(month, %s))' % field_name
200206
CONVERT_WEEK = "DATEADD(DAY, (DATEPART(weekday, %s) + 5) %%%% 7 * -1, %s)" % (field_name, field_name)

0 commit comments

Comments
 (0)