Exception handling · MSSQL · failed to convert nvarchar value ‘xxx’ to data type int

Time:2021-7-24
Reading time |0.16 minutes word count |260.8 characters
primary coverage |1. Introduction & background
“Exception handling · MSSQL · failure in converting nvarchar value ‘xxx’ to data type int”
Author | SCscHero Writing time | 2021/7/17 PM2:31
Article type |Series Degree of completion |Completed
motto Every great cause has a trivial beginning.

1、 Introduction & background    Completion: 100%


a) Dealing with problems

The following errors are encountered in MSSQL:
[cn version]

Message 245, level 16, status 1, line 13
Failed to convert nvarchar value ‘xxx’ to data type int.

[en version]

MSG 245, level 16, state 1, line 26
Conversion failed when converting the nvarchar value ‘XXX’ to data type int.

Exception handling · MSSQL · failed to convert nvarchar value 'xxx' to data type int

b) Response scenario

Such errors often occur when writing T-SQL.

c) Solution principle & method

The reason for the problem is that when we write T-SQL, we sometimes get used to writing numbers without double quotes. This will cause many problems.It is possible that the filtered field itself is of nvarchar type, while SSMS filters by forcing the type, so the conversion fails for some strings.In addition, this writing method is also easy to cause various performance problems (such as not walking the index, wasting conversion performance, etc.), which should be kept in mind when writing T-SQL.

Exception handling · MSSQL · failed to convert nvarchar value 'xxx' to data type int

2、 Statements and references    Completion: 100%


Original blog post, please do not reprint without permission.

If you are helpful, you are welcome to like, collect and pay attention. If you have any questions, please comment! If you need to contact the blogger, you can write scschero directly.