最近做專案中,發現這兩個方法非常不錯,記下來。以備後用
///
/// //按自然月計算應收日期
///
public partial class wytablefunction
//如果有應收日,那就以合同的應收日為準,否剛就為應收日期就為自然月最後一天
///
/// 計算應收日期及所屬期
/// cyblebegindate:週期實際的起始日
/// cybleenddate:週期實際的結束日。
///
/// 週期單位
/// 週期
/// 起始日期
/// 截止日期
/// 應收類別,1代表提前,2代表本期,3代表延後
/// 指定月份,0代表本期
/// 指定應收天數,0代表每期最後一天
///
[sqlfunction(tabledefinition = @" begindate datetime,enddate datetime,gatheringdate datetime,cyblebegindate datetime,cycleenddate datetime", fillrowmethodname = "fillrow2")]
public static ienumerable cw_f_caldatetable_clr(int unittype, int cycle, string sbegindate,
string senddate, int exestype, int exesmonth, int accdate, int ienumperoiod,bool returndatarow)
至",begindate,enddate));
switch (unittype)
else
dtcycleenddate = dtend;
dtcyblebegindate = dtend.adddays(1).addyears(-cycle);//例:2011-12-31,變2012-1-1,再減1年(設cycle為1)
if (dtend > enddate)
dtend = enddate;
accountdate = calaccountdate(dtbegin, dtend, exestype, exesmonth, accdate);
rowlist.add(new datetime );
if (peroid!=enumperoiod.allperoid) break;
dtbegin = dtend.adddays(1);
}break;
#endregion
#region 季交
case 2://季
while (dtbegin <= enddate)
else
}dtcycleenddate = dtend;
dtcyblebegindate = dtend.adddays(1).addmonths(-(cycle * 3));//倒退到起始日。例:結束日期是2011-3-31,則先變為2011-4-1,再減一季(3個月),則變為2011-1-1
if (dtend > enddate)
dtend = enddate;
accountdate = calaccountdate(dtbegin, dtend, exestype, exesmonth, accdate);
rowlist.add(new datetime );
if (peroid != enumperoiod.allperoid) break;
dtbegin = dtend.adddays(1);
}break;
#endregion
#region 月交
case 3://月
while (dtbegin <= enddate)
else
dtcycleenddate = dtend;
dtcyblebegindate = new datetime(dtcycleenddate.year, dtcycleenddate.month, 1);
if (dtend > enddate)
dtend = enddate;
"計算自然月時:起始,結束,週期:", dtbegin,dtend,cycle));
accountdate = calaccountdate(dtbegin, dtend, exestype, exesmonth, accdate);
"計算應收日:exestype=,exesmonth=,accdate=,accountdate=", exestype, exesmonth, accdate, accountdate));
rowlist.add(new datetime );
if (peroid != enumperoiod.allperoid) break;
dtbegin = dtend.adddays(1);
}break;
#endregion
#region 週交
case 4://周
while (dtbegin <= enddate)
else
dtcycleenddate = dtend.adddays(-1);
dtcyblebegindate = dtcycleenddate.adddays(-7 * cycle);
if (dtend > enddate)
dtend = enddate;
else
dtend = dtend.adddays(-1);
accountdate = calaccountdate(dtbegin, dtend, exestype, exesmonth, accdate);
rowlist.add(new datetime );
if (peroid != enumperoiod.allperoid) break;
dtbegin = dtend.adddays(1);
}break;
#endregion
#region 日交
case 5://日
while (dtbegin <= enddate)
);if (peroid != enumperoiod.allperoid) break;
dtbegin = dtend.adddays(1);
}break;
#endregion
#region 其他
case 6:
);if (peroid != enumperoiod.allperoid) break;
}break;
#endregion
}datatable dt=new datatable();
dt.columns.add("begindate",typeof(datetime));
dt.columns.add("enddate",typeof(datetime));
dt.columns.add("gatheringdate",typeof(datetime));
dt.columns.add("cyclebegindate", typeof(datetime));
dt.columns.add("cycleenddate", typeof(datetime));
for (int i = 0; i < rowlist.count; i++)
if (returndatarow==true)
return dt.rows as ienumerable;
else
return rowlist as ienumerable;
//返回乙個string 陣列,這個陣列符合ienumerable介面,當然你也可以返回hashtable等型別。
//return (ienumerable)dt.rows.getenumerator();
}private static datetime calaccountdate(datetime dtbegin,datetime dtend, int delaytype,int delaymonth, int delayday)
,,", delaytype, delaymonth, delayday));
switch (delaytype)
datetime dtmonthlast=new datetime(dtaccount.year, dtaccount.month, 1).addmonths(1).adddays(-1);
int monthdays = dtmonthlast.day;//應收所在月份的天數。
if (delayday <= 0 || delayday > monthdays)
dtaccount = dtmonthlast;
else
dtaccount = new datetime(dtaccount.year, dtaccount.month, delayday);
return dtaccount;
}}
測試
select * from cw_f_caldatetable_clr(3,1,'2013-01-15','2014-01-14',3,1,15,2,0)
執行結果
[img]
時間段函式
在sql server中,操作select查詢時,將時間型別的字段作為搜尋條件,如果稍微不注意,也許沒有辦法查詢到意想的結果。條件不能用等於,而是時間段。如某一天,應該是從00 00 00至23 59 59之間,方可查詢出結果出來。因此,為了開發方便,寫了乙個函式 setansi nulls ong...
根據時間段,返回時間段的工作小時數
根據時間段,返回時間段的工作小時數 一天按8小時 create function dbo fc getworkhour startdate alldatetime,enddate alldatetime returns int as begin declare delayday int 暫用工時 日...
分時間段查詢
declare t table 時間 datetime,金額 int insert t select 2007 1 1 10 00 23 8 union all select 2007 1 1 01 00 04 4 union all select 2007 1 1 01 00 14 4 union...