"The text, ntext, and image data types cannot be compared or sorted, …"
See original GitHub issueWhen reading separate columns using the following DB structure and code, I get error when the column is of the type TEXT
.
The table schema type for that is:
TABLE_NAME COLUMN_NAME DATA_TYPE CHARACTER_MAXIMUM_LENGTH
---------- ----------- --------- ------------------------
mytable col_name text 2147483647
I’m then trying to reading all the columns (one at a time) using:
def get_data(table_list):
cursor = connect()
for table, column in table_list:
SQLA = 'SELECT TOP 1 [{}] as data FROM [{}] ORDER BY [{}] DESC;'.format(column, table, column)
cursor.execute(SQLA)
while 1:
row = cursor.fetchone()
if not row:
break
print(' Data : {}'.format(row.data))
db_close(cursor)
This works fine until it hits the first column with a TEXT type (as shown above).
Running the same query from powershell CMD yields the same result:
$ iex "$qcmd 'SELECT TOP 1 [notes] FROM [mytable ] ORDER BY [notes] DESC;' -W"
Msg 306, Level 16, State 2, Server DBSQL01, Line 1
The text, ntext, and image data types cannot be compared or sorted, except when using IS NULL or LIKE operator.
Environment
[INFO] Getting MS SQL Server Version info...
------------------------------------------------------------
Microsoft SQL Server 2008 R2 (SP3-GDR) (KB4057113) - 10.50.6560.0 (X64)
Dec 28 2017 15:03:48
Copyright (c) Microsoft Corporation
Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: ) (Hypervisor)
pyodbc 4.0.32
- Python:
3.10.3
Issue
Observed behavior:
I get the following error trying to read MS SQL columns of the type TEXT
.
pyodbc.ProgrammingError: ('42000', '[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]
The text, ntext, and image data types cannot be compared or sorted, except when using IS NULL or LIKE operator. (306) (SQLExecDirectW)')
Expected behavior:
That it works without errors.
Issue Analytics
- State:
- Created a year ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
The text, ntext, and image data > types cannot be ...
The text, ntext, and image data types cannot be compared or sorted, except when using IS NULL or LIKE operator. I am using...
Read more >The text, ntext, and image data types cannot be ... - MSDN
Yes, this is normal behavior. These types, by design, are for storing large objects of character and binary data and cannot be used...
Read more >SQL Server Error Messages - Msg 306
SQL Server Error Messages - Msg 306 - The text, ntext, and image data types cannot be compared or sorted, except when using...
Read more >How do I get by with "The text, ntext, and image data types ...
"The text, ntext, and image data types cannot be compared or sorted, except when using IS NULL or LIKE operator." Thanks,. Haimanti. Lynn...
Read more >MSSQL How to fix error The text, ntext, and image data types ...
Fi the following errorMSSQL - How to fix error - The text, ntext, and image data types cannot be compared or sorted, except...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Yes, ntext is UTF-16 and image is plain binary. You can cast them to nvarchar(max) and varbinary(max) respectively.
You can try
cast(col_name as varchar(max))
The text type is one of the early large object (LOB) types, it is stored and processed differently by the server than the regular types.