Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Query put a word in a field instead of 0?

Tags:

sql

database

When i run sql query such as;

Select 
    F1 as name, 
    F2 as phone 
from Table where condition = true

If F2 = 0 i need to that field to be No Phone Number instead of null or 0

like image 836
Lesther Avatar asked Feb 11 '14 14:02

Lesther


People also ask

How can I replace zero values in SQL?

UPDATE [table] SET [column]=0 WHERE [column] IS NULL; Null Values can be replaced in SQL by using UPDATE, SET, and WHERE to search a column in a table for nulls and replace them. In the example above it replaces them with 0.

How do I exclude 0 in SQL?

To exclude entries with “0”, you need to use NULLIF() with function AVG(). Insert some records in the table using insert command. Display all records from the table using select statement.

How do I match a word in SQL?

SQL pattern matching allows you to search for patterns in data if you don't know the exact word or phrase you are seeking. This kind of SQL query uses wildcard characters to match a pattern, rather than specifying it exactly. For example, you can use the wildcard "C%" to match any string beginning with a capital C.


Video Answer


3 Answers

You can do that with a CASE statement:

SELECT
CASE WHEN F2 = '0' THEN '(No Phone Number)' ELSE F2 END AS Phone
...
like image 72
D Stanley Avatar answered Sep 29 '22 07:09

D Stanley


SELECT
CASE WHEN F2 = '0' OR F2 IS NULL THEN '(No Phone Number)' ELSE F2 END AS Phone 
like image 29
JBrooks Avatar answered Sep 29 '22 05:09

JBrooks


You will need to use CASE as;

SELECT 
    F1 As name, 
    CASE WHEN F2 IS NULL OR F2 = '0' THEN 'No Phone Number' ELSE F2 END As phone
FROM Table
WHERE condition = 'true'
like image 37
Saro Taşciyan Avatar answered Sep 29 '22 07:09

Saro Taşciyan