Solved

how to select the greatest date?

Posted on 2001-09-08
7
259 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: 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!

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

707 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