简体   繁体   中英

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.

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM