Langsung ke konten utama

Comparison of NULLIF and ISNULL Sql Server

NULLIF

Returns a null value if the two specified expressions are equal.
Syntax :
NULLIF ( expression , expression )
Arguments :
expression :Is any valid scalar expression.
ISNULL

Replaces NULL with the specified replacement value.
Syntax :
ISNULL ( check_expression , replacement_value )
Arguments :
check_expression : Is the expression to be checked for NULL. check_expression can be of any type.

replacement_value : Is the expression to be returned if check_expression is NULL. replacement_value must be of a type that is implicitly convertible to the type of check_expresssion.
Comparison


[sql]
DECLARE
@budgets TABLE
(
current_year DECIMAL NULL
,previous_year DECIMAL NULL
)
INSERT INTO @budgets
VALUES
(NULL
,40
)
INSERT INTO @budgets
VALUES
(50
,50
)
INSERT INTO @budgets
VALUES
(50
,60
)
INSERT INTO @budgets
VALUES
(50
,70
)

SELECT
NULLIF(current_year,previous_year) AS 'Average Budget'
,isnull(current_year,0)
FROM @budgets
[/sql]

Exemple 2
[sql]
SELECT
NULLIF(doc_totalitem,0)
,ISNULL(doc_discvalue * (trans_total / NULLIF(doc_totalitem,0)),0)
FROM Sales.[Invoice.Header] a
INNER JOIN Sales.[Invoice.Detail] b ON a.doc_id = b.doc_id
[/sql]

Komentar

Postingan populer dari blog ini

sys.processes Status sp_who2 SQL Server

Taken from the books online reference for sys.processes  and the status column. dormant = SQL Server is resetting the session. running = The session is running one or more batches. When Multiple Active Result Sets (MARS) is enabled, a session can run multiple batches. For more information, see Using Multiple Active Result Sets (MARS). background = The session is running a background task, such as deadlock detection. rollback = The session has a transaction rollback in process. pending = The session is waiting for a worker thread to become available. runnable = The task in the session is in the runnable queue of a scheduler while waiting to get a time quantum. spinloop = The task in the session is waiting for a spinlock to become free. suspended = The session is waiting for an event, such as I/O, to complete.