Monday, November 21, 2005

COALESCE AND ISNULL

Using coalesce requires casting the data Type while isnull results in the same data type as underlying table

Using coalesce when there are null values in the column require you to cast the data type.

Create table sourceTable(TableId tinyint)

Insert into sourceTable(TableId)
SELECT 1 AS TableId
UNION
SELECT NULL


select COALESCE(tableId, 0)
into table1
from SourceTable


select ISNULL(tableId, 0)
into table2
from SourceTable


Table1 is created with Int data Type while Table2 is created with tinyint data type. I don’t know if this is by design.

No comments: