Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

SQL query - latest date

Posted on 2006-04-06
3
Medium Priority
?
1,089 Views
Last Modified: 2008-02-01
How is the SQL query for retrieving the recordset with the latest date (in the field myDate)?

The following 2 SQL queries don't work like I want them to work:

SELECT * FROM myTable WHERE myDate >= ALL (SELECT myDate FROM myTable);

SELECT * FROM myTable WHERE myDate = (SELECT MAX(myDate) FROM myTable);

Assuming there are the following dates in the table:
20.12.2005
31.12.2005
05.02.2006
06.04.2006
both queries return 31.12.2005 but they should return 06.04.2006
0
Comment
Question by:TheFunkSoulBrother
  • 2
3 Comments
 
LVL 54

Accepted Solution

by:
nico5038 earned 400 total points
ID: 16396142
That's because they aren't seen as dates because of the dots and it's probably defines as a text field.
Try:
SELECT * FROM myTable WHERE dateserial(right(myDate,4),mid(myDate,4,2),left(myDate,2)) = (SELECT MAX(dateserial(right(myDate,4),mid(myDate,4,2),left(myDate,2))) FROM myTable);

Nic;o)
0
 
LVL 1

Author Comment

by:TheFunkSoulBrother
ID: 16396611
Indeed, it's defined as a text field. Your query works!! Thanks a lot!
I thought it was defined as a date field.
Access displays dates differently depending on the country setting - in my country dates are displayed in the format dd.mm.yyyy by default.
Therefore I thought it was defined as a date field.

If it would have been a date field my queries would have worked, right?
0
 
LVL 54

Expert Comment

by:nico5038
ID: 16398122
Yes :-)

Nic;o)
0

Featured Post

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!

Question has a verified solution.

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

I have had my own IT business for a very long time. I started mostly with hardware and after about a year started to notice a common theme. I had shelves with software boxes -- Peachtree, Quicken, Sage, Ouickbooks -- and yet most of my clients were…
If you’re using QODBC to update QuickBooks data from Microsoft® Access but Access is not showing the updated data, you could have set up QODBC incorrectly.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
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…

580 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