开发者

SQL INNER JOIN on Text Columns

I have two tables (equipment & software) that I want to do an INNER JOIN on. They both have a field called EQCN. It is a text field. I get the following error:

The data types text and text are incompatible in the equal to operator.

There has to be a way around this开发者_如何学C.


Change the data types for these columns to varchar(max).

From Microsoft:

ntext, text, and image data types will be removed in a future version of Microsoft SQL Server. Avoid using these data types in new development work, and plan to modify applications that currently use them. Use nvarchar(max), varchar(max), and varbinary(max) instead.


Although this is odd, Microsoft suggests doing an indirect comparison on a text or ntext using something like SUBSTRING. Example:

SELECT * 
FROM t1 
JOIN t2 ON SUBSTRING(t1.textcolumn, 1, 20) = SUBSTRING(t2.textcolumn, 1, 20)

This of course raises a whole other set of issues like, what if the first # of characters are identical, etc. I would suggest going the route of changing the type if you can first, rather than take this advice.

Source


Doing a join on a TEXT field would be VERY slow, even if it did work. Perhaps use:

CONVERT(varchar, myColumnName) = 'my value'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜