Michael Garrison
Michael Garrison

Reputation: 941

Display all null mysql values as N/A

I'm working on a clients website and they want a simple check in page for their overnight employees. Each employee can fill out up to 6 clients names on a form. The data entered on the form is then stored in a database. The data is then displayed on an admin page so that it can be checked to make sure each employee checks in. The issue I am having is that I want all null values to display as N/A or --, I haven't decided. I know I have to have a loop but I'm not sure how to read the null values. Any help would be greatly appreciated!

Upvotes: 1

Views: 3006

Answers (4)

Ray
Ray

Reputation: 884

I recommend storing N/A in the database on the form submit. If using php

If (Isset($var)!=true){
$var="N/a";

}

Insert $var into database

Upvotes: 0

John Woo
John Woo

Reputation: 263703

Use COALESCE or ISNULL

SELECT COALESCE(FieldName, 'N/A')

or

SELECT  ISNULL(FieldName, 'N/A')

Upvotes: 1

Peter
Peter

Reputation: 793

You have not mentioned fully if you wish for the database data to be in tact and only the data to be modified for output.

If you only want the output modified you could do :-

SELECT field1, field2, COALESCE(field3,'N/A')
FROM tableName;

In this example if field3 is null it will replace it in the returned data to be N/A otherwise it will be left as is.

Upvotes: 4

Daniel A. White
Daniel A. White

Reputation: 190915

You could in your select statement, do ISNULL:

SELECT ISNULL(field, 'N/A') FROM Table

Upvotes: 4

Related Questions