EDIT
The format in the .csv excel file for the date was 15-12-2011 and in the MySQL phpmyadmin database the column type was set to DATE so if I imported the file all data in date column changed to 0000-00-00. I did the next to fix this, went into the .csv file and selected the column for date and changed the format to 'Africa' so it would change everything from d-m-Y to Y-m-d. So in the .csv it would become 2011-12-15 instead of 15-12-2011. After importing it again the date was correctly imported without turning to 0000-00-00.
After that I Changed the ORDER BY in index.php:
$query = mysql_query("SELECT * FROM `newitems` ORDER BY startVisible DESC LIMIT $start, $per_page");
After that I changed the format date where was needed.
Available from: <?php
$timestamp = strtotime($query_row["startVisible"]);
echo date("d-m-Y", $timestamp);
?>
These scripts would first change the order by 1.year 2.month 3.day and after that change the format so it would echo 15-12-2011 instead of 2011-12-15 (how it is saved in the database)
Hope this will help others with the same problem.
Cheers.
below = question I asked
I have exported a table and modified some things that needed to be modified. Next I tried to import them into the table again but startVisible column Type was set to date. All my dates where stored as 0000-00-00. All my dates in the csv file are like 15-12-2011. I've changed the column Type to varchar and after that I could import the csv file with everything as 15-12-2011 I wanted to sort everything by date in www.domain.com/index.php. That is because the column type is not set to DATE. So I Changed the Column type from VARCHAR to DATE again and all the dates changed to 0000-00-00.
So no matter what I did everything changed to 0000-00-00. I removed everything and imported the same file and all the dates where imported correctly. So it may be a MySQL load fail or something.
I used:
$query = mysql_query("SELECT * FROM newItems ORDER BY startVisible DESC LIMIT $start, $per_page");
But all the dates are mixed now. It has to short them by d-m-Y but does not do that.
9-8-2011
9-3-2011
8-3-2011
7-6-2011
7-3-2011
7-11-2011
6-7-2011
Now everything is sorted correctly but I want to change the order it shows from 2011-12-15 to 15-12-2011. ( I MEAN ON THE WEBSITE not in MySQL ). in index.php I want to show everything as d-m-Y but I still want to sort (ORDER BY) them correctly. How to do that?
Thanks,
F4LLCON
This is because the query is now ordered by a varchar column so it is sorted alphabetically, So try to convert it to a date first:
select DATE_FORMAT(STR_TO_DATE(startVisible, '%d-%m-%Y'), '%d-%m-%Y') as startDate,...
from newItems
ORDER BY startDate DESC LIMIT $start, $per_page;
You can try MySQL function STR_TO_DATE. Here is an example query.
SELECT STR_TO_DATE(dt,'%d-%m-%Y') AS fmtDate, dt FROM test.text ORDER BY fmtDate
Prasad.
Your problem is the fact the column is no longer a date, and therefore cannot be sorted as a date. You've mentioned that it is now a VARCHAR. Ergo, it will sort alphabetically, not as a date.
The type is currently varchar, so it will use text sorting.
My advise is to add a column to your table of type date, fill it by converting the date-strings to proper dates using str-to-date() and then sort on that column. (To really do d-m-y you probably have to use
ORDER BY day(datecolumn) desc, month(datecolumn) desc, year(datecolumn) desc
But are you sure you don't just mean ORDER BY datecolumn DESC - most recent first?
You can update the columns using STR_TO_DATE
Do this.
Convert and update the columns using STR_DO_DATE1
Alter the column to date type.
If you do this in Laravel then use like this below
$covid19Result = DB::select('select DATE_FORMAT(STR_TO_DATE(your_field_name,"%d-%m-%Y"), "%Y-%m-%d") as startDate, active_people, death_people, recover_people from covid_graphs ORDER BY startDate DESC');
It works fine for me
Maybe, it will be useful to create a function (or a trigger), and use it when you need :
DROP FUNCTION IF EXISTS makedate;
CREATE FUNCTION makedate(your_field VARCHAR(10)) RETURNS DATE DETERMINISTIC
RETURN
DATE_FORMAT(STR_TO_DATE(your_field, '%d/%m/%Y' ) , '%Y-%m-%d' );
Hope it helps.
Related
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 am trying to get records from my database which are added on a specific date, for example records added on the 2013-09-05. However the records are stored in my database in datetime format so 2013-09-06 08:22:35
I have tried to use a like format thinking it might just match the first half but that makes no difference.
SELECT search_term, COUNT(search_term) as count FROM $tableName WHERE client_id = '{$client_id}' AND timedate LIKE '$dates' ORDER BY count DESC LIMIT $start, $limit
I would be grateful for suggestions or a nudge in the right direction.
Thanks
you could use DATE() to get date part from datetime, so change:
...AND timedate LIKE '$dates'...
to
...AND DATE(timedate) = '$dates'...
or you could also do:
...AND timedate LIKE '$dates%'...
You can convert date using DATE_FORMAT
Use in Query as below,
AND DATE_FORMAT(timedate, '%Y-%m-%d' ) = '$dates'
Document
All,
I have the following query:
SELECT ...
FROM....
WHERE...
ORDER BY gear_checkout_summary.check_out_date DESC";
I then have the following dates for example:
12/29/2012
11/30/2012
09/04/2012
07/21/2013
07/08/2013
06/29/2013
They are sorted this way when my results are returned. My data element is stored as VARCHAR(10) in my database since I messed up that design. Is there a way to change my query so it sorts the dates correctly?
Thanks!
Try this:
ORDER BY SUBSTR(check_out_date, 7) DESC, check_out_date DESC
This first orders by the year using the SUBSTR() function. For rows that have the same year, it then orders by the date itself. This second-level ordering doesn't require SUBSTR() because MM/DD is already ordered properly.
STR_TO_DATE will see your job
STR_TO_DATE(gear_checkout_summary.check_out_date, '%c/%e/%Y') // will give '2013-06-23' in Date format
This will convert your varchar field into date format.
This would put the date in the proper MySQL date format and then ordering would be simple.
SELECT
STR_TO_DATE(gear_checkout_summary.check_out_date,"%m/%d/%Y") AS fixed_date
...
FROM ...
WHERE ...
ORDER BY fixed_date DESC
As others have stated, you should fix the format in the database if at all anyway possible. You may need to change the insert statements, but it will be worth the extra work. Here is what you would need to do to format the dates in the database
ALTER TABLE gear_checkout_summary ADD check_out_date_temp DATE AFTER check_out_date;
UPDATE gear_checkout_summary SET check_out_date_temp = STR_TO_DATE(gear_checkout_summary.check_out_date,"%m/%d/%Y");
ALTER TABLE gear_checkout_summary DROP check_out_date;
ALTER TABLE gear_checkout_summary CHANGE check_out_date_temp check_out_date DATE;
Here is explanation of above code:
Create new column in your table.
Populate the new column with the dates from the previous column formatted correctly.
Drop the old bad formatted column.
rename new column to previous columns name.
Use str_to_date:
select *
from your_table
order by STR_TO_DATE(yourDateField,'%m/%d/%Y') desc;
This will convert the string to a valid MySQL date. Check this link: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_str-to-date
You should change your data type to DATE... it is the easiest and cleanest way to store dates.
I tried all codes and at the end I achieve my sorting on varchar date with this code...
ORDER BY SUBSTR(check_out_date, 7) DESC, ORDER BY SUBSTR(check_out_date, 1,2) DESC,ORDER BY SUBSTR(check_out_date, 4,2) DESC
First sort by year, then by month and at the end by day...
Greetings hope to get some help from you here as i been searching high and low for this.
This query works but its not the results i wanted more correctly not the right date format i want it in.
SELECT DISTINCT colum FROM table WHERE colum IS NOT NULL
This query gives me the dates
01.04.13
02.04.13
03.04.13
30.03.13
31.03.13
I wanted it to show latest date.
This info is posted in colum that stores the info as text. It is posted in the following format
dd.mm.yy
I wanted it then to show me the results as in
03.04.13 since this is todays date. i know i can limit it to 1 but still it will show the wrong date.
Thank you again for all help so far
First, you should not be storing a date as a string, you should store a data as a DateTime datatype.
Since you are storing it as a string, you will have to convert it to a date to get the max() date value. The following uses the STR_TO_DATE() function to convert the string to a date to get the max value:
select max(str_to_date(yourdate, '%d.%m.%y')) MaxDate
from table1
See SQL Fiddle with Demo
Try
select distinct column from tab where column1 = date_format(curdate(),'%d.%m.%y') AND brukernavn is not null order by brukernavn;
1) You need to save DATES in date or datetime
2) try MAX() function in MySQL
or try to ORDER BY date DESC and LIMIT 1
3) by the way, did you try using DISTINCT with GROUP BY?
I had the question for the mysql date between select. In the mysql table, the field is varchar. The date range is '21-01-2013' and '31-01-2013', it can show the records, but the date range is '21-01-2013' and '20-02-2013', it cannot show the records.
"SELECT * from away_from_office where (awaydatefrom between '21-01-2013' and '31-01-2013') ";
you should convert it to date first using STR_TO_DATE, eg.
SELECT *
from away_from_office
where awaydatefrom between STR_TO_DATE('21-01-2013', '%d-%m-%Y') and
STR_TO_DATE('31-01-2013', '%d-%m-%Y')
if the column has the same format with the one you've shown, convert it also
WHERE STR_TO_DATE(awaydatefrom, '%d-%m-%Y') BETWEEN
if you have a chance to alter the table, or you're working with sample records, alter your table by changing the data type of the column to DATE.
Change column datatype to DATE
http://dev.mysql.com/doc/refman/5.1/en/datetime.html