want to fetch only the month part from a date in mysql
Is it possible to fetch only the month part f开发者_如何转开发rom a sql date entry?
like the date is stored like '2011-01-06'
. if i want to fetch only the '01'
portion how can I do that?
thanxx in advance.
You can use DATE_FORMAT
to do it, like this:
SELECT DATE_FORMAT(DateField, '%m') FROM table
you can do it in mysql
like :
select DATE(date_field) from mytable
from manual :
MONTH(date)
Returns the month for date, in the range 1 to 12 for January to December, or 0 for dates such as '0000-00-00' or '2008-00-00' that have a zero month part.
mysql> SELECT MONTH('2008-02-03');
-> 2
in a mysql query, you can just do this:
MONTH(date_field)
mysql> SELECT MONTH('2008-02-03');
Result: 2
You can use the MONTH
function of MySQL:
SELECT MONTH('2011-01-06');
Result:
1
To get month name instead, you can use MONTHNAME
like this:
SELECT MONTHNAME('2011-01-06');
Result:
January
You may want to try
select substring(`mydate_field`,6,2) from `table_name`;
In ANSI-SQL you can use EXTRACT:
SELECT
EXTRACT('month' FROM fieldname)
FROM
tablename;
This works in many databases, MySQL as well.
精彩评论