How to return default value from SQL query
Assuming the name is not nullable and that Id
is unique so can match at most one row.
SELECT
ISNULL(MAX(Name),'John Doe')
FROM
Users
WHERE
Id = @UserId
Try this
SELECT IFNULL(Name,'John Doe')
FROM Users
WHERE Id = @UserId)
Try ISNULL
or COALESCE
:
SELECT ISNULL((SELECT TOP 1 Name FROM Users WHERE Id = @UserId), 'John Doe')
The inner select will return nothing if no user exist with this id, the isnull will solve this case.