Solved

SQL and date fields

Posted on 1997-05-27
5
141 Views
Last Modified: 2010-04-04
How do I return records from table with dates less than
or greater than mm/dd/yy
the min(dd/mm/yy) or max(dd/mm/yy)
don't seem to work

thanks
0
Comment
Question by:John Culkin
5 Comments
 
LVL 2

Expert Comment

by:javiertb
ID: 1336619
You say that this code doesn't work?
Select * from table1 where table1.date>'dd/mm/yy'

The problem might be in the date format you have in your system.
You can use FormatDateTime function or you should check your
win.ini file and look for the [intl] section where the variable sShortDate must be the date format you need.

Regards,
Javi
0
 

Author Comment

by:John Culkin
ID: 1336620
I don't want to find all dates greater than a certain date
I need to find the maximum date, ie the last date in a field.
by the way my sshortdate is ok.

many thanks

john
ps last(dd/mm/yy) doesnt seem to work either
0
 
LVL 2

Accepted Solution

by:
vorlon earned 70 total points
ID: 1336621
John,
The reason why your min/max statements aren't working is because you're passing them a string of the format mm/dd/yy. If the tables contain date/time fields then the internal format is not stored as mm/dd/yy by as xx.yy where xx is the date and yy is the time. You must convert your string to that format first. Here is some sample code:

procedure TForm1.Button1Click(Sender: TObject);
var
  dt:string;
begin
   query1.close;
   query1.sql.clear;
   Str(StrToDate('05/29/97'),dt);
   dt:='select * from maxdate where datevalue > ' + dt + 'order  datevalue desc'
   query1.sql.add(dt);
   query1.open;
end;{button1click}

I've created a table with 1 field (datevalue) and placed some dates into it. Obviously the table name is maxdate. To use the max and min function, I placed the order by clause. All you have to do now is get the first record and that will be your max function. To use the min function, use the order by clause without the "desc" keyword and then get the first record as well.

Hope this helps.
0
 

Expert Comment

by:vyga
ID: 1336622
SQL's like:select max(tdate) from timesliporselect min(tdate) from timeslipor select min(tdate) from timeslipwhere tdate>'mm/dd/yy'really work. All you need is to construct SQL you need runtime.Let's say this way:QueryTimeslip.SQL.Clear;QueryTimeslip.SQL.Add('select min(tdate) from timeslip');QueryTimeslip.SQL.Add('where tdate>'+DateToStr(someDate));QueryTimeslip.Open;or you may use FormatDateTime('mm/dd/yy',someDate) instead of DateToStr(). This function is safer. Using DateToStr() you must sure that global variables shortDateFormat and DateSeparator are set to correct values.vyga
0
 

Author Comment

by:John Culkin
ID: 1336623
sorry about the delay Ive been on holidayThanksJohn
0

Featured Post

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.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now