In a table, I have a column called MEMO_TEXT that is a text data type. When I try creating a view and use a GROUP BY, I get the following error:
SQL S
Try these...
SELECT DistinctMemo = DISTINCT(CAST(MEMO_TEXT AS varchar(max)))
FROM MyTable
-- or
SELECT DistinctMemo = CAST(MEMO_TEXT AS varchar(max))
FROM MyTable
GROUP BY CAST(MEMO_TEXT AS varchar(max))
One hack around it is to cast it as an nvarchar(max)
.
This is a documented way to increase the string length beyond 4,000:
nvarchar [ ( n | max ) ]
Variable-length Unicode string data. n defines the string length and can be a value from 1 through 4,000. max indicates that the maximum storage size is 2^31-1 bytes (2 GB). The storage size, in bytes, is two times the actual length of data entered + 2 bytes. The ISO synonyms for nvarchar are national char varying and national character varying.
A similar trick applies to varchar().
Do you know that there will never be repeated data in the ntext field? You could do the distinct in a derived table on the other fields and then join to the table with the ntext field and grab it in the outer query.
something like (assume field3 is the ntext field)
select mt.idfield, a.field1, a.field2, mt.field3
from mytable mt
join
(select disitinct mt1.idfield, mt1.field1, mot.field2 from mytable mt1
join myothertable mot on mt1.idfield = mot.idfield) a
on a.ifield = mt.idfield
Can you just change the data type of the column to nvarchar(max)?
Consider putting in another column that takes the first 40 characters or so of your memo field, and group on that. Grouping on a memo field is going to be really slow if you have a lot of text in there.
UPDATE myTable SET myNewField = LEFT(myOldField, 40);