Visual Basic .NET » Database Programming
How do I compare dates in ms sql tables in C#? -- srussell --



Using ms sql 2000 and VS2003

I need a little addition to my application that already gets a bunch of
records from a database.
Each row has two dates from two different tables, and an ID column so
the return set looks like this:

ID DateLastModified DateLastSent
1 12/12/05 time 12/15/05
2 9/8/06 9/1/06

What I need to do is compare the two dates in any row and be able to
have an event fire if and only if the date in the DateLastSent column
is smaller (occured before) the date in DateLastModified. I think I
could get as far as getting the two dates but I have no idea how to
compare the datetimes in C#. My other problem is that I'm not sure how
I would be able to check every row and select the ID of those that meet
my condition.

Why do it in C#, when you can do it in your database instead? 


convert( varchar(10), YourDate, 101 ) as YourDate,

-- BradleyPeter --

Well, as I often do, I could be missing something, but I don't see where the comparison is in that solution. I agree with doing the job in SQL though; preferably in a stored procedure.

What I was going to suggest was to create an extra column via the sql query along the lines of:

SELECT ID,
DATE_LAST_MODIFIED,
DATE_LAST_SENT,
CASE WHEN DATE_LAST_SENT > DATE_LAST_MODIFIED
THEN 1
ELSE 0
END AS IS_GREATER FROM MY TABLE etc

There's probably a need for more info on what event is supposed to be fired, though; and when and how.

My apologies, Steve, if I've got the wrong end of the stick. Again.

:)
Peter

-----Original Message-----
From: DotNetDevelopment on behalf of srussell Sent: Mon 7/24/2006 8:29 PM To: DotNetDevelopment Cc:
Subject:
Re: How do I compare dates in ms sql tables in C#?

Using ms sql 2000 and VS2003

I need a little addition to my application that already gets a bunch of
records from a database.
Each row has two dates from two different tables, and an ID column so
the return set looks like this:

ID DateLastModified DateLastSent
1 12/12/05 time 12/15/05
2 9/8/06 9/1/06

What I need to do is compare the two dates in any row and be able to
have an event fire if and only if the date in the DateLastSent column
is smaller (occured before) the date in DateLastModified. I think I
could get as far as getting the two dates but I have no idea how to
compare the datetimes in C#. My other problem is that I'm not sure how
I would be able to check every row and select the ID of those that meet
my condition.

Why do it in C#, when you can do it in your database instead? 


convert( varchar(10), YourDate, 101 ) as YourDate,



[Submit Comment]Home