WilliamRen / django-pyodbc

Automatically exported from code.google.com/p/django-pyodbc
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

Patch for field_cast_sql() #121

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
According to 
http://msdn.microsoft.com/en-us/library/aa276823%28v=sql.80%29.aspx the default 
length of nvarchar inside a CAST function is 30 characters. However, the field 
value that is to be casted to nvarchar could be longer than 30 characters. In 
that case an error is raised that looks like this: 
"Arithmetic overflow error converting expression to data type nvarchar." 

To overcome this error we can give an explicit length for nvarchar that is 
equal to the maximum length (4000 characters according to the same source as 
above).

The patch follows:

--- operations.py   2012-02-13 18:36:43.000000000 +0200
+++ operations_patched.py   2012-02-13 19:55:07.000000000 +0200
@@ -60,7 +60,7 @@
         searched against.
         """
         if self.sql_server_ver < 2005 and db_type and db_type.lower() == 'ntext':
-            return 'CAST(%s as nvarchar)'
+            return 'CAST(%s as nvarchar(4000))'
         return '%s'

     def fulltext_search_sql(self, field_name):

Original issue reported on code.google.com by petros.m...@gmail.com on 13 Feb 2012 at 8:42