Instead of NULL how do I show `0` in result with SELECT statement sql?

前端 未结 4 1631
深忆病人
深忆病人 2020-12-05 07:47

I have one stored procedure which is giving me an output (I stored it in a #temp table) and that output I\'m passing to another scalar function.

相关标签:
4条回答
  • 2020-12-05 08:02

    Try these three alternatives:

    1. ISNULL(MyColumn, 0)
    
    2. SELECT CASE WHEN MyColumn IS NULL THEN 0 ELSE MyColumn END FROM MyTable
    
    3. SELECT COALESCE(MyCoumn, 0) FROM MyTable
    

    There is another way but it is not supported by most of the databases SELECT MyColumn + 0 This might work, but NULL + anything is still NULL in T-SQL.

    0 讨论(0)
  • 2020-12-05 08:03

    You could use this:

    SELECT Ename , Eid , ISNULL(Eprice, 0) as Eprice, Ecountry from Etable
    Where Ecountry = 'India'
    
    0 讨论(0)
  • 2020-12-05 08:04

    Try ISNULL(Eprice, 0) instead of Eprice

    0 讨论(0)
  • 2020-12-05 08:05

    Use coalesce():

    select  coalesce(Eprice, 0) as Eprice
    

    In SQL Server only, you can save two characters with isnull():

    select  isnull(Eprice, 0) as Eprice
    
    0 讨论(0)
提交回复
热议问题