Groups | Blog | Home
all groups > sql server mseq > february 2004 >

sql server mseq : Need 0s instead of NULLs


Kole
2/11/2004 7:33:32 PM
Is there a way to return "0" instead of "NULL" in a
query? several columns that are from other views and are
linked to a table where it returns all records from my
main table. I need the NULLs to be 0s.

Rohtash Kapoor
2/11/2004 9:07:21 PM
In addition to what Ray wrote, you can use ISNULL or COALESCE.

Here is the example:

--Copy the following and Run in Query Analyzer.

SET NOCOUNT ON

IF OBJECT_ID('JustTest') IS NOT NULL
DROP TABLE JustTest

CREATE TABLE JustTest
(
CustomerID INT,
Name VARCHAR(20),
Amount INT
)

INSERT INTO JustTest VALUES (101, 'First', 50)
INSERT INTO JustTest VALUES (102, 'Second', 50)
INSERT INTO JustTest VALUES (103, 'Third', NULL)
INSERT INTO JustTest VALUES (104, NULL, 50)

SELECT * FROM JustTest

SELECT CustomerID, ISNULL(Name, ' '), ISNULL(Amount,0)
FROM JustTest

SELECT CustomerID, COALESCE(Name, ' '), COALESCE(Amount,0)
FROM JustTest


[quoted text, click to view]



Ray Higdon
2/11/2004 11:38:52 PM
Use case

select case columnname when null then '0' else columnname end

--
Ray Higdon MCSE, MCDBA, CCNA
---
[quoted text, click to view]

AddThis Social Bookmark Button