Oracle:获取当前日期的上个月的第一天和最后一天(to_char、trunc、add_months、last_day)
select to_char(trunc(add_months(sysdate,-1),‘mm’),‘yyyymmdd’) first_day,to_char(last_day(add_months(sysdate,-1)),‘yyyymmdd’) last_day from dual;
oracle不区分大小写,MM和mm效果一样。
一.to_char()函数
to_char()函数:可以将日期按照一定格式转换为字符串类型
与to_char()函数相反的则是to_date()函数,可以将字符串类型转换为日期类型。
select to_char(sysdate,‘yyyy’) as nowYear from dual; --获取时间的年 2016
select to_char(sysdate,‘mm’) as nowMonth from dual; --获取时间的月 09
select to_char(sysdate,‘dd’) as nowDay from dual; --获取时间的日 07
select to_char(sysdate,‘hh24’) as nowHour from dual; --获取时间的时 10
select to_char(sysdate,‘mi’) as nowMinute from dual; --获取时间的分 33
select to_char(sysdate,‘ss’) as nowSecond from dual; --获取时间的秒 11
select to_char(sysdate,‘day’) as nowDay from dual; --获取当天是星期几 星期三
select to_char(sysdate,‘D’) as nowDay from dual; --获取当天是星期几 4
select floor(sysdate - to_date(‘2016-08-05’,‘yyyy-mm-dd’)) from dual; --取两个日期间的天数 33
二.trunc()函数
trunc()函数两种用法,截断数字或者是截断日期。
1.截断数字:格式:TRUNC(n1,n2),n1表示被截断的数字,n2表示要截断到那一位。n2可以是负数,表示截断小数点前。注意,TRUNC截断不是四舍五入。
2.截断日期:
截取今天:select sysdate,trunc(sysdate,‘dd’) from dual
截取本周第一天:select sysdate,trunc(sysdate,‘d’) from dual
截取本月第一天:select sysdate,trunc(sysdate,‘mm’) from dual
截取本年第一天:select sysdate,trunc(sysdate,‘y’) from dual
三.add_months()函数
add_month(n1,n2),n1表示日期格式数据,n2表示对日期加减的数字(以月为单位)
获取上个月的今天:select add_months(sysdate,-1) from dual
四.last_day()函数
last_day()函数返回指定日期对应月份的最后一天
获取当前日期的最后一天:SELECT last_day(SYSDATE) FROM dual
sql update 多表关联更新方法总结
SQLServer多表更新方法:
update test1
set test1.name=test2.name,test1.age=test2.age
from test1
inner join test2
on test1.id=test2.id
Oracle 多表更新方法:
update test1
set (test1.name,test1.age)=
(select test2.name,test2.age from test2 where test2.id=test1.id)
MySql多表更新方法
update test1,test2
set test1.name=test2.name,test1.age=test2.age
where test1.id=test2.id
通用方法
update test1
set name=(select name from test2 where test2.id=test1.id),
age=(select age from test2 where test2.id=test1.id)
oracle判断存储过程是否执行完成
select name from v$db_object_cache where locks > 0 and pins > 0 and type=‘PROCEDURE’;
explain sql 查看 type列是否为All 判断是否走索引
索引失效
1.查询条件中带有or ,除非所有条件都建有索引,否则索引失效
2.like 查询是以% 开头
3.如果列类型是字符串,那在查询条件中需要将数据用引号引用起来,否则不走索引
4.索引列上参与计算会导致索引失效
5.组合索引,查询条件必须需要都有
6.如果mysql估计全表扫描比使用索引扫描要快,会不适用索引