0

我在 table 中有一个passed_date列,我想找到这个日期和当前日期之间的差异,并且必须检查这个差异是否>365days?例如,“passed_date”列由 09/26/2013 09:16:48.000 PM 组成。我怎样才能找到日期之间的差异?

sqlcommand cmd = new sqlcommand("select passed_date from table");
cmd.executereader();
dr.read();
sring date1 = stdr[0].tostring();
string date2 = DateTime.Now.ToString();
4

4 回答 4

2

这使用索引:

Select passed_date from table where passed_date > Dateadd(day,-365,getdate())
于 2013-11-01T07:57:45.157 回答
1

try this ...

declare @startDate datetime
set @startdate = DATEADD(day, -365, GETDATE())
-- or set @startDate = DATEADD(year, -1, GETDATE())
select passed_date, datediff(day, passed_date, getdate()) from table where passed_date > @startDate

... if you want a solution in t-sql

于 2013-11-01T08:04:46.530 回答
1

这有效:

select passed_date from table where DateDiff(day,passed_date,GetDate()) > 365
于 2013-11-01T07:47:41.330 回答
0

试试这个代码: -

SqlCommand sqlcmd = new SqlCommand("select passed_date from table", con);
var date = sqlcmd.ExecuteScalar();
DateTime startDate = (DateTime)date;
DateTime endDate = DateTime.Now;
TimeSpan span = endDate.Subtract(startDate);

double days = span.TotalDays;
if (365 < days)
{
    ********your logic ************************     
}
于 2013-11-01T08:29:06.750 回答