Solved

how to select the greatest date?

Posted on 2001-09-08
7
254 Views
Last Modified: 2012-05-04
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
Comment
Question by:sujatharathinam
7 Comments
 
LVL 2

Expert Comment

by:ramses
ID: 6466511
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
 
LVL 22

Expert Comment

by:CJ_S
ID: 6466653
Use the ORDER BY clause in the sql statement

example:
SELECT * FROM MYTABLE ORDER BY datefieldname DESC

Regards,
CJ
0
 
LVL 2

Expert Comment

by:ramses
ID: 6466657
Is this a JS question or SQL question?


Ramses
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Expert Comment

by:snakehollywood
ID: 6466750
if the only thing you want is the top date then use

select top1 from table
order by datefield desc
0
 
LVL 1

Accepted Solution

by:
pak_slm earned 50 total points
ID: 6468117
Use select statement as

select max(date) from table;
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 6468271
Pak_Sim: Please read the bottom of this page and post comments in questions that already have comments.

Michel
0
 

Author Comment

by:sujatharathinam
ID: 6470602
Thanx for u suggestion.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now