FDate为日期字段,在MSSQL中根据FDate过滤数据,在ORACLE中如何写
DateDiff(dd,FDate,GetDate())=0 //今天
DateDiff(dd,FDate,GetDate())=1 //昨天
DateDiff(week,FDate-1,GetDate())=0 //本周
DateDiff(week,FDate-1,GetDate())=1 //上周
DateDiff(month,FDate,GetDate())= 0 //本月
DateDiff(month,FDate,GetDate())= 1 //上月
year(FDate) = year(GetDate()) //今年
year(FDate) = year(GetDate())-1 //去年
DateDiff(dd,FDate,GetDate())=0 //今天
FDate - sysdate = 0
DateDiff(dd,FDate,GetDate())=1 //昨天
sysdate - FDate = 1
DateDiff(week,FDate-1,GetDate())=0 //本周
to_char(sysdate,'iw') - to_char(fdate,'iw')= 0
DateDiff(week,FDate-1,GetDate())=1 //上周
to_char(sysdate,'iw') - to_char(fdate,'iw')= 1
DateDiff(month,FDate,GetDate())= 0 //本月
to_char(sysdate,'yyyymm') -to_char(FDate,'yyyymm') = 0
DateDiff(month,FDate,GetDate())= 1 //上月
to_char(sysdate,'yyyymm') - to_char(FDate,'yyyymm')= 1
year(FDate) = year(GetDate()) //今年
to_char(sysdate,'yyyy') -to_char(FDate,'yyyy') = 0
year(FDate) = year(GetDate())-1 //去年
to_char(sysdate,'yyyy') -to_char(FDate,'yyyy') = 1
1楼的上月的语句稍有问题
如果FDate为2007年12月的某天,而sysdate为2008年的元月份
那么结果就是
200801-200712=89,显然不等于1
利用oracle的两个时间相减得到的是一个以天为单位的数
则上月的最后一天减去本月的第一天肯定是1
上月的最后一天是:last_day(FDate)
本月的第一天是: to_date(to_char(sysdate,'yyyymm'),'yyyymm')
那么判断是否上月的语句就是:
selectabs(trunc(last_day(FDate))-to_date(to_char(sysdate,'yyyymm'),'yyyymm') fromdual;
结果为1则是上月,否则不是,trunc(last_day(FDate))加trunc是取整到天
2楼的学习了,呵,不过最后的sql语句,末尾少了个)号
1楼本周/上周,本月/上月的语句存在问题,跨年度的情况没有考虑
DateDiff(dd,FDate,GetDate())=0 ?nbsp;?nbsp; ?nbsp; ?nbsp; ?//今天
trunc(fdate) = trunc(sysdate)
DateDiff(dd,FDate,GetDate())=1 ?nbsp;?nbsp; ?nbsp; ?nbsp; ?//昨天
trunc(fdate) = trunc(sysdate) -1
DateDiff(week,FDate-1,GetDate())=0 ?//本周
to_char(fdate, 'iw') =to_char(sysdate, 'iw') and (sysdate - fdate) <7
DateDiff(week,FDate-1,GetDate())=1 ?//上周
to_char(fdate, 'iw') =to_char(sysdate-7, 'iw') and (sysdate -7 - fdate) < 7
DateDiff(month,FDate,GetDate())= ?0 ?//本月
to_char(fdate, 'yyyymm') = to_char(sysdate, 'yyyymm')
DateDiff(month,FDate,GetDate())= ?1 ?//上月
to_char(fdate, 'yyyymm') = to_char(add_months(sysdate, -1), 'yyyymm')
year(FDate) = year(GetDate()) //今年
to_char(fdate, 'yyyy') = to_char(sysdate, 'yyyy')
year(FDate) = year(GetDate())-1 //去年
to_number(to_char(fdate, 'yyyy')) = to_number(to_char(sysdate, 'yyyy'))-1