LoginSignup
4
4

More than 5 years have passed since last update.

[.NET][JavaScript][SQL] null と空文字の等価/不等価判定を言語別に並べて比較してみた

Last updated at Posted at 2019-02-06

言語によって null や空文字の扱いは異なることがあります。
以下に例を示します。
(赤字は注意が必要な箇所です)

言語ごとの判定結果(C#/VB.NET/JavaScript/SQL)

C# VB.NET JavaScript(厳密/非厳密) SQL(ANSI/ISO 準拠)
Equals Equals(null, null)
true
Equals(Nothing, Nothing)
True
Is (Nothing Is Nothing)
True
Object.is(null, null)
true
(NULL IS NULL)
TRUE
Is Not (Nothing IsNot Nothing)
False
(NULL IS NOT NULL)
FALSE
null との等号比較 (null == null)
true
(Nothing = Nothing)
True
(null === null)
true
(null == null)
true
(NULL = NULL)
UNKNOWN
NOT (NULL = NULL)
UNKNOWN
※NOT しても UNKNOWN(以下同様)
null との不等号比較 (null != null)
false
(Nothing <> Nothing)
False
(null !== null)
false
(null != null)
false
(NULL <> NULL)
UNKNOWN
空文字との等号比較 (null == "")
false
(Nothing = "")
True
(null === "")
false
(null == "")
false
(NULL = '')
UNKNOWN
空文字との不等号比較 (null != "")
true
(Nothing <> "")
False
(null !== "")
true
(null != "")
true
(NULL <> '')
UNKNOWN
有効文字との等号比較 (null == "a")
false
(nullString = "a")
False
(null === "a")
false
(null == "a")
false
(NULL = 'a')
UNKNOWN
有効文字との不等号比較 (null != "a")
true
(Nothing <> "a")
True
(null !== "a")
true
null != "a")
true
(NULL <> 'a')
UNKNOWN
未定義値との等号比較 (null === undefined)
false
(null == undefined)
true
未定義値との不等号比較 (null !== undefined)
true
(null != undefined)
false

ポイントは3つです。

  • VB.NET では、String 型の等価/不等価演算子は Nothing と空文字を等しいと判断します。
  • JavaScript では、非厳密演算子は null と undefined を等しいと判断します。
  • ANSI/ISO 準拠のSQLでは、比較演算子による NULL との比較結果は UNKNOWN となります。(SQL Server で ANSI_NULLS オプションをOFFにすると違う結果になりますが、今後のバージョンでサポートされなくなることがアナウンスされています)

LINQ to Entities における null の扱い

LINQ to Entities の値比較における null の扱いには別途注意が必要です。
Entity Framework 6 で UseDatabaseNullSemantics プロパティ が導入され、既定(false)で (operand1 == operand2) が次のような複雑なSQL式に変換されるようになりました。
※operand1, operand2 がどちらも NULL となりうるケース、たとえば operand1 が NULL 許可列、operand2 が String 型変数の場合を想定します。

(((operand1 = operand2) AND (NOT (operand1 IS NULL OR operand2 IS NULL))) OR ((operand1 IS NULL) AND (operand2 IS NULL)))

どちらかが NULL の場合、ANSI 準拠の等価演算子 '=' では常に UNKNOWN が返されてしまうため、NULL を絡めた比較も正しく行われるような考慮がされています。
設定を true に変えれば (operand1 = operand2) という素直な式が生成されますが、NULL を正しく比較できないことを認識したうえで設定しないと「不可解な」挙動に頭を抱えることになりかねませんので注意しましょう。

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4