How to Ignore 0(zero) Values in SQL Query?
Last Updated :
28 Oct, 2021
In SQL, many times we are required to display all the rows of a table in which the entries having 0(zero) as an entry/value are ignored. This is achieved through REPLACE command in SQL. In this article, we will discuss how to ignore 0(zero) and replace them with an empty space in SQL. For this article, we will be using the Microsoft SQL Server as our database.
Step 1: Create a Database. For this use the below command to create a database named GeeksForGeeks.
Query:
CREATE DATABASE GeeksForGeeks
Output:

Step 2: Use the GeeksForGeeks database. For this use the below command.
Query:
USE GeeksForGeeks
Output:

Step 3: Create a table MARKS inside the database GeeksForGeeks. This table has 4 columns namely ID, ENG, MATHS, and PHYS containing student IDs and marks of various subjects.
Query:
CREATE TABLE MARKS(
ID INT,
ENG INT,
MATHS INT,
PHYS INT);
Output:

Step 4: Insert 5 rows into the MARKS table and enter some 0(zero) values as subject marks to demonstrate how to ignore such entries.
Query:
INSERT INTO MARKS VALUES(1,99,0,89);
INSERT INTO MARKS VALUES(2,0,65,78);
INSERT INTO MARKS VALUES(3,83,85,0);
INSERT INTO MARKS VALUES(4,0,77,0);
INSERT INTO MARKS VALUES(5,97,45,54);
Output:

Step 5: Display all the rows of the MARKS table including the 0(zero) values.
Query:
SELECT * FROM MARKS;
Output:

Step 6: Display all the rows of the MARKS table ignoring the 0(zero) values. This is achieved through REPLACE command which replaces all 0's with an empty blank.
Syntax:
REPLACE(Column_name, value_to_be_replaced, new_value)
To perform this for more than one column, place a comma and then write multiple replace commands.
Query:
SELECT ID, REPLACE(ENG,0,'') "ENG", REPLACE(MATHS,0,'') "MATHS", REPLACE(PHYS,0,'') "PHYS" FROM MARKS;
Output:
Similar Reads
How to Insert Rows with NULL Values in SQL? In SQL, due to lack of data, we sometimes need to insert rows with NULL values in the tables. Here, the keyword NULL(without quotes) is used to represent no data. There are some key points of Null value: NULL value is different from a zero value.A NULL value is used to represent a missing value, but
2 min read
SQL Query to Exclude Null Values In relational databases, managing NULL values is a critical aspect of data integrity and accuracy. A NULL value signifies the absence of data in a column, distinguishing it from a zero, which is an integer, or a blank space, which is a character. Queries involving NULL values require careful handlin
3 min read
How to Hide Zero Values in Pivot Table in Excel? One of Microsoft Excel's most important features is the pivot table. You might be aware of this if you have worked with it. It provides us with a thorough view and insight into the dataset. You can do a lot with it. The pivot table might include zero values. In this lesson, you will learn how to hid
5 min read
How to Hide Zero Values in Excel Microsoft Excel enables us to format, organize and calculate data in a spreadsheet. It's a great tool for preparing datasets and it makes those datasets easier for users and analysts to analyze the data. Sometimes these datasets contain zero values that are not required to be seen. Excel provides an
6 min read
SQL Query to Exclude Multiple Values To exclude multiple values to be fetched from a table we can use multiple OR statements but when we want to exclude a lot of values it becomes lengthy to write multiple AND statements, To avoid this we can use the NOT IN clause with the array of values that need to be excluded with the WHERE stateme
2 min read