Amr Elgarhy
Amr Elgarhy

Reputation: 68912

How to return a field as Nullable from a View

I have a SQL Server View, and It has a field like this ISNULL(co.[MyValue],0) AS MyValue in the select.

MyValue in its table is a Nullable field, now I want to return 0 if the value is null but keep the field Nullable.

I want this because I don't want to change the Entity Framework EDMX file which has this view field as Nullable field.

Is there a way to do that in SQL?

Upvotes: 1

Views: 72

Answers (3)

Chiragkumar Thakar
Chiragkumar Thakar

Reputation: 3716

Try this way, that I just used CASE WHEN here, and it is as simple as you can see, I don't think any other explanation will be needed here.

SELECT CASE 
           WHEN Table_Name .[MyValue] IS NULL THEN 0 
           ELSE Table_Name .[MyValue] 
       END
FROM Table_Name 

Upvotes: 0

Matt
Matt

Reputation: 15071

Use a CASE statement

SELECT CASE 
           WHEN [MyValue] IS NULL THEN 0 
           ELSE [MyValue] 
       END
FROM yourtable

Upvotes: 1

Ajay
Ajay

Reputation: 6590

I think you should try COALESCE

COALESCE(co.[MyValue],0)

Upvotes: 4

Related Questions