Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to compare dates in C# /sql server.

Posted on 2012-03-11
5
Medium Priority
?
481 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
5 Comments
 
LVL 9

Accepted Solution

by:
suvmitra earned 2000 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 52

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 52

Expert Comment

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

/gustav
0

Featured Post

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Integration Management Part 2
Screencast - Getting to Know the Pipeline

782 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