Closed Thread
Results 1 to 6 of 6

Thread: Date comparison with C# and SQL Server 2005

  1. #1
    hoser2001's Avatar
    hoser2001 is offline Programmer
    Join Date
    Jul 2006
    Posts
    175
    Rep Power
    0

    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. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Jordan Guest
    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).

  4. #3
    hoser2001's Avatar
    hoser2001 is offline Programmer
    Join Date
    Jul 2006
    Posts
    175
    Rep Power
    0
    sorry! i thought i had it on the first post I made, MS SQL Server 2005

  5. #4
    Lop's Avatar
    Lop
    Lop is offline Speaks fluent binary
    Join Date
    May 2006
    Posts
    1,178
    Rep Power
    30
    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.

  6. #5
    hoser2001's Avatar
    hoser2001 is offline Programmer
    Join Date
    Jul 2006
    Posts
    175
    Rep Power
    0
    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.

  7. #6
    hoser2001's Avatar
    hoser2001 is offline Programmer
    Join Date
    Jul 2006
    Posts
    175
    Rep Power
    0
    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

Closed Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Does SQL Server 2005 SP2 Contains SP1 updates ?
    By rakz in forum Database & Database Programming
    Replies: 3
    Last Post: 11-17-2010, 01:44 AM
  2. SQL Server 2005 Express: Cannot connect to Server Name
    By newphpcoder in forum Database & Database Programming
    Replies: 4
    Last Post: 09-28-2010, 08:59 PM
  3. Comparison of Java and C++ for server-based application
    By penkomitev in forum General Programming
    Replies: 38
    Last Post: 10-15-2009, 04:55 AM
  4. How to connect vb 2005 with SQL Server 2005
    By Ubuntu_monster in forum Visual Basic Programming
    Replies: 6
    Last Post: 10-13-2008, 10:36 AM
  5. MSSQL Server 2005
    By innoinco in forum General Programming
    Replies: 2
    Last Post: 05-15-2007, 09:10 AM

Tags for this Thread

Bookmarks

Posting Permissions

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