I have a table column named date_created where I store datetime values in VARCHAR e.g. 16-06-2013 10:49:29
I want to get results through mysql query between tow dates. I am using query like this:
... WHERE date_created BETWEEN '06-08-2013 22:30:18' AND '28-08-2013 22:30:22' ...
This query return results but that result also includes older date records that are between 06 and 28 of every month. I also use < and > but these also did not work.
How can I get results that only include records between '06-08-2013 22:30:18' AND '28-08-2013 22:30:22'
You can use MySQL-function STR_TO_DATE to format strings into DATETIME.
SELECT STR_TO_DATE('06-08-2013 22:30:18', '%d-%m-%Y %H:%i:%s');
# 2013-08-06 22:30:18
I would recommend to reformat all VARCHARs to DATETIMEs with a single UPDATE and fix the code.
you need to use the STR_TO_DATE function to convert your date string into a date that can be used for comparisons.
STR_TO_DATE(date_created, '%d-%m-%Y %H:%i:%s') BETWEEN STR_TO_DATE('06-08-2013 22:30:18', '%d-%m-%Y %H:%i:%s') AND STR_TO_DATE('28-08-2013 22:30:22', '%d-%m-%Y %H:%i:%s')
Related
I have a column in MySQL datatable named added_date, datatype of it is varchar(255) which is inserting date like mm/dd/yyyy. I have a lot of data in the table for dates like '5/12/2018', '4/10/2018', '3/5/2018' etc. Now, I want to get data for may, 2018 month only.
How to have the data for may, 2018 only?
Thanks.
MySQL retrieves and displays dates in 'YYYY-MM-DD' format. Thus, you have to use STR_TO_DATE function to convert string to date. Then it is possible to use BETWEEN keyword in WHERE clause to set specific date range:
SELECT *
FROM MyTable
WHERE STR_TO_DATE(added_date, '%m/%d/%Y') BETWEEN '2018-05-01' AND '2018-05-31';
You could try the query online.
Convert varchar to date within a format that is suitable for your condition:
SELECT * FROM your_table WHERE STR_TO_DATE(added_date, '%c%Y') = '52018';
You can use the same function in the select component to return the date too:
SELECT STR_TO_DATE(added_date, '%Y-%m-%d') AS fDate FROM your_table WHERE STR_TO_DATE(added_date, '%c%Y') = '52018';
In mySQL I have a timestamp column named when
2015-01-07 16:43:21
My question is how using PHP/mySQL
For now I can show the results based on month number like
... where month(`when`) = '1' ...
but what if I want to show the rows of a particular date for example 2015-01-05 ?
I will pass the preferable date through a variable into the sql query.
Just wrap the date() function around your date string which is already in the YYYY-MM-DD format, and also around your timestamp field in the where clause:
select * from table where date(`when`) = date('2015-01-05');
From the documentation for date():
Extracts the date part of the date or datetime expression expr.
Here's how to convert the date via MySQL:
SELECT
DATE_FORMAT(`when`, '%Y-%m-%d') AS my_date
FROM
my_table
WHERE
MONTH(`when`) = 1;
Take a look at the MySQL documentation here for other formats: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format
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
In my sql query I output dates in chronological order.
The dates in my database are stored in d-M-Y format.
What I want to do is sort the results by dates equal to or greater than today to be output first.
In my query I have this sort in my query
...From $db ORDER BY STR_TO_DATE(sortdate, '%d-%M-%Y') ASC
Can anyone tell me if I can do a comparison on todays date as each record is output from the db?
This will give me todays date
$todaysdate = date("d-M-Y", time());
but can anyone tell me if I can build that into my query?
Thanks in advance.
check mysql DATEDIFF in combination with CURRENT_DATE ==>
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_current-date
My guess is that you saved the date in a VARCHAR column. Please don't do that, you make it very complicated for yourself when you want to do stuff (like this) with the date. I'd suggest that you convert the column to a DATE field and then just use:
SELECT * FROM my_table WHERE my_date_field >= CURDATE()
And if you want to output the date in the d-m-Y format, you can use DATE_FORMAT()
You really should be storing the dates in a dateTime format. That will make it much easier to do all sorts of orders, comparisons and plenty of other things. You could for example, then use the mysql now() function to only get the results you need?
...From $db where sortDate>=now() ORDER BYsortdate ASC
Assuming sortdate is datetime field, in order to display dates equal to or greater than today first,could use UNION.
SELECT * FROM my_table WHERE sortdate>= CURDATE()
UNION
SELECT * FROM my_table WHERE sortdate< CURDATE()
You can use WHERE sortdate >= $todaysdate
Just put this condition in where like date_column >= curdate()/$todaysdate
thanks
My script works fine for all sales but skips all sales on the 15th.
The MySQL rows for the period look like this:
ID: 10 START: 2010-12-01 END: 2010-12-15
The MySQL rows for the sales look like this:
DATE: 2010-12-15 20:40:26
$period_info=mysql_fetch_array(mysql_query("SELECT start,end FROM period WHERE id='$period' LIMIT 1"));
$start=$period_info["start"];
$end=$period_info["end"];
$total_sales=mysql_num_rows(mysql_query("SELECT * FROM sales WHERE seller='$seller' AND date BETWEEN '$start' AND '$end'"));
And ideas?
This is because MySQL uses 00:00:00 as the time for the DATE type. I think you can use:
"SELECT * FROM sales WHERE seller='$seller' AND (CAST date AS DATE) BETWEEN '$start' AND '$end'".
Only compare the date part of the datetime, and ignore the time part:
WHERE DATE(`date`) BETWEEN '$start' AND '$end'
For best results when using BETWEEN with date or time values, use CAST() to explicitly convert the values to the desired data type. Examples: If you compare a DATETIME to two DATE values, convert the DATE values to DATETIME values. If you use a string constant such as '2001-1-1' in a comparison to a DATE, cast the string to a DATE.