SQL SERVER – Fix: Error : 402 The data types ntext and varchar are incompatible in the equal to operator

Some errors are very simple to understand but the solution of the same is not easy to figure out. Here is one of the similar errors where it clearly suggests where the problem is but does not tell what is the solution. Additionally, there are multiple solutions so developers often get confused with which one is correct and which one is not correct.

Let us first recreate scenario and understand where the problem is. Let us run following

  1. CREATE TABLE TestTable (ID INT, MyText NTEXT)
  2. GO
  3. SELECT ID, MyText
  4. FROM TestTable
  5. WHERE MyText = 'AnyText'
  6. GO
  7. DROP TABLE TestTable
  8. GO

When you run above script it will give you following error.

  1. Msg 402, Level 16, State 1, Line 1
  2. The data types ntext and varchar are incompatible in the equal to operator.

One of the questions I often receive is that voucher is for sure compatible to equal to operator, then why does this error show up. Well, the answer is much simpler I think we have not understood the error message properly. Please see the image below. The next and varchar are not compatible when compared with each other using equal sign.

Now let us change the data type on the right side of the string to nvarchar from varchar. To do that we will put N’ before the string.

  1. CREATE TABLE TestTable (ID INT, MyText NTEXT)
  2. GO
  3. SELECT ID, MyText
  4. FROM TestTable
  5. WHERE MyText = N'AnyText'
  6. GO
  7. DROP TABLE TestTable
  8. GO

When you run above script it will give following error.

  1. Msg 402, Level 16, State 1, Line 1
  2. The data types ntext and nvarchar are incompatible in the equal to operator.

You can see that error message also suggests that now we are comparing next to nvarchar. Now as we have understood the error properly, let us see various solutions to the above problem.

Solution 1: Convert the data types to match with each other using CONVERT function.

Change the datatype of the MyText to nvarchar.

  1. SELECT ID, MyText
  2. FROM TestTable
  3. WHERE CONVERT(NVARCHAR(MAX), MyText) = N'AnyText'
  4. GO

Solution 2: Convert the data type of columns from NTEXT to NVARCHAR(MAX) (TEXT to VARCHAR(MAX))

  1. ALTER TABLE TestTable
  2. ALTER COLUMN MyText NVARCHAR(MAX)
  3. GO

Now you can run the original query again and it will work fine.

Solution 3: Using LIKE command instead of Equal to command.

  1. SELECT ID, MyText
  2. FROM TestTable
  3. WHERE MyText LIKE 'AnyText'
  4. GO

Well, any of the three of the solutions will work. Here is my suggestion if you can change the column data type from ntext or text to nvarchar or varchar, you should follow that path as text and ntext datatypes are marked as deprecated(弃用了). All developers any way to change the deprecated data types in future, it will be a good idea to change them right early.

If due to any reason you can not convert the original column use Solution 1 for temporary fix. Solution 3 is the not the best solution and use it as a last option. Did I miss any other method? If yes, please let me know and I will add the solution to original blog post with due credit.

微软官方ntext, text, image类型弃用声明

IMPORTANT! ntext, text, and image data types will be removed in a future version of 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.

原文链接

SQL Server的NTEXT类型不支持等号"="操作(转载)的更多相关文章

  1. .SQL Server中 image类型数据的比较

    原文:.SQL Server中 image类型数据的比较 在SQL Server中如果你对text.ntext或者image数据类型的数据进行比较.将会提示:不能比较或排序 text.ntext 和 ...

  2. SQL Server数据库字段类型说明

    SQL Server数据库字段类型说明 目前Sql Server 数据库一共有X个字段类型,大体分为9类,分别是字符串类型.二进制码字符串数据类型.Unincode字符串数据.整数类型.精确数据类型. ...

  3. SQL Server存储ntext截断问题

    SQL Server存储ntext截断问题   最近遇到一个问题:将大文本存储到数据库的时候,查询出来的文本却被截断了. 最后百度发现,作者提出 sql server management studi ...

  4. SQL Server返回DATETIME类型,年、月、日、时、分、秒、毫秒

    SQL Server返回DATETIME类型的年.月.日,有两种方法,如下所示: DECLARE @now DATETIME=GETDATE() --第一种方法 SELECT @now,YEAR(@n ...

  5. SQL Server 磁盘空间告急(磁盘扩容)转载

    一.背景 在线上系统中,如果我们发现存放数据库文件的磁盘空间不够,我们应该怎么办呢?新买一个硬盘挂载上去可以嘛?(linux下可以直接挂载硬盘进行扩容),但是我们的SQL Server是运行在Wind ...

  6. 如何对SQL Server 2005进行设置以允许远程连接(转载)

    如何对SQL Server 2005进行设置以允许远程连接(转载) 在尝试从远程计算机连接到 Microsoft SQL Server 2005 实例时,可能会接收到错误消息.在使用任何程序连接到 S ...

  7. SQL Server Log文件对磁盘的写操作大小是多少

    原文:SQL Server Log文件对磁盘的写操作大小是多少 SQL Server 数据库有三种文件类型,分别是数据文件.次要数据文件和日志文件,其中日志文件包含着用于恢复数据库的所有日志信息,SQ ...

  8. XEvent – SQL Server Log文件对磁盘的写操作大小是多少

    原文:XEvent – SQL Server Log文件对磁盘的写操作大小是多少 本篇是上一篇SQL Server Log文件对磁盘的写操作大小是多少的续,使用XEvent收集SQL Server D ...

  9. SQL Server 表的管理_关于事务操作的详解(案例代码)

    SQL Server 表的管理_关于事务操作的详解(案例代码) 1.概念 事务(transaction): 是将多个修改语句组合在一起的方法,这个方法中的所有语句只有全部执行才能正确完成功能.即要么全 ...

随机推荐

  1. Flask 中内置的 Session 应用

    目录 1.Flask 中 session 是需要 secret_key 的 2.session抛异常 3.session 用法: 4.验证session Flask中的Session非常的奇怪,他会将 ...

  2. 201671010456-张琼 实验十四 团队项目评审&课程学习总结

    博文简要信息表 项目 内容 这个作业属于哪个课程 http://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/nwnu- ...

  3. Spring——JDBC——数据库

    1.Spring 的数据访问哲学 数据访问的功能放到一个或者多个专注于此项任务的组件.这样的组件通常称为数据访问对象(data access object)DAO或者Repository. 为了避免应 ...

  4. 简述 gevent模块的作用和应用场景。

    当一个greenlet遇到IO操作时,比如访问网络,就自动切换到其他的greenlet,等到IO操作完成, 再在适当的时候切换回来继续执行.由于IO操作非常耗时,经常使程序处于等待状态, 有了geve ...

  5. 树莓派项目(1-3 )目标识别 NNPACK支持版Darknet,可用于树莓派等嵌入设备

    https://github.com/digitalbrain79/darknet-nnpack https://github.com/AlexeyAB/darknet#how-to-train-to ...

  6. axios post方式请求x-ww格式的数据

    //使用axios时,要确定是json格式还是x-www格式的,axios默认是json格式的,如果是x-ww格式需要做如下配置: let url = "/hehe/site/getcomm ...

  7. ES6解构赋值常见用法

    解构赋值出现的契机: let obj = { a: 1, b: 2 } // 取值 let a = obj.a let b = obj.b 问题核心: 每次取值既要确定对象属性名,还得重新定义一个变量 ...

  8. 【BigData】Java基础_HashMap

    HashMap简介 HashMap是一种非常常见.方便和有用的集合,是一种键值对(K-V)形式的存储结构 常见的方法如下: put:将数据添加到集合中 get:获取集合中的元素 size:获取集合的长 ...

  9. 数据结构与算法系列——排序(4)_Shell希尔排序

    1. 工作原理(定义) 希尔排序,也称递减增量排序算法,是插入排序的一种更高效的改进版本.但希尔排序是非稳定排序算法. 希尔排序的基本思想是:先将整个待排序的记录序列分割成为若干子序列分别进行直接插入 ...

  10. 安装kafka + zookeeper集群

    系统:centos 7.4 要求:jdk :1.8.x kafka_2.11-1.1.0 1.绑定/etc/hosts 10.10.10.xxx      online-ops-xxx-0110.10 ...