Convert nulls to zero with SQL
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Hello,
I am pulling data through an ODBC, and am returning nulls with the below statement. How can I edit this SQL code so that if it returns a null, it converts it into a zero? I know I can use data cleansing, but it may slow down my process and wanted to have it written in the SQL code.
Sum(P.COGSAmount) as CostOfGoodsSold,
Thank you!
Solved! Go to Solution.
- Labels:
- Developer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
probably some thing around
if x is null then 0 else y
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
i sually use syntax something like this with case staement
case when field is null then 0 else field end as 'new_field)
so if you writing
select field1, field2, case when field1 is null then 0 else field end as 'new_field_name' from xyz where etc etc
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Hi @kvrensanchez ,
You can use a COALESCE statement to set Nulls to Zero.
COALESCE(SUM(P.COGSAmount), 0) as CostOfGoodsSold,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Thank you! This was it.
