select datetime without specifying the time part? - mysql

I have a table like this:
I want to to do something like this:
select * from stat_tps Where min_date ='2013-06-12'
but the problem as you see the min_date is datetime format, so I have to provide the hh:mm:s part. I was wondering if there is any way that I can apply my query without specifying the hours-minutes and seconds?
thanks..

select * from stat_tps
Where date(min_date) = '2013-06-12'
But that won't take use of indexes if you have one on the min_date column. Better use
select * from stat_tps
Where min_date >= '2013-06-12'
and min_date < '2013-06-13'

Use the DATE() function:
SELECT * FROM stat_tps WHERE DATE(min_date) ='2013-06-12'

You need to specify a range, this will effecitvely use an index.
select * from stat_tps
Where min_date BETWEEN '2013-06-12' AND DATE_ADD('2013-06-12',INTERVAL 1 DAY)

Use the SUBSTR function to chop off the time.

Related

selecting time only from datetime

Can someone please explain to me why the first code doesn't work? in the first one I am try to extract time only and input in the Time column.
UPDATE SampleData_0816
SELECT Time1 = DATE_FORMAT(opened_first_time, '%h:%i')
SELECT DATE_FORMAT(opened_first_time, '%h:%i')
FROM SampleData_0816
If you use the update function you must use SET instead of SELECT
UPDATE SampleData_0816
SET Time1 = DATE_FORMAT(opened_first_time'%H:%i:%s')
SELECT DATE_FORMAT(colName, '%Y-%m-%d') DATEONLY,
DATE_FORMAT(colName,'%H:%i:%s') TIMEONLY
You can achieve that using DATE_FORMAT().

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 order by time am/pm

I have a row in a table which holds the time of day in am/pm format e.g.
timeField
---------
9.00am
10.00pm
7.00am
etc...
Is there a way in mysql to order these values?
You can do this by using STR_TO_DATE function in MySQL:
SELECT STR_TO_DATE('10.00pm','%h.%i%p');
try this:
SELECT *
FROM table_name
ORDER BY STR_TO_DATE(timeField,'%h.%i%p');
Example: SQLFiddle
In order to order it you need to format the date field fist.
try this
SELECT COl1, COl2, DATE_FORMAT(yourdate, '%k:%i:%s') AS mydate
FROM your_table
ORDER BY mydate
Enjoy
Use Below Query
select * from 'Your Table Name' order by str_to_date('yourtimefield', '%l:%i %p');
SELECT * FROM table ORDER BY Name
This put everything in order for me: Alphabetized, Dates and Time

How to select rows within some date range

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'

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'