Solved

How to compare dates in C# /sql server.

Posted on 2012-03-11
5
466 Views
Last Modified: 2012-03-12
I have date at back end in 3/1/2012 12:00:00 AM
And from front end the date of entry is: 1-March-2012

string s = "select * from mstbt where feeddate>='" + Convert.ToDateTime(txtfdate.Text.Trim()) + "' and feeddate<='" + Convert.ToDateTime(txttdate.Text.Trim()) + "' order by 1 desc,2 desc,3 asc";
0
Comment
Question by:searchsanjaysharma
[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
5 Comments
 
LVL 9

Accepted Solution

by:
suvmitra earned 500 total points
ID: 37708509
select someColumn from someTable where DATEDIFF(seconds,GETDATE(),someDate) > ElapsedTimeColumn


msdn: http://msdn.microsoft.com/en-us/library/system.datetime.aspx
0
 
LVL 51

Expert Comment

by:Gustav Brock
ID: 37708627
To play safe, you should convert to date, then format this to the correct string format for SQL Server:
string sqlfdate = "\'" + DateTime.Parse(txtfdate.Text.Trim()).ToString("yyyy-MM-dd") + "\'";
string sqltdate = "\'" + DateTime.Parse(txttdate.Text.Trim()).ToString("yyyy-MM-dd") + "\'";
s = "select * from mstbt where feeddate between " + sqlfdate + " and " + sqltdate + " order by 1 desc,2 desc,3 asc";

Open in new window

/gustav
0
 

Author Closing Comment

by:searchsanjaysharma
ID: 37713109
Thnx
0
 
LVL 51

Expert Comment

by:Gustav Brock
ID: 37713337
??
There was no ElapsedTimeColumn field but a start and an end date.

/gustav
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
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…

628 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