w3resource

MySQL MONTHNAME() function

MONTHNAME() function

MySQL MONTHNAME() returns the full name of the month for a given date. The return value is within the range of 1 to 12 ( January to December). It Returns NULL when month part for the date is 0 or more than 12

Syntax:

MONTHNAME(date1)

Where date1 is a date.

Syntax Diagram:

MySQL MONTHNAME() Function - Syntax Diagram

MySQL Version: 5.6


Video Presentation

Pictorial Presentation

Pictorial Presentation of MySQL MONTHNAME() function

Example: MySQL MONTHNAME() function

The following statement will return the full name of the month for the given date 2009-05-18.

Code:

SELECT MONTHNAME('2009-05-18');

Sample Output:

mysql> SELECT MONTHNAME('2009-05-18');
+-------------------------+
| MONTHNAME('2009-05-18') |
+-------------------------+
| May                     | 
+-------------------------+
1 row in set (0.02 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-monthname-function - php mysql examples | w3resource</title>
<meta name="description" content="example-monthname-function - 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>Extract name of the month from 2009-05-18:</h2>
<table class='table table-bordered'>
<tr>
<th>Name of the month from  2009-05-18</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 MONTHNAME("2009-05-18")') as $row) {
echo "<tr>";
echo "<td>" . $row['MONTHNAME("2009-05-18")'] . "</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-monthname-function</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 MONTHNAME('2009-05-18')";
rs = statement.executeQuery(Data);
%>
<TABLE border="1">
<tr width="10" bgcolor="#9979">
<td>Name of the month from 2009-05-18</td>
</tr>
<%
while (rs.next()) {
%>
<TR>
<TD><%=rs.getString("MONTHNAME('2009-05-18')")%></TD>
</TR>
<%   }    %>
</table>
<%
rs.close();
statement.close();
connection.close();
} catch (Exception ex) {
out.println("Can’t connect to database.");
}
%>
</body>
</html>

Example: MONTHNAME() function using table

The following statement will return invoice_no and the full name of the month for the invoice_dt from the table purchase.

Sample table: purchase


Code:

SELECT invoice_no,MONTHNAME(invoice_dt)
FROM purchase;

Sample Output:

mysql> SELECT invoice_no,MONTHNAME(invoice_dt)
    -> FROM purchase;
+------------+-----------------------+
| invoice_no | MONTHNAME(invoice_dt) |
+------------+-----------------------+
| INV0001    | July                  | 
| INV0002    | August                | 
| INV0003    | September             | 
| INV0004    | August                | 
| INV0005    | July                  | 
| INV0006    | September             | 
+------------+-----------------------+
6 rows in set (0.05 sec)

PHP script

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>example1-monthname-function - php mysql examples | w3resource</title>
<meta name="description" content="example1-monthname-function - 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>List of invoice number and name of the month from invoice date:</h2>
<table class='table table-bordered'>
<tr>
<th>Invoice no.</th><th>MONTHNAME(invoice_dt)</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 invoice_no,MONTHNAME(invoice_dt)
FROM purchase') as $row) {
echo "<tr>";
echo "<td>" . $row['invoice_no'] . "</td>";
echo "<td>" . $row['MONTHNAME(invoice_dt)'] . "</td>";
echo "</tr>"; 
}
?>
</tbody></table>
</div>
</div>
</div>
</body>
</html>

View the example in browser

Example: MONTHNAME() function with where clause

The following statement will return invoice_no and the full name of the month for the invoice_dt as MONTHNAME(invoice_dt) from the table purchase, making sure that value of the MONTHNAME(invoice_dt) is more than 7 (i.e. July).

Sample table: purchase


Code:

SELECT invoice_no,MONTHNAME(invoice_dt)
FROM purchase            
WHERE MONTH(invoice_dt)>7;

Sample Output:

mysql> SELECT invoice_no,MONTHNAME(invoice_dt)
    -> FROM purchase            
    -> WHERE MONTH(invoice_dt)>7;
+------------+-----------------------+
| invoice_no | MONTHNAME(invoice_dt) |
+------------+-----------------------+
| INV0002    | August                | 
| INV0003    | September             | 
| INV0004    | August                | 
| INV0006    | September             | 
+------------+-----------------------+
4 rows in set (0.00 sec)

PHP script

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>example2-monthname-function - php mysql examples | w3resource</title>
<meta name="description" content="example2-monthname-function - 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>List of invoice number and name of the month from invoice date, where month of invoice is after July:</h2>
<table class='table table-bordered'>
<tr>
<th>Invoice no.</th><th>MONTHNAME(invoice_dt)</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 invoice_no,MONTHNAME(invoice_dt)
FROM purchase 
WHERE MONTH(invoice_dt)>7') as $row) {
echo "<tr>"; 
echo "<td>" . $row['invoice_no'] . "</td>";
echo "<td>" . $row['MONTHNAME(invoice_dt)'] . "</td>";
echo "</tr>";
}
?>
</tbody></table>
</div>
</div>
</div>
</body>
</html>

View the example in browser

All Date and Time Functions:

Click here to see the MySQL Date and time functions.

Previous: MONTH()
Next: NOW()