简体   繁体   English

在MSSQL中使用ISNULL()的奇怪行为

[英]Strange behavior with ISNULL() in MSSQL

I have a select statement that is combining multiple segments of a persons name. 我有一个select语句,它组合了一个人名的多个段。 This isn't anything new. 这不是什么新鲜事。

SELECT FirstName + ' ' + LastName AS FullName FROM MyTable

I then tried to add the middle initial to this and I came up with the following 然后我尝试将中间的首字母添加到此,我想出了以下内容

SELECT FirstName + ' ' + ISNULL(MiddingInitial + ' ', '') + LastName AS FullName FROM MyTable

This appears to work, but during my testing of ISNULL() , I came across an odd behavior. 这似乎有效,但在我测试ISNULL() ,我遇到了一个奇怪的行为。 I'm aware that NULL + 'any string' resolves to NULL . 我知道NULL + 'any string'解析为NULL However this was just plain odd... 然而,这简直奇怪......

Here's my code, and what I get out as a result... 这是我的代码,以及我得到的结果......

print '''' + isnull(null + 'Any String','Results in null') + ''''
print '''' + isnull(null + 'Any','Results in null') + ''''
print '''' + isnull(null + 'A','Results in null') + ''''
print '''' + isnull(null + '','Results in null') + ''''
/*
'Results in '
'Resu'
'Re'
'Re'
*/

Any idea of why this behavior occurs? 知道为什么会出现这种行为吗? Does it do the same for you? 它对你有同感吗?

It comes down to the datatypes you're working with and the behavior of the ISNULL function. 它归结为您正在使用的数据类型以及ISNULL函数的行为。 Let's look at one example: 我们来看一个例子:

null + 'Any String'

The above fits perfectly into a varchar(11) datatype. 以上完全适合varchar(11)数据类型。 The NULL (which is really just the result of char(0) and has length 1) and a regular 10-character string concatenated together makes for 11 characters total. NULL (实际上只是char(0)的结果,长度为1)和连接在一起的常规10个字符的字符串共计11个字符。 The replacement string -- the second parameter to your ISNULL function -- is going to be forced to fit into a varchar(11) , so it is truncated to 11 characters. 替换字符串 - ISNULL函数的第二个参数 - 将被强制插入varchar(11) ,因此它被截断为11个字符。

The pattern repeats for the remaining items, with a special case for the empty string. 其余项目的模式重复,空字符串的特殊情况。

If you don't want this to happen, use COALESCE , which instead of taking the datatype of the first item in the list, it uses data type precedence. 如果您不希望发生这种情况,请使用COALESCE ,它不使用列表中第一项的数据类型,而是使用数据类型优先级。 A varchar(15) takes precedence over a varchar(11) , so you will get the full replacement string: varchar(15)优先于varchar(11) ,因此您将获得完整的替换字符串:

print '''' + coalesce(null + 'Any String','Results in null') + ''''
print '''' + coalesce(null + 'Any','Results in null') + ''''
print '''' + coalesce(null + 'A','Results in null') + ''''
print '''' + coalesce(null + '','Results in null') + ''''
/*
'Results in null'
'Results in null'
'Results in null'
'Results in null'
*/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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