Menu Close

Using DISTINCT with ntext fields issue

The problem of trying to use DISTINCT when you have a ntext field is that it will not let you do it. So here is the fix:

Simply CAST the ntext column as nvarchar(MAX)

Example:

SELECT DISTINCT
CAST(ntextColumn as nvarchar(MAX)
FROM dbtable

NOTE: MAX is what saves the day.

nvarchar(MAX) = ntext (when comparing the storage)
varchar(MAX) = text (when comparing the storage)