w3resource logo


MySQL MONTHNAME function

MySQL MONTHNAME() function

rating has average rating 9 out of 10. Total 5 users rated.

<<PreviousNext>>

Description

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(date);

Where date is a date.

Example :

Code

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

Explanation :

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

Output

MYSQL MONTHNAME()

PHP script

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>php mysql examples</title>
</head>
<body>
<?php
echo "<h2>Extract name of the month from 2009-05-18 : </h2>";
echo "<table border='1' style='border-collapse: collapse;border-color: silver;'>";
echo "<tr style='font-weight: bold;'>";
echo "<td width='300' align='center'>Name of the month from  2009-05-18</td>";
echo "</tr>";
include("../dbopen.php");
$result = mysql_query("SELECT MONTHNAME('2009-05-18')");
while($row=mysql_fetch_array($result))
{
echo "<tr>";
echo "<td align='center' width='200'>" . $row["MONTHNAME('2009-05-18')"] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>

View the example in browser

Example : MONTHNAME() function using table

Sample table : purchase

Code

SELECT invoice_no,MONTHNAME(invoice_dt)
FROM purchase; 

Explanation

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

Output

MYSQL MONTHNAME() EXAMPLE

PHP script

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>php mysql examples</title>
</head>
<body>
<?php
echo "<h2>List of invoice number and name of the month from invoice date : </h2>";
echo "<table border='1' style='border-collapse: collapse;border-color: silver;'>";
echo "<tr style='font-weight: bold;'>";
echo "<td width='300' align='center'>Invoice no.</td><td width='300' align='center'>MONTHNAME(invoice_dt)</td>";
echo "</tr>";
include("../dbopen.php");
$result = mysql_query("SELECT invoice_no,MONTHNAME(invoice_dt)
FROM purchase");
while($row=mysql_fetch_array($result))
{
echo "<tr>";
echo "<td align='center' width='200'>" . $row['invoice_no'] . "</td>";
echo "<td align='center' width='200'>" . $row['MONTHNAME(invoice_dt)'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>

View the example in browser

Example : MONTHNAME() function with where clause

Sample table : purchase

Code

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

Explanation

The above 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).

Output

MYSQL MONTHNAME() EXAMPLE1

PHP script

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>php mysql examples</title>
</head>
<body>
<?php
echo "<h2>List of invoice number and name of the month from invoice date, where month of invoice is after July : </h2>";
echo "<table border='1' style='border-collapse: collapse;border-color: silver;'>";
echo "<tr style='font-weight: bold;'>";
echo "<td width='300' align='center'>Invoice no.</td><td width='300' align='center'>MONTHNAME(invoice_dt)</td>";
echo "</tr>";
include("../dbopen.php");
$result = mysql_query("SELECT invoice_no,MONTHNAME(invoice_dt)
FROM purchase 
WHERE MONTH(invoice_dt)>7");
while($row=mysql_fetch_array($result))
{
echo "<tr>";
echo "<td align='center' width='200'>" . $row['invoice_no'] . "</td>";
echo "<td align='center' width='200'>" . $row['MONTHNAME(invoice_dt)'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>

View the example in browser

photo credit: rosipaw. Photo is used under creative Common License.

<<PreviousNext>>