Solved

how to select the greatest date?

Posted on 2001-09-08
7
253 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
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…

758 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

18 Experts available now in Live!

Get 1:1 Help Now