• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 265
  • Last Modified:

how to select the greatest date?

I am retrieving a list of dates from the mysql database and from that i want to select the greatest date from them all.. I used sorting method which runs in for loop(by comparision).. But the response time is very slow when the record count reaches more..
help me in finding the solution..
Regards,
Sujatha N.
0
sujatharathinam
Asked:
sujatharathinam
1 Solution
 
ramsesCommented:
Please note that I can change this code to support other types of dates.  Currently, it excepts dates in order of mm/dd/yyyy

If you need to compare dates that are explicitly declared as date (var cTime=new Date()) , please let me know.


<HTML>
<HEAD><TITLE>Date chooser</TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!--
function bigger()
{var x1=document.all.xdat1.value;
 var x2=document.all.xdat2.value;
 var x3=biggest(x1,x2);
 document.all.xbig.value=x3}

function biggest(date1, date2)
{
 //compare dates
 //dates must be in format mm/dd/yyyy
 //this can be changed very easaly dough
 var m1=date1.substring(0,2);
 var m2=date2.substring(0,2);
 var d1=date1.substring(3,2);
 var d2=date2.substring(3,2);
 var y1=date1.substring(7);
 var y2=date2.substring(7);
 //now the comparisation can start
 if( y1 > y2) return date1;
 if( y2 > y1) return date2;
 //when years are the same, go to the months
 if( m1 > m2) return date1;
 if( m2 > m1) return date2;
 //years and months are the same, go to the days
 if( d1 > d2) return date1;
 if( d2 > d1) return date2;
 //both dates are the same, so we return -1
 return -1}
//-->
</SCRIPT>
</HEAD>
<BODY>
Date 1 <INPUT TYPE="TEXT" ID="xdat1" SIZE="10" MAXLENGTH="10"><br>
Date 2 <INPUT TYPE="TEXT" ID="xdat2" SIZE="10" MAXLENGTH="10">&nbsp;
<BUTTON ONCLICK="bigger()">Show me</BUTTON><br>
Biggest <INPUT TYPE="TEXT" ID="xbig" SIZE="10" MAXLENGTH="10" READONLY">
</BODY>
</HTML>

Ramses
0
 
CJ_SCommented:
Use the ORDER BY clause in the sql statement

example:
SELECT * FROM MYTABLE ORDER BY datefieldname DESC

Regards,
CJ
0
 
ramsesCommented:
Is this a JS question or SQL question?


Ramses
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
snakehollywoodCommented:
if the only thing you want is the top date then use

select top1 from table
order by datefield desc
0
 
pak_slmCommented:
Use select statement as

select max(date) from table;
0
 
Michel PlungjanIT ExpertCommented:
Pak_Sim: Please read the bottom of this page and post comments in questions that already have comments.

Michel
0
 
sujatharathinamAuthor Commented:
Thanx for u suggestion.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now