decode(条件,值1,翻译值1,值2,翻译值2,...值n,翻译值n,缺省值)
if (条件==值1)
then
return(翻译值1)
elsif (条件==值2)
then
return(翻译值2)
......
elsif (条件==值n)
then
return(翻译值n)
else
return(缺省值)
end if
trunc()函数的用法
/**************日期********************/
1.select trunc(sysdate) from dual --2013-01-06 今天的日期为2013-01-06
2.select trunc(sysdate, 'mm') from dual --2013-01-01 返回当月第一天.
3.select trunc(sysdate,'yy') from dual --2013-01-01 返回当年第一天
4.select trunc(sysdate,'dd') from dual --2013-01-06 返回当前年月日
5.select trunc(sysdate,'yyyy') from dual --2013-01-01 返回当年第一天
6.select trunc(sysdate,'d') from dual --2013-01-06 (星期天)返回当前星期的第一天
7.select trunc(sysdate, 'hh') from dual --2013-01-06 17:00:00 当前时间为17:35
8.select trunc(sysdate, 'mi') from dual --2013-01-06 17:35:00 TRUNC()函数没有秒的精确
一般用作条件判断 例子: where xx>trunc(sysdate,'dd') 大于今天 (就是今天00.00之后开始产生的数据)
to_date('..','..')
select to_date('2020-10-23 ','yyyy-MM-dd') from dual; //就是将对应的格式转换成date格式2020/10/23
输出:2020/10/23
to_char(date,'格式'); //一般用作日期转换
select to_char(sysdate,'yyyy-MM-dd HH24:mi:ss') from dual; //将sysdate按照指定格式输出
输出:2020-10-23 09:20:30
修改日期格式为年月日时分秒: alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss';
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') as nowTime from dual; //日期转化为年月日时分秒格式
select to_char(sysdate,'yyyy') nowYear from dual; //获取时间的年
select to_char(sysdate,'mm') nowMonth from dual; //获取时间的月
select to_char(sysdate,'dd') nowDay from dual; //获取时间的日
select to_char(sysdate,'hh24') nowHour from dual; //获取时间的时
select to_char(sysdate,'mi') nowMinute from dual; //获取时间的分
select to_char(sysdate,'ss') nowSecond from dual; //获取时间的秒
NVL(aab001,值X)
判断是否为空并对其赋值,如果aab001为空则取 值X,如果不为空则取自己aab001
case when then else end
常见例子1:
SELECT
sname,
CASE
WHEN sex = '1' THEN '男'
WHEN sex = '2' THEN '女'
ELSE '其他'
END as sex
FROM
student //case 后面接属性sex : CASE sex WHEN sex =... 和不接是一样的 该函数一般用来做条件比较取对应合适的值
常见例子2: SELECT a,
SUM(CASE WHEN lx= '付款成功' THEN '1' ELSE 0 END) AS '收款个数'
FROM
p //case和sum组合使用 ,case将类型分别赋值数字,sum对其进行求和,最后可以得到对应付款成功的个数
upper() 和 lower()
upper()将小写的全置大写输出;lower()将大写的全置小写输出;
select upper('abcABC我') from dual; //输出‘’ABCABC我‘’
select lower('abcABC我') from dual ;//输出‘’abcabc我‘’
instr() 字符查找函数
格式一:instr( string1, string2 ) // instr(源字符串, 目标字符串)
格式二:instr( string1, string2 [, start_position [, nth_appearance ] ] ) // instr(源字符串, 目标字符串, 起始位置, 匹配序号)
解析:string2 的值要在string1中查找,是从start_position给出的数值(即:位置)开始在string1检索,检索第nth_appearance(几)次出现string2。
例子:
select instr('helloworld','l') from dual; --返回结果:3 默认第一次出现“l”的位置
select instr('helloworld','l',2,2) from dual; --返回结果:4 也就是说:在"helloworld"的第2(e)号位置开始,查找第二次出现的“l”的位置
select instr('helloworld','l',-2,2) from dual; --返回结果:4 也就是说:在"helloworld"的倒数第2(l)号位置开始,往回查找第二次出现的“l”的位置