How to select rows within some date range - mysql

I want to select rows that falls between some date range, I tried the following query but it didn't work.
SELECT * FROM tbl WHERE DATE_FORMAT(date_col, '%2011-%c-%e') BETWEEN '2011-11-28' AND '2011-12-5'
It doesn't seem like the BETWEEN keyword works on date. Please how do I get the results? Thanks

You don't need to use DATE_FORMAT if you want to compare dates.
SELECT *
FROM tbl
WHERE DATE(date_col) BETWEEN '2011-11-28' AND '2011-12-05'
Your code compares strings, assuming you use DATE_FORMAT(date_col, '%Y-%c-%e')

SELECT * FROM tbl WHERE DATE_FORMAT(date_col, '%Y-%m-%d') BETWEEN '2011-11-28' AND '2011-12-5'

Related

Using date separated colums to compare in mysql

I have three columns in a table to save a date, but now I need to compare those three columns as a date using the CONCAT and STR_TO_DATE function, but the comparision does not work, I don't see the error.
Query
SELECT * FROM mytable WHERE STR_TO_DATE(CONCAT(myyear,'-',mymonth,'-',myday),'%Y-%m-%d')>'2015-01-01'
For your example, why not just use this?
select *
from mytable
where myyear >= 2015

MySQL Timestamp how to write a query to select weekdays only

I want to write mysql for filter weekday result only. I used timestamp for store dates. can anyone help me to this?
eg; SELECT * FROM mytable WHERE WEEKDAY('mydate_field')
Considering you mean excluding Sunday and Saturday.
SELECT * FROM mytable
WHERE DAYOFWEEK('mydate_field') in (2,3,4,5,6)
Try something like this:
SELECT * FROM mytable
WHERE WEEKDAY('mydate_field')<5
you can also try like this.
SELECT * FROM your_table WHERE WEEKDAY(mydate_field)>0 AND WEEKDAY(mydate_field)<6;

mysql DATE_FORMAT() between gives me bug

I am trying folowing on my_table where modifiedtime is of type datetime
select DATE_FORMAT(modifiedtime,'%d-%m-%Y') from my_table
where DATE_FORMAT(modifiedtime,'%d-%m-%Y') between '05-11-2013' and '28-11-2013';
The query gives me some other record too which are not falls between above dates, for example there is a record in result set dated '04-01-2014'
select DATE_FORMAT(modifiedtime,'%d-%m-%Y') from my_table
where DATE_FORMAT(modifiedtime,'%d-%m-%Y')='05-11-2013'
this query works fine and gives all the records for the given date
why the first behaves like that?
How can i correct it?
what is the efficient way to implement it?
such that i can get all the records only between given two dates.
SELECT
DATE_FORMAT(modifiedtime, '%d-%m-%Y')
FROM
my_table
WHERE
modifiedtime BETWEEN STR_TO_DATE('05-11-2013', '%d-%m-%Y') AND STR_TO_DATE('28-11-2013', '%d-%m-%Y');
DATE_FORMAT() returns TEXT type column and dates can't be applied.
Use without DATE_FORMAT in the WHERE
select DATE_FORMAT(modifiedtime,'%d-%m-%Y') from my_table
where modifiedtime between '05-11-2013' and '28-11-2013';
you DATE_FORMAT function converts the column modifiedtime to String.
and hence in your first query you do a string comparison rather then a date comparison.
Also your date literal is not incorrect. It must be of form YYYY-MM-DD
select DATE_FORMAT(modifiedtime,'%d-%m-%Y') from my_table
where cast(modifiedtime as date) between '2013-11-05' and '2013-11-28';

How do I run my query and add the convert_tz(FROM (FROM_UNIXTIME

This will be simple for you. I have a query that returns all the information in the table but the dates are all Unix timestamp and I need to convert that one column.
These are my 2 strings and I just need to know how to link them together:
Query 1:
SELECT * FROM `wrd_project_payment_transactions`
The conversion:
SELECT CONVERT_TZ(FROM_UNIXTIME(`datemade`), ##session.time_zone, '+00:00')
FROM `wrd_project_payment_transactions`
I'm a noob so be gentle.
Thanks!
SELECT *, CONVERT_TZ(FROM_UNIXTIME(`datemade`), ##session.time_zone, '+00:00') AS convertedTime
FROM `wrd_project_payment_transactions`

Retrieve records based on Date

I have a date column in my table.
Is there any way in mysql to retrieve record based on date excluding the time part?
I have tried appending * but does not seems to work.
SELECT * FROM myTable where filterDate="2010-08-01 *"
Is there any way to do it like this rather then filtering it by using between?
Thanks.
SELECT * FROM myTable where filterDate LIKE "%2010-08-01 %"
You can use DATE_FORMAT function of mysql
SELECT * FROM myTable where DATE_FORMAT(filterDate,'%Y-%m-%d') ="2010-08-01"
Try this
SELECT DATE_FORMAT(filterDate , '%d-%m-%Y') AS new_date FROM table_name WHERE
DATE_FORMAT(filterDate , '%d-%m-%Y') = '2010-08-01'