MYSQL order by time am/pm - mysql

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

Related

How filter data from database according alphabetical month name

I want to filter data from the database according to alphabetically month name, if I have date formated in the database like this: 2019-02-21.
I have put a screenshot of my data table structure here:
You can use the following using MONTHNAME:
SELECT * FROM table_name WHERE MONTHNAME(column_name) = 'February'
... or you can use the following using DATE_FORMAT:
SELECT * FROM table_name WHERE DATE_FORMAT(column_name, '%M') = 'February'

Can I add time with date in mysql?

Can I add time with date in mysql ?
( e.g. date=2017-04-05 00:00:39 and time=00:10:00
result should be 2017-04-05 00:10:39 )
I have tried
SELECT dateadd(start_time,max_bidding_time) from product;
Yes, you can use ADD_TIME() function:
SELECT ADDTIME('2017-04-05 00:00:39', '00:10:00');
yes. you can change the structure of your table date field. Select type like "datetime" in database.
SELECT
ADDTIME(start_time, DATE_FORMAT(time,'%H:%i:%s')) AS updatetime
FROM product;

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;

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'