简体   繁体   English

SQL Server:uniqueidentifier上的ISNULL

[英]SQL Server: ISNULL on uniqueidentifier

I am trying to compare a column col1 and a variable @myvar in a WHERE clause. 我试图在WHERE子句中比较列col1和变量@myvar Both usually contain GUIDs, but may also have NULL values. 两者通常都包含GUID,但也可能具有NULL值。 I thought I could get around the fact that NULL=NULL evaluates to FALSE by using WHERE ISNULL(col1, '')=ISNULL(@myvar, '') . 我以为我可以通过使用WHERE ISNULL(col1, '')=ISNULL(@myvar, '')来解决NULL=NULL计算为FALSE的事实。 That would compare two empty strings instead, and evaluate to TRUE. 这将比较两个空字符串,并评估为TRUE。

This will, however, produce the following error message: 但是,这将产生以下错误消息:

Msg 8169, Level 16, State 2, Line 3 Conversion failed when converting from a character string to uniqueidentifier. 消息8169,级别16,状态2,行3转换从字符串转换为uniqueidentifier时失败。

I tried 我试过了

DECLARE @myvar uniqueidentifier = NULL
SELECT ISNULL(@myvar,'') as col1

Same error message. 相同的错误消息。

Two questions: First, I am trying to convert a uniqueidentifier variable - even though it has a NULL value - to an (empty!) string, not the other way around, as the error message suggests. 两个问题:首先,我试图将uniqueidentifier变量 - 即使它具有NULL值 - 转换为(空!)字符串,而不是相反,如错误消息所示。 What gives? 是什么赋予了?

Second, is there a better way to word that WHERE clause I need, to allow for comparing uniqueidentifiers that might be NULL? 第二,有没有更好的方法来说明我需要的WHERE子句,以允许比较可能为NULL的uniqueidentifier?

Since the first argument you are passing isnull is not a literal null , it will determine the return type of that call, a uniqueidentifier in your case. 由于您传递的第一个参数isnull不是文字null ,它将确定该调用的返回类型,在您的情况下是唯一uniqueidentifier The second argument, '' , cannot be cast to this type, hence the error you're getting. 第二个参数'' ,不能转换为这种类型,因此你得到的错误。

One way around this is just to explicitly check for null s: 解决这个问题的一种方法就是明确检查null s:

WHERE (@myvar IS NULL AND col1 IS NULL) OR (col1 = @myvar)

I think below expression can be used to check if the GUID column is empty 我认为下面的表达式可用于检查GUID列是否为空

CAST(0x0 AS UNIQUEIDENTIFIER)

some thing like 就像是

...WHERE GuidId <>  CAST(0x0 AS UNIQUEIDENTIFIER)

The reason ISNULL isn't working for you is that the replacement value (the value to be used if the check expression really is null) must be implicitly convertible to the type of the check expression. ISNULL不适合您的原因是替换值(如果检查表达式确实为null,则使用的值)必须可以隐式转换为检查表达式的类型。

Your WHERE clause can use a col IS NULL AND @var IS NULL to check that state. 您的WHERE子句可以使用col IS NULL AND @var IS NULL来检查该状态。

As others have pointed out, exclude the NULL values from the results and THEN do the comparison. 正如其他人所指出的那样,从结果中排除NULL值然后进行比较。 You can use COALESCE to exclude NULL values from comparisons. 您可以使用COALESCE从比较中排除NULL值。

请尝试以下代码:

WHERE ISNULL([Guid], NEWID()) = @myvar 

Here is another way to overcome this issue: 以下是克服此问题的另一种方法:

DECLARE @myvar uniqueidentifier = NEWID()

SELECT * FROM TABLE
Where ISNULL(col1,@myvar) = ISNULL(Col2,@myvar)

This will resolve your error. 这将解决您的错误。 Conversion failed when converting from a character string to uniqueidentifier . 从字符串转换为uniqueidentifier时转换失败。

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

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