Snowflake Vs MS-SQL - Part 41 - NVL2
NVL2
A table contains 3 columns, if the value in the first column is NOT NULL then you have to return/display the value from the second column and if the value in the first column is NULL then you have to return/display the value from the third column.
Suppose you have data as below:
--------+--------+--------+
Col1 | Col2 | Col3 |
--------+--------+--------+
0 | 5 | 3 |
0 | 5 | [NULL] |
0 | [NULL] | 3 |
0 | [NULL] | [NULL] |
[NULL] | 5 | 3 |
[NULL] | 5 | [NULL] |
[NULL] | [NULL] | 3 |
[NULL] | [NULL] | [NULL] |
--------+--------+--------+
Expected Output
--------+--------+--------+---------------+
Col1 | Col2 | Col3 | RESULT |
--------+--------+--------+---------------+
0 | 5 | 3 | 5 |
0 | 5 | [NULL] | 5 |
0 | [NULL] | 3 | [NULL] |
0 | [NULL] | [NULL] | [NULL] |
[NULL] | 5 | 3 | 3 |
[NULL] | 5 | [NULL] | [NULL] |
[NULL] | [NULL] | 3 | 3 |
[NULL] | [NULL] | [NULL] | [NULL] |
--------+--------+--------+---------------+
MS-SQL Server
You can achieve this by using the CASE Statement as follows.
In Snowflake, we can use the same approach as above however Snowflake has a different conditional expression NVL2.
You can use nested as well.
Comments
Post a Comment