当前位置:网站首页>Oracle-单行函数大全
Oracle-单行函数大全
2022-06-25 08:13:00 【旷世奇才李先生】
工欲善其事必先利其器
文章持续更新,可以微信搜索【小奇JAVA面试】第一时间阅读,回复【资料】获取福利,回复【项目】获取项目源码,回复【简历模板】获取简历模板,回复【学习路线图】获取学习路线图。

一、字符串函数
转换为大写函数:upper
转换为小写函数:lower
首字母变为大写:initcap
计算字符串长度:length
字符串替换:replace(类,要替换的值,替换为值)
字符串截取:substr(列,起始点,长度)
1、查询学生的姓名并大写
select upper(name) from student;
2、查询学生的姓名并小写
select lower(name) from student;
3、将"helloword"首字母大写
注意dual是oracle自带的一张表
select initcap('helloword') from dual;
结果:Helloword
4、查询所有的学生姓名并首字母大写
以下两种都可以
select initcap(name) from student;
5、查询所有学生的姓名,并显示姓名的长度
select name, length(name) from student;
6、查询所有学生的姓名,并将“张三”改为“李四”
select replace(name,'张三','李四') from student;
7、将"hello word"中的空格去除
select replace('hello word',' ','') from dual;
9、查询出所有学生的姓氏
注意,截取字符初始是从1开始的,如果初始值写为0也会默认从1开始
select substr(name,1,1) from student;
二、数值函数
四舍五入:round(数据,保留小数位)
直接截取小数位,不进行四舍五入:trunc(数据,保留小数位)
取模函数:mod(被除数,除数)
1、将123.4567保留两位小数
select round(123.4567,2) from dual;
结果:123.46
2、将123.4567保留小数点前两位,小于5的舍去
select round(123.4567,-2) from dual;
结果:100
3、将-123.65取整数
select round(-123.65) from dual;
结果:-124
4、将123.4567直接截取为两位小数
select trunc(123.4567) from dual;
结果:123.45
5、将-123.65直接截取为整数
select trunc(-123.65) from dual;
结果:-123
6、将10与3取模
select mod(10,3) from dual;
结果:1
三、日期函数
oracle自带的当前日期数据列:sysdate
计算两个日期之间相差的月份:months_between(日期1,日期2)
增加若干月后的日期:add_months(日期,月数)
查询当月的最后一天:last_day(日期)
计算下一个指定的日期:next_day(日期,一周时间数)
1、查询当前日期
注意:这里查询的是oracle数据库所安装在的电脑上的日期,比如oracle安装在服务器上,这样你用自己本地的电脑远程查询数据库的日期和自己本机电脑日期会有差距,一般为1分钟左右。
select sysdate from dual;
2、查询三天后的日期
select sysdate + 3 from dual;
3、查询两个日期之间相差的月数
select months_between(date1,date2) from dual;
4、查询两个日期之间相差的年数
select trunc(months_between(date1,date2)/12) years from dual;
5、查询当前日期5个月后的日期
select add_months(sysdate,5) from dual;
6、查询当月的最后一天
select last_day(sysdate) from dual;
7、查询下个星期三
select next_day(sysdate,'星期三') from dual;
四、转换函数
转换为字符串:to_char()
转换为日期:to_date()
转换为数字:to_number()
1、将当前日期格式化为yyyy-mm-dd格式的字符串
select to_char(sysdate,'yyyy-mm-dd') from dual;
2、查询当前日期的年份
select to_char(sysdate,'yyyy') year from dual;
3、将“1998-09-27”转换为日期格式
select to_date('1998-09-27','yyyy-mm-dd') from dual;
4、将“1”转换为数字格式
select to_number('1') from dual;
五、通用函数
如果为空则使用定义的值,如果不为空则用原来的值:nvl(列,定义值)
多值判断:decode(列,匹配内容1,显示内容1,匹配内容2,显示内容2)
1、查询同学的姓名,如果姓名为空就用“张三”代替
select nvl(name,'张三') from student;
2、查询同学年龄为空的就用0代替
select nvl(age,0) from student;
3、查询学生的性别,并将“男”对应“man”,将“女”对应“woman”
select decode(sex,'男','man','女','woman') from student;
六、总结
这里的相关内容还没有整理完毕,文章后面持续更新,建议收藏。
文章中涉及到的命令大家一定要像我一样每个都敲几遍,只有在敲的过程中才能发现自己对命令是否真正的掌握了。
可以微信搜索【小奇JAVA面试】第一时间阅读,回复【资料】获取福利,回复【项目】获取项目源码,回复【简历模板】获取简历模板,回复【学习路线图】获取学习路线图。
边栏推荐
猜你喜欢
![[operation tutorial] how does the tsingsee Qingxi video platform import the old database into the new database?](/img/21/08194ac26dec50c222b88f1f64f894.png)
[operation tutorial] how does the tsingsee Qingxi video platform import the old database into the new database?

【515. 在每个树行中找最大值】

故障:Outlook 收发邮件时的 0x800CCC1A 错误

从别人库里拷贝的游戏如何再自己的库里显示

The city chain technology platform is realizing the real value Internet reconstruction!

A 35 year old Tencent employee was laid off and sighed: a suite in Beijing, with a deposit of more than 7 million, was anxious about unemployment

Day 5 script and UI System

Cazy eight trigrams maze of Chang'an campaign

compiling stm32f4xx_it.c... “.\Objects\BH-F407.axf“ - 42 Error(s), 1 Warning(s).

Wechat applet_ 7. Project practice, local life
随机推荐
[operation tutorial] how does the tsingsee Qingxi video platform import the old database into the new database?
云网络技术的好处以及类型
声纹技术(五):声纹分割聚类技术
一、单个神经元网络构建
How to choose an account opening broker? Is it safe to open an account online?
Trendmicro:apex one server tools folder
在哪个平台买股票开户安全?求分享
[MySQL] understanding of transactions
C language "recursive series": recursive implementation of 1+2+3++ n
Summary of NLP data enhancement methods
Various synchronous learning notes
RTOS 多线程下hardfault问题总结
wav文件(波形文件)格式分析与详解
atguigu----18-组件
在华泰证券上面开户好不好,安不安全?
socket编程——epoll模型
声纹技术(一):声纹技术的前世今生
对常用I/O模型进行比较说明
[515. find the maximum value in each tree row]
About i/o -- the relationship between memory and CPU and disk