

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# NVL2 関数
<a name="NVL2"></a>

指定された式の結果が NULL か NOT NULL かに基づいて、2 つの値のいずれかを返します。

## 構文
<a name="NVL2-synopsis"></a>

```
NVL2 ( expression, not_null_return_value, null_return_value )
```

## 引数
<a name="NVL2-arguments"></a>

 *expression*   
Null ステータスが評価される列名などの式。

 *not\$1null\$1return\$1value*   
*expression* が NOT NULL に評価された場合に返される値。*not\$1null\$1return\$1value* 値は、*expression* と同じデータ型を持つか、そのデータ型に暗黙的に変換可能である必要があります。

 *null\$1return\$1value*   
*expression* が NULL に評価される場合に値が返されます。*null\$1return\$1value* 値は、*expression* と同じデータ型を持つか、そのデータ型に暗黙的に変換可能である必要があります。

## 戻り型
<a name="NVL2-return-type"></a>

NVL2 の戻り値の型は、次のように決まります。
+ *not\$1null\$1return\$1value* または *null\$1return\$1value* が null の場合、not-null 式のデータ型が返されます。

*not\$1null\$1return\$1value* と *null\$1return\$1value* がどちらも null である場合
+ *not\$1null\$1return\$1value* と *null\$1return\$1value* のデータ型が同じ場合、そのデータ型が返されます。
+ *not\$1null\$1return\$1value* と *null\$1return\$1value* の数値データ型が異なる場合、互換性を持つ最小の数値データ型が返されます。
+ *not\$1null\$1return\$1value* と *null\$1return\$1value* の日時データ型が異なる場合、タイムスタンプデータ型が返されます。
+ *not\$1null\$1return\$1value* と *null\$1return\$1value* の文字データ型が異なる場合、*not\$1null\$1return\$1value* のデータ型が返されます。
+ *not\$1null\$1return\$1value* と *null\$1return\$1value* で数値データ型と数値以外のデータ型が混合している場合、*not\$1null\$1return\$1value* のデータ型が返されます。

**重要**  
*not\$1null\$1return\$1value* のデータ型が返される最後の 2 つのケースでは、*null\$1return\$1value* がそのデータ型に暗黙的にキャストされます。データ型に互換性がない場合、関数は失敗します。

## 使用に関する注意事項
<a name="nvl2-usage-notes"></a>

NVL2 では、*not\$1null\$1return\$1value* または *null\$1return\$1value* パラメータのうち、どちらか関数により選択された方の値が戻り値に含められますが、データ型については *not\$1null\$1return\$1value* のデータ型が含められます。

例えば、column1 が NULL の場合、次のクエリは同じ値を返します。ただし、DECODE の戻り値のデータ型は INTEGER となり、NVL2 の戻り値のデータ型は VARCHAR になります。

```
select decode(column1, null, 1234, '2345');
select nvl2(column1, '2345', 1234);
```

## 例
<a name="NVL2-examples"></a>

次の例では、一部のサンプルデータを変更し、2 つのフィールドを評価してユーザーに適切な連絡先情報を提供します。

```
update users set email = null where firstname = 'Aphrodite' and lastname = 'Acevedo';

select (firstname + ' ' + lastname) as name, 
nvl2(email, email, phone) AS contact_info
from users 
where state = 'WA'
and lastname  like 'A%'
order by lastname, firstname;

name			     contact_info	
--------------------+-------------------------------------------
Aphrodite Acevedo	(555) 555-0100
Caldwell Acevedo 	Nunc.sollicitudin@example.ca
Quinn Adams		   vel@example.com
Kamal Aguilar		 quis@example.com
Samson Alexander	 hendrerit.neque@example.com
Hall Alford		   ac.mattis@example.com
Lane Allen		    et.netus@example.com
Xander Allison	   ac.facilisis.facilisis@example.com
Amaya Alvarado	   dui.nec.tempus@example.com
Vera Alvarez		  at.arcu.Vestibulum@example.com
Yetta Anthony		 enim.sit@example.com
Violet Arnold		 ad.litora@example.comm
August Ashley		 consectetuer.euismod@example.com
Karyn Austin		  ipsum.primis.in@example.com
Lucas Ayers		   at@example.com
```