Commits

paulocheque  committed b8e59f6

[bugfix] method date_trunc_sql

  • Participants
  • Parent commits 1977518

Comments (0)

Files changed (1)

File sql_server/pyodbc/operations.py

         truncates the given date field field_name to a DATE object with only
         the given specificity.
         """
-        if lookup_type == 'year':
-            return "Convert(datetime, Convert(varchar, DATEPART(year, %s)) + '/01/01')" % field_name
-        if lookup_type == 'month':
-            return "Convert(datetime, Convert(varchar, DATEPART(year, %s)) + '/' + Convert(varchar, DATEPART(month, %s)) + '/01')" % (field_name, field_name)
-        if lookup_type == 'day':
-            return "Convert(datetime, Convert(varchar(12), %s, 112))" % field_name
+        #http://code.google.com/p/django-pyodbc/issues/detail?id=112
+        return "dateadd(%s,datediff(%s,0,%s),0)" % (lookup_type, lookup_type, field_name)
 
     def field_cast_sql(self, db_type):
         """
             sql_list = ['ALTER TABLE %s NOCHECK CONSTRAINT %s;' % \
                     (self.quote_name(fk[0]), self.quote_name(fk[1])) for fk in fks]
             sql_list.extend(['%s %s %s;' % (style.SQL_KEYWORD('DELETE'), style.SQL_KEYWORD('FROM'),
-                             style.SQL_FIELD(self.quote_name(table)) ) for table in tables])
+                             style.SQL_FIELD(self.quote_name(table))) for table in tables])
             # Then reset the counters on each table.
             sql_list.extend(['%s %s (%s, %s, %s) %s %s;' % (
                 style.SQL_KEYWORD('DBCC'),
         elif value is not None and field and field.get_internal_type() == 'FloatField':
             value = float(value)
         return value
-        
+