Hi can any one help me!!!
I need to display the latest entries by date and time ie i stored the date as 2009-Apr-16. so that i wrote a query as

select * from tbl_projectstatus order by date asc ,time  desc"

But the above code displays as follows ie Normaly i want to display as april first and then mar and then feb . But it displays april first and then feb and then march

2009-Apr-16         12:33:11            Hai i am good here
2009-Apr-16         12:32:10            Hai i am good here
2009-Apr-16         12:31:22            kjjkkj
2009-Feb-3          14:52:01            Good keep it Up
2009-Feb-3          14:37:12            How i s this
2009-Mar-18         10:41:52            I  the xxgyg project as on 
                                                        26.34.3455.    
2009-Mar-19         17:03:10            Hi iam prawin
2009-Mar-19         16:48:50            prawin kumar

Dani AI

Generated

is correct: the behavior shows the date column is being sorted as text, not as a true date. Storing month names (alphabetic tokens) forces lexicographic ordering so "Apr" will come before "Feb" and "Mar" regardless of calendar order. For reliable “latest first” results the values need to be interpreted as actual dates/times, not plain strings.

A quick, non-invasive fix is to convert the textual date/time on the fly in the ORDER BY using MySQL’s STR_TO_DATE(). When the stored format uses an abbreviated month and possibly non-zero-padded days, the format string should use %Y-%b-%e for the date and %H:%i:%s for time. Example:

SELECT *
FROM tbl_projectstatus
ORDER BY STR_TO_DATE(CONCAT(date_col, ' ', time_col), '%Y-%b-%e %H:%i:%s') DESC;

This avoids changing the schema but is slower on large tables because conversion runs per row.

A safer, long-term solution is to convert to a DATE or DATETIME column and order by that native type. Typical workflow:

ALTER TABLE tbl_projectstatus ADD COLUMN status_dt DATETIME NULL;

UPDATE tbl_projectstatus
SET status_dt = STR_TO_DATE(CONCAT(date_col, ' ', time_col), '%Y-%b-%e %H:%i:%s');

-- verify conversions, then:
-- use ORDER BY status_dt DESC in queries

Use %b for abbreviated month names and %M if full month names were used; use %e if days are not zero-padded (single-digit days). Check for bad rows before dropping old columns:

SELECT * FROM tbl_projectstatus
WHERE STR_TO_DATE(CONCAT(date_col,' ',time_col), '%Y-%b-%e %H:%i:%s') IS NULL;

Backup the table before schema changes and test the conversion on a copy. MySQL docs for STR_TO_DATE and date/time column types provide format details: see the STR_TO_DATE reference and Date and Time Types in the MySQL manual.

Hi can any one help me!!!
I need to display the latest entries by date and time ie i stored the date as 2009-Apr-16. so that i wrote a query as

But the above code displays as follows ie Normaly i want to display as april first and then mar and then feb . But it displays april first and then feb and then march

2009-Apr-16         12:33:11            Hai i am good here
2009-Apr-16         12:32:10            Hai i am good here
2009-Apr-16         12:31:22            kjjkkj
2009-Feb-3          14:52:01            Good keep it Up
2009-Feb-3          14:37:12            How i s this
2009-Mar-18         10:41:52            I  the xxgyg project as on 
                                                        26.34.3455.    
2009-Mar-19         17:03:10            Hi iam prawin
2009-Mar-19         16:48:50            prawin kumar

end quote.

You are sorting the date alphabetically. Is it possible your field is TEXT, and not DATE?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.