+ Reply to Thread
Results 1 to 6 of 6

Thread: Date comparison with C# and SQL Server 2005

  1. #1
    Programmer hoser2001 is an unknown quantity at this point hoser2001's Avatar
    Join Date
    Jul 2006
    Posts
    175

    Date comparison with C# and SQL Server 2005

    I am running a query from some C# code.
    Code:
    select someColumn from someTable
    where 
    CurrentDate-someDate > ElapsedTime
    My problem is I don't know how to create a string in the correct format to include as part of my select statement.

    The format of the date column in sql server is:

    mm/dd/yyyy hh:mm:ss AM/PM

  2. #2
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,556
    Blog Entries
    97
    Which SQL server are you using (Oracle, MS, MySQL - they are all a bit different)? I've done this exact same thing in C# or Managed C++ (can't remember).

  3. #3
    Programmer hoser2001 is an unknown quantity at this point hoser2001's Avatar
    Join Date
    Jul 2006
    Posts
    175
    sorry! i thought i had it on the first post I made, MS SQL Server 2005

  4. #4
    Lop
    Lop is offline
    Speaks fluent binary Lop has a spectacular aura about Lop has a spectacular aura about Lop's Avatar
    Join Date
    May 2006
    Posts
    1,179
    I'm not sure if this helps but this will allow you to convert your MS SQL date/time into a C# object:

    Code:
    DateTime date_dt = DateTime.Parse(date_str);
    MSDN: DateTime Structure (System)

    Ahh, I think I just found your answer. Use function above to convert to an object. Then use the datetime.compare to compare the two:

    Code:
    System.DateTime date_dt = DateTime.Parse(SQL_str);
    System.DateTime dtNow=DateTime.Now;
    DateTime.Compare(dtNow,date_dt);
    If this works, let me know. I'm interested now.
    Lop

  5. #5
    Programmer hoser2001 is an unknown quantity at this point hoser2001's Avatar
    Join Date
    Jul 2006
    Posts
    175
    What I did was use the System.Datetime.Now and made that a string.
    Then I concatenated that string into the select statement i was sending to the server.


    I guess I didnt' realize that Datetime.Now was formatted like that, silly me.

  6. #6
    Programmer hoser2001 is an unknown quantity at this point hoser2001's Avatar
    Join Date
    Jul 2006
    Posts
    175
    After tinkering, I figured out that sql server actually has a built in date difference function, and a built in GetDateTime function... So now I can just have a simple select statement.

    select someColumn from someTable where DATEDIFF(seconds,GETDATE(),someDate) > ElapsedTimeColumn

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Change method to send sql server sproc XML
    By hoser2001 in forum Database & Database Programming
    Replies: 0
    Last Post: 07-17-2007, 09:35 AM
  2. How do I centralize sql server database
    By hoser2001 in forum Database & Database Programming
    Replies: 3
    Last Post: 07-13-2007, 01:07 PM
  3. Accessing Databases without SQL Server or FP Extensions
    By ashleysmithd in forum Database & Database Programming
    Replies: 2
    Last Post: 07-10-2007, 01:47 PM
  4. Database Tuning Advisor in SQL Server 2005
    By reachpradeep in forum Database & Database Programming
    Replies: 0
    Last Post: 03-03-2007, 12:46 PM
  5. Installing SQL server on Windows XP 64 bit
    By roger in forum Database & Database Programming
    Replies: 1
    Last Post: 05-23-2006, 10:13 AM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts