SQLUSA

Microsoft SQL Server 2005 Best Practices

How to use COALESCE instead of ISNULL?

 

Execute the following script in Query Editor to create a view of all employee names:


USE AdventureWorks

IF object_id(N'Person.vEmployeeName', 'V') IS NOT NULL
DROP VIEW Person.vEmployeeName
GO

CREATE VIEW Person.vEmployeeName
AS
SELECT FirstName,
MiddleName,
LastName,
Name=LastName + ', ' + FirstName + COALESCE(' ' + MiddleName, '')
FROM Person.Contact c
JOIN HumanResources.Employee e
ON c.ContactID = e.ContactID

GO

SELECT * FROM Person.vEmployeeName
ORDER BY LastName, FirstName
GO


 

SQLUSA - The Best SQL Server 2005 Training in the World
 
 
SQLUSA.com Home Page