Solved

How to get max date

Posted on 2010-11-11
8
297 Views
Last Modified: 2012-05-10
I have a table which contains the times documents were created. There are two groups of documents, one that is created by our system automatically at 11:23 PM on a daily basis, and documents that were created by users. What I need to find out, is when the last user-created document was entered in the system in a 1 month range. For example... the date column looks like this:

date
2010-11-10 17:10
2010-11-10 17:23
2010-11-10 23:00
2010-11-11 17:24
2010-11-11 17:35
2010-11-11 23:00

My query would return the following results:
2010-11-10 17:23
2010-11-11 17:35

How can this be accomplished?
0
Comment
Question by:horalia
  • 4
  • 2
  • 2
8 Comments
 
LVL 95

Expert Comment

by:Lee W, MVP
ID: 34115265
I would suggest:SELECT TOP 1 DateField FROM YourTable WHERE DateField BETWEEN 'YYYY-MM-DD' AND 'YYYY-MM-DD' ORDER BY DateField DESCreplacing YYYY-MM-DD with the appropriate start and end dates for the date range you want.(I always get ASC and DESC mixed up... if that returns the OLDEST in the range, then use ASC instead of DESC at the end)
0
 

Author Comment

by:horalia
ID: 34115282
This will only return one row... I need the maximum date for a group of dates.....
0
 

Author Comment

by:horalia
ID: 34115304
I have created this query:
select convert(nvarchar(10),sentdatetime,110), max(sentdatetime)
from alg_fax (nolock)
where sentdatetime > '2010-11-01'
group by convert(nvarchar(10),sentdatetime,110)

But I get rows for 11:00 PM. How can I exclude these rows?
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34115307
This is going to be interesting.
What other columns are available on the table? That differentiates between user and system?

date
2010-11-10 17:10
2010-11-10 17:23  <<< why this?
2010-11-10 23:00
2010-11-11 17:24
2010-11-11 17:35  <<< why this?
2010-11-11 23:00
2010-11-11 23:55  <<< what if there is this one?

My query would return the following results:
2010-11-10 17:23
2010-11-11 17:35

My guess is that you want the latest datetime prior to 23:00, PER day?  See 3rd << note above.
0
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 500 total points
ID: 34115313
select convert(nvarchar(10),sentdatetime,110), max(sentdatetime)
from alg_fax (nolock)
where sentdatetime > '2010-11-01' and datepart(hour, sentdatetime) < 23
group by convert(nvarchar(10),sentdatetime,110)
0
 

Author Comment

by:horalia
ID: 34115316
I want the times that show before 23:00. Maybe using DATEPART?
0
 
LVL 95

Expert Comment

by:Lee W, MVP
ID: 34115402
didn't register you were looking for a grouping...

Without knowing field names, my answer could have worked for your question as quoted below.
"What I need to find out, is when the last user-created document was entered in the system in a 1 month range"
0
 

Author Comment

by:horalia
ID: 34115419
I might have chosen the wrong comment, I want to award points and accept cyberkiwi's answer.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 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