简体   繁体   English

SQL Server 2008中的ISNULL

[英]ISNULL in SQL Server 2008

Today I have encountered an odd issue with ISNULL function in SQL 今天我在SQL中遇到了一个奇怪的ISNULL函数问题

I have table contains the customer information I have to display a full name concatenating First, Middle Initial and Last Name, In the table I know Middle Initial is a nullable column. 我有表包含客户信息我必须显示连接的全名First,Middle Initial和Last Name,在表中我知道Middle Initial是一个可以为空的列。

So I used ISNULL function in SQL to return the Middle Initial if it is not null, which works fine but I used the ISNULL function with a expression as below which I ideally should not work in case if the MI is null, but interestingly it worked. 所以我在SQL中使用了ISNULL函数来返回Middle Initial,如果它不是null,它工作正常,但是我使用了ISNULL函数,下面有一个表达式,如果MI为null,我理想情况下不应该工作,但有趣的是它工作了。

SELECT FirstName + ' ' + LastName + ' '+ ISNULL(MI + '.', '') As MiddleInitial 
FROM CS_tblMaster WHERE CustomerNo = 2627240

Above SOL Query should return me MiddleInitial as "." 以上SOL Query应该将MiddleInitial作为“。”返回给我。 when MiddleInitial is null, but it did return as empty string. 当MiddleInitial为null时,它确实返回为空字符串。

So I wrote another query as below. 所以我写了另一个查询如下。

SELECT (MI + '.') AS MiddleInitial FROM CS_tblMaster WHERE CustomerNo = 2627240

which again given as NULL 再次给出为NULL

Somehow when you concatenate a string with NULL value it returns null. 不知何故,当您将字符串与NULL值连接时,它将返回null。 I would like to understand of this implementation. 我想了解这个实现。

Can someone help 有人可以帮忙吗

如果您想在MI为空时设置一个句点,请使用:(您只是在错误的位置使用'.'

SELECT FirstName + ' ' + LastName + ' '+ ISNULL(MI, '.') As MiddleInitial FROM CS_tblMaster WHERE CustomerNo = 2627240

– Original statement - 原始声明

SELECT FirstName + ' ' + LastName + ' '+ ISNULL(MI + '.', '') As MiddleInitial 
FROM CS_tblMaster WHERE CustomerNo = 2627240

– My original answer (edited) - 我的原始答案(已编辑)

SELECT FirstName + ' ' + LastName + ' '+ (ISNULL(MI, '') + '.') As MiddleInitial 
FROM CS_tblMaster WHERE CustomerNo = 2627240

In this case, SQL would first check to see if MI is Null and uses MI if Not Null or uses an empty string if it is. 在这种情况下,SQL首先检查MI是否为Null,如果不为Null则使用MI,如果是,则使用空字符串。 Then it concatenates that result, which is now never Null, with the period. 然后它将该结果与句点连接起来,现在这个结果现在永远不会为空。

– Final answer - 最后的答案

SELECT 
    FirstName + ' ' + LastName + ' '
        + CASE WHEN MI IS NULL THEN '' ELSE MI + '.' END As MiddleInitial 
FROM CS_tblMaster WHERE CustomerNo = 2627240

@Satish, not sure if you feel you have answer yet since you haven't selected one, and I apologize if my answer was short and fast. @Satish,不确定你是否觉得你有答案,因为你还没有选择,我很抱歉,如果我的答案简短而快速。 Seeing all the responses made me realize I hadn't thought much about your question when I first saw it. 看到所有回复让我意识到当我第一次看到它时,我对你的问题没有多想。

To answer “I would like to understand of this implementation”, Nulls are a completely special value in SQL. 要回答“我想了解这个实现”,Nulls在SQL中是一个完全特殊的值。 Not an empty string, not spaces, not zeros. 不是空字符串,不是空格,不是零。 They mean in a more literal sense “nothing”. 从字面意义上讲,它们意味着“没有”。 You can check for them, can see if something is null. 您可以检查它们,可以看到,如果事情零。 But you can't “do” things with Nulls. 但你不能用Null“做”事情。 So 57 + Null = Null. 所以57 + Null = Null。 'Mary' + Null = Null. 'Mary'+ Null = Null。 ((12 *37) +568) / Null = Null. ((12 * 37)+568)/ Null = Null。 The Max() of 'Albert', 'Mary', Null, and 'Zeke' is Null. 'Albert','Mary',Null和'Zeke'的Max()是空的。 This article http://en.wikipedia.org/wiki/Null_(SQL) may help, with a decent description in the section on Null Propagation. 这篇文章http://en.wikipedia.org/wiki/Null_(SQL)可能有所帮助,在Null Propagation一节中有一个不错的描述。

The Isnull function is not so much a test for Null, but a way to handle it. Isnull函数不是Null的测试,而是一种处理它的方法。 So to test if something is Null you would use ColumnName Is Null or ColumnName Is Not Null in your Select. 因此,要测试某些内容是否为空,您将在Select中使用ColumnName Is Null或ColumnName Is Null。 What Isnull(MI,'') says is: I want a value, if the value of MI it not null, then I want MI, otherwise I want an empty string. Isnull(MI,'')说的是:我想要一个值,如果MI的值不为null,那么我想要MI,否则我想要一个空字符串。

Going on, I'm not sure I initially understood what you were actually trying to do. 继续,我不确定我最初是否理解你实际上要做的事情。 If you were trying to get a period when the middle initial was null, then my original answer and most of the others would work for you. 如果你试图得到一个中间的首字母为空的时期,那么我原来的答案和其他大多数都适合你。 But I think you may be trying to say: If I have a middle initial, then I want the middle initial followed by a period. 但我想你可能会试着说:如果我有一个中间的首字母,那么我想要中间的首字母,然后是一段时间。 If I don't have middle initial then I want nothing: 'Alberto C. Santaballa' or 'Alberto Santaballa', never 'Alberto . 如果我没有中间首字母那么我什么都不想:'Alberto C. Santaballa'或'Alberto Santaballa',从来没有'Alberto。 Santaballa”. Santaballa”。 If that is the case then use the final statement in the edited answer. 如果是这种情况,则在编辑的答案中使用最终语句。

@Zec, thanks for the edit. @Zec,谢谢你的编辑。 The typo was another product of too-fast typing! 错字是太快打字的另一个产物! :-/ : - /

If you concatenate to a null you get a null. 如果连接到null,则得到null。 Take the period outside. 走出户外时期。 Isnull(MI, '') + '.' Isnull(MI,'')+'。'

Will something like this work for you... 这样的事情会对你有用吗......

    SELECT FirstName + ' ' + LastName + ' '+ CASE WHEN MI is null then '.' else MI END As MiddleInitial 
        FROM CS_tblMaster WHERE CustomerNo = 2627240
END

brothers, i guess u are misunderstanding isnull function 兄弟,我猜你是误解isnull功能

ISNULL([Columns To Check],[Replaced String]) 

SELECT FirstName + ' ' + LastName + ' '+ ISNULL(MI + '.', '') As MiddleInitial 
FROM CS_tblMaster WHERE CustomerNo = 2627240

ISNULL function refer to is the column you want to check is null, then replace it with some others string ISNULL函数引用是要检查的列为null,然后将其替换为其他字符串

in your query, u are trying to replace ur MI+'.' 在您的查询中,您正在尝试替换您的MI +'。' columns to Empty string, hopefully my explanation can help u up=) 列到空字符串,希望我的解释可以帮助你=)

What about NULLIF: 那么NULLIF:

SELECT FirstName + ' ' + LastName + ' '+ ISNULL((NULLIF(MI + '.', '.'), '') As MiddleInitial FROM CS_tblMaster WHERE CustomerNo = 2627240 SELECT FirstName +''+ LastName +''+ ISNULL((NULLIF(MI +'。','。'),'')作为MiddleInitial FROM CS_tblMaster WHERE CustomerNo = 2627240

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

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