Solved

How do I get latest(max) date from the field?

Posted on 2011-02-17
7
886 Views
Last Modified: 2012-05-11
I need to getLatestDate from certain field with some records.
Using C#, How do I compare all those dates in the records?
Let's say I have this table:

Test1: 5/20/2010
Test2: 8/12/2010
Test3: 2/1/2011
Test4: 11/2/2010

I want the code to return 2/1/2011 from Test3

Thanks,

Danny
0
Comment
Question by:luisr69
[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
  • 3
  • 2
  • 2
7 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 34922936
Are you talking about a database field, or a class field?  What version of .NET are you using?  Do you have any knowledge about LINQ?
0
 
LVL 23

Expert Comment

by:wdosanjos
ID: 34922947
Where are the dates stored?
0
 

Author Comment

by:luisr69
ID: 34922977
Hi,

It's a database field, .NET 3.5
I'm using SQL Statement with Store Procedure.
Thanks, sorry I'm new with ASP.NET, please bear with me.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 23

Expert Comment

by:wdosanjos
ID: 34922984
You can get the correct value from a SQL statement like this:

select max(YourDateColumn) from YourTable

I hope this helps.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 34926621
If you have a fixed number of fields, then you might be able to use a CASE statement in the query, but MAX only returns the maximum for a single column, not the max between different fields in the same row.
0
 

Author Comment

by:luisr69
ID: 34927596
SELECT MAX works as an SQL statement. Thanks,
How do I start with the C# Code?
And yes, it's a single column only for now.
Thank you.
0
 
LVL 23

Accepted Solution

by:
wdosanjos earned 500 total points
ID: 34927850
You can try something like this.  The following sample connects to a local SQL Server Express instance (line with "new SqlConnection").

using System;
using System.Data;
using System.Data.SqlClient;

void Main()
{
	using (var cn = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=Test;Integrated Security=True;MultipleActiveResultSets=True"))
	{
		cn.Open();
		
		try
		{
			var cmd = new SqlCommand();
			
			cmd.CommandText = "select max(YourDateColumn) as maxdate from YourTable";
			cmd.CommandType = CommandType.Text;
			
			cmd.Connection = cn;
			
			var maxdate = cmd.ExecuteScalar();
			
			Console.WriteLine(maxdate);
		}
		finally
		{
			cn.Close();
		}
	}
}

Open in new window

0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

624 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