We help IT Professionals succeed at work.

How to compare dates in C# /sql server.

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";
Comment
Watch Question

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


msdn: http://msdn.microsoft.com/en-us/library/system.datetime.aspx
Kiran SonawaneProject Lead
CERTIFIED EXPERT
Top Expert 2011

Commented:
CERTIFIED EXPERT
Most Valuable Expert 2015
Distinguished Expert 2018

Commented:
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

Author

Commented:
Thnx
CERTIFIED EXPERT
Most Valuable Expert 2015
Distinguished Expert 2018

Commented:
??
There was no ElapsedTimeColumn field but a start and an end date.

/gustav

Explore More ContentExplore courses, solutions, and other research materials related to this topic.