文本,ntext和图像数据>types不能进行比较或sorting,除非使用IS NULL或LIKE>运算符

电子邮件属于表预订,并在我们的Microsoft SQL服务器中定义为types“文本”

SELECT email, COUNT(email) AS NumOccurrences FROM Booking GROUP BY email HAVING ( COUNT(email) > 1 ) 

运行上面的查询(试图find重复的电子邮件在预订)后,我得到了这样的错误信息:

文本,ntext和图像数据types不能进行比较或sorting,除非使用IS NULL或LIKE运算符。

我正在使用Microsoft Sql

因为您使用SQL Server ,为什么不将数据types更改为VARCHAR(100)

若要在不更改数据types的情况下解决此错误,当在SELECT语句的ORDER BY子句或GROUP BY子句中使用TEXT or NTEXT列时,需要将其转换为VARCHAR or NVARCHAR 。 例如, 这有点凌乱

 SELECT CAST(email AS NVARCHAR(100)) email, COUNT(CAST(email AS NVARCHAR(100))) AS NumOccurrences FROM Booking GROUP BY CAST(email AS NVARCHAR(100)) HAVING COUNT(CAST(email AS NVARCHAR(100))) > 1 
  • SQL Server错误消息 – 消息306