954,568 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

how to find max date through query

i have a date column where dates are saved in varchar format like this
17-03-2011 :: 11:19:48

i want to find last date through query..

please help

thanks

sam230
Light Poster
25 posts since Jul 2011
Reputation Points: 10
Solved Threads: 2
 

Convert your varchar to a date and use ORDER BY DESC, or GROUP BY and MAX()

pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

sorry i cant change the database. is there anyway to explode date format in mysql.

sam230
Light Poster
25 posts since Jul 2011
Reputation Points: 10
Solved Threads: 2
 

You can do it in a query, I did not mean for you to change the database. See the manual entry on string functions . You can use SUBSTR() together with CAST() .

pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

Cast() function..???? :O

didnt get any such function

sam230
Light Poster
25 posts since Jul 2011
Reputation Points: 10
Solved Threads: 2
 

It's a mysql function, you can use it in your query.

pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 
SELECT `field1`, `field2`, STR_TO_DATE(`datefield`,'%d-%m-%Y :: %h:%i:%s') AS mydate FROM `table1` ORDER BY mydate;


Don't know if that will work. Can you order on the calculated field?? Sorry brain frazzled.

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

I think you need to do:

SELECT STR_TO_DATE(`datefield`, '%d-%m-%Y :: %h:%i:%s') AS mydate
FROM `table1` 
ORDER BY STR_TO_DATE(`datefield`, '%d-%m-%Y :: %h:%i:%s') DESC LIMIT 1
pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: