Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

MySQL ISNULL() function

ISNULL() function

MySQL ISNULL() function returns 1 when the expression is NULL otherwise it returns 0.

Syntax:

ISNULL(expr)

MySQL Version: 5.6

Example: ISNULL() function with non-null value

In the following MySQL statement, given argument is a non-NULL value. So , ISNULL function returns 0.

Code:

SELECT ISNULL(1+0);

Sample Output:

mysql> SELECT ISNULL(1+0);
+-------------+
| ISNULL(1+0) |
+-------------+
|           0 | 
+-------------+
1 row in set (0.03 sec)

PHP script:

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>example-is-null()- php mysql examples | w3resource</title>
<meta name="description" content="example-is-null()- php mysql examples | w3resource">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<h2>Checking if NULL,using MySQL:</h2>
<table class='table table-bordered'>
<tr>
<th>ISNULL(1+0)</th>
</tr>
<?php
$hostname="your_hostname";
$username="your_username";
$password="your_password";
$db = "your_dbname";
$dbh = new PDO("mysql:host=$hostname;dbname=$db", $username, $password);
foreach($dbh->query('SELECT ISNULL(1+0)') as $row) {
echo "<tr>";
echo "<td>" . $row['ISNULL(1+0)'] . "</td>";
echo "</tr>";
}
?>
</tbody></table>
</div>
</div>
</div>
</body>
</html>

View the example in browser

JSP script:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>example-is-null()</title>
</head>
<body>
<%
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
String Host = "jdbc:mysql://localhost:3306/w3resour_bookinfo";
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
connection = DriverManager.getConnection(Host, "root", "datasoft123");
statement = connection.createStatement();
String Data ="SELECT ISNULL(1+0)";
rs = statement.executeQuery(Data);
%>
<TABLE border="1">
<tr width="10" bgcolor="#9979">
<td>ISNULL(1+0)</td>
</tr>
<%
while (rs.next()) {
%>
<TR>
<TD><%=rs.getString("ISNULL(1+0)")%></TD>
</TR>
<%   }    %>
</table>
<%
rs.close();
statement.close();
connection.close();
} catch (Exception ex) {
out.println("Can’t connect to database.");
}
%>
</body>
</html>

Example : ISNULL() function with NULL value

In the following MySQL statement, given argument is a non-NULL value. So , ISNULL function returns 0.

Code:

SELECT ISNULL(0/1);

Sample Output:

mysql> SELECT ISNULL(0/1);
+-------------+
| ISNULL(0/1) |
+-------------+
|           0 | 
+-------------+
1 row in set (0.00 sec)

Slideshow of MySQL Comparison Function and Operators

MySQL Comparison Function and Operators, slide presentation

Previous: IS
Next: LEAST()