want to fetch only the month part from a date in mysql - php

Is it possible to fetch only the month part from 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.

Related

Get data from the current month

I have a question. So I need to get data from the last days of months using a sql request. For example :
Now date is 22/08/2016, I need to get data from 01/08/2016-22/08/2016;
Now date is 04/06/2016, I need to get data from 01/06/2016-04/06/2016;
Sorry but I dont have an idea. Thx in advance and sorry for my english
I would do something like this:
SELECT <table-columns> FROM <table-name>
WHERE (<date-column> BETWEEN DATE_FORMAT(NOW() ,'%Y-%m-01') AND NOW() )
This DATE_FORMAT(NOW() ,'%Y-%m-01') will return the first date of your current month and year.
Need to use something like:
YEAR(time) = YEAR(NOW())
AND MONTH(time) = MONTH(NOW())
You are looking for something like this:
SELECT * FROM tbl_name WHERE yourDate.MONTH()=NOW().MONTH();
Refine the syntax from here.
You need to get the first day of the month for a given date then, in order to make your comparison with the database:
select date(concat_ws('-',year(curdate()),lpad(month(curdate()),2,'00'),'01')); gives you that (for curdate() here)

PHP/MySQL targeting only month from date field

My table has a date column formatted as yyyy-mm-dd.
Is it possible to have a query that only lists results found for any day in a specific month? There could be any month of any year in this table, but say i want to list them for only june 2013?
Thanks
SELECT * FROM tablename WHERE date_column_name LIKE '2013-06%'
select * from your_table
where date_column between '2013-06-01' and '2013-06-30'

SQL Query Date Format Two Columns

I have two columns (Date and Time) in database that I would like to get from SQL query with specific date format. It can be two values from sql query or one. Here are the details.
Display Format:
Sunday 03/31/2013 12:13:27 AM
Database Columns:
EndDate (Date yyyy-mm-dd)
EndTime (Time hh:mm:ss)
This is what you need:
DATE_FORMAT(CONCAT(EndDate, EndTime), '%W %m/%d/%Y %r')
The date_format() function gives you everything you need.
Example. Let's say you have columns aDate and aTime in your table, and you need to show the date like 'dd-mm-yyyy' and the time like 'hh:mm'. So:
select
date_format(aDate, '%d-$m-%Y'), date_format(aTime, '%H:%i')
from myTable;
Check the reference manual (Chapter 12 - Functions and operators for MySQL 5.5)
If you need to concatenate the results, then:
select
concat(date_format(aDate, '%d-$m-%Y'), ' ', date_format(aTime, '%H:%i'))
from myTable;
This seems to work for me:
select date_format(concat(enddate,' ',endtime),'%W %m/%d/%Y %r')
from yourtable
SQL Fiddle Demo
Good documentation for date_format: https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format

SQL query with temporary column showing the first 4 characters of the date column

instead of displaying the whole date i only want to display the first 4 characters of the date column.
SELECT Test.Date From Test
wanting Date to be limited to 4 characters long
This will give you the year.
SELECT YEAR(Date) as Year FROM Test
or you can also use DATE_FORMAT()
SELECT DATE_FORMAT(Date, '%Y') as Year FROM Test
SELECT LEFT(Test.Date, 4) FROM Test
Should work just fine, assuming the format is "2013-04-10"

php mysql date extract with distinct

in mysql table i have the field 'date' which has the data as
2012-08-02 02:33:26
2012-08-02 05:33:26
2012-07-02 06:33:26
2012-06-02 01:33:26
2012-06-10 09:33:26
2011-05-10 10:33:26
2011-04-10 02:33:26
like that
i want to get the output as
2012-08
2012-07
2012-06
2011-05
2011-04
ie get the unique year and month (distinct of year and month of the date field)
I used the following query
SELECT distinct(YEAR(date)) FROM table
It returns the year only. i want to get the output as defined above. is there any possibility to get the out put like this. How do i write the select query.
Please help to this.
You might want to try this:
SELECT DISTINCT DATE_FORMAT(colName, '%Y-%m')
FROM tableName
SEE HERE: Date_Format( )
SELECT DISTINCT DATE_FORMAT(`date`, '%Y-%m') FROM table
you can get full help from here http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
suppose you are getting like this from Database:-
$row['date']=2012-08-02 02:33:26;
then you can use explode function:-
$var=explode(" ",$row['date']);
$date=$var[0];//here you will get 2012-08-02
$res=explode("-",$date);
$res1=$res[0];//you will get 2012
$res2=$res[1];//you will get 08
$res3=$res[2];//you will get 02
now the final result:-
$final=$res1."-".$res2;//you will get 2012-08
this is an alternate answer if you don't want to handle this on MySQL end so you can handle this by using PHP concepts.

Categories