SQL INNER JOIN on Text Columns

岁酱吖の 提交于 2021-02-16 14:25:10

问题


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.


回答1:


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.




回答2:


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




回答3:


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

CONVERT(varchar, myColumnName) = 'my value'


来源:https://stackoverflow.com/questions/7615163/sql-inner-join-on-text-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!