Actually, I am trying to sort my rows by Date field in which the date is stored in the form of like
DateC
===========
April, 2012
May, 2012
January, 2013
Date
===========
2013-01-03 10:51:23
2013-02-19 10:51:23
2013-03-26 10:51:23
But i am not able to sort it simply by just using ORDER BY datec DESC.
I can't use ORDER BY FIELD (datec, 'December 2012', November 2012'...) because there are two dates stored in my database. One is the timestamp i.e date, and the other is custom date i.e. datec which the user enters by himself. If the datec field is empty, it outputs the date field.
So, what I want is it should order the rows by both the date columns...
Please Help. If you have some questions, please ask mee....
Thanks in advance...
I think this is what you are looking for, using STR_TO_DATE and COALESCE:
select *
from yourtable
order by
COALESCE(STR_TO_DATE(datec, '%M %Y'),date)
SQL Fiddle Demo
Date are stored in string format. so you need to use STR_TO_DATE() function
Try this
SELECT * FROM table ORDER BY STR_TO_DATE(datec, "%M, %Y") DESC
This is one advantage of storing dates as string on the database. The ordering can still be applied provided that you need to use STR_TO_DATE() function to convert the string into valid dates.
SELECT *
FROM tableName
ORDER BY STR_TO_DATE(datec, '%M, %Y') DESC
SQLFiddle Demo
LINK
STR_TO_DATE()
if you have time to alter the schema, the best way to do is to store those dates as DATE or DATETIME on the database. If you worry about the formatting of value during SELECT statement, there is still a function that called DATE_FORMAT() which convert the date into string at your desired format.
DATE_FORMAT()
Example,
SELECT DATE_FORMAT(CURDATE(), '%M, %Y') DateC
will yield
March, 2013
SQLFiddle Demo
UPDATE 1
SELECT *
FROM tableName
ORDER BY STR_TO_DATE(datec, '%M, %Y') DESC,
date DESC
SQLFiddle Demo
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';
i have a database values values like this format
premium_paid_date
31-10-17
30-10-17
11-10-18
31-08-18
31-10-17
25-11-17
but it was stored type is var-char. my customer wants this table ORDER BY year. which mean based on last two digits and followed by month and date. how can i order this using MySQL without convert into date format
You can convert string to date in your order by statement. Like
SELECT Premium_paid_date FROM Table_Name
ORDER BY Convert( DateTime, premium_paid_date, 102) DESC
see date format
Something like:
SELECT [premium_paid_date]
FROM [dbo].[premium_paid_date]
ORDER BY SUBSTRING(Date,5,6)
then you are only ordering it by the last 2 digits of the date (the year) using the substring.
Finally i solved this issue my answer is
SELECT * FROM `tablename` order by STR_TO_DATE(premium_paid_date, '%d-%m-%Y') desc
I have a query (written to be easier from a class)
$cms->my_query('SELECT * FROM location');
Which will return an array
Though I have a DATE type in the mySQL Table which it is formatted like so 2014-06-22
Is there a way I can format so it's like this Nov 04 2008 11:45 PM with using DATE_FORMAT(NOW(),"%b %d %Y %h:%i %p") now I believe DATE cannot use this properly so i'd have to use DATETIME but if that is the case it's fine but how do I select all and change date at the same time?
Example
$cms->my_query('SELECT * FROM location DATE_FORMAT(NOW(),"%b %d %Y %h:%i %p")');
I just don't want that ugly 2014-06-22 and I have very little knowledge of mySQL and I am learning as I try new things out. So if someone who is more skilled please explain the best scenario for me, I'd like to learn and I am willing!
The first argument of DATE_FORMAT() is the date you want to format. Putting NOW() in there means you will return the current date.
First, you'll need to change the date column to DATETIME, then use that column as the first argument to DATE_FORMAT. Try this:
SELECT *, DATE_FORMAT(mydate ,"%b %d %Y %h:%i %p") as date_added FROM location
Where mydate is the DATETIME column from the table.
See demo
The column need to be in type DATETIME. With date_time_column is a column in location table. Should be like this:
$cms->my_query('SELECT DATE_FORMAT(date_time_column,"%m-%d-%Y %r") FROM location');
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'
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