传智播客java学习--sql常用函数

我今天学习了sql语句的常用函数,是冯威老师讲的,我做了简单的笔记:

 

一,SQL函数:

1. 大小写转换函数

   lower 转成小写  

   upper 大写

   initcap  将字符串的(每个单词的)第一个字母变为大写,后面的小写

 

 select initcap('huangHY') from dual

  将首字母变成大写,其余的变成小写

 

2. 字符处理函数

   concat  连接两个字符串

   select concat(first_name,last_name) from employees

 

    substr  返回子字符串

     select substr('huangu',2,3) from dual

 

    length 返回字符串长度

    select length('huangyu') from dual

 

    instr(c1,c2,i,j)   子串的位置

    select instr('huangyu','yu',2,3) from dual

 

    lpad  补充

    lpad('husangyu',10,'*') from dual

 

    rpad

 

    trim ,截取两端的空格

     select trim('huangyu') from dual

 

    replace('wewwew','e','huangyu') 替换函数

 

3.数学函数

 

   round(num1,2)   按指定的小数位数进行四舍五入,未指定则默认为0

   select round(333.353,2)    333.35

  

   mod(num1,num2) 两数想除

 

4.日期函数

   months_between()   计算两个日期相差多少月?

   add_months(sysdate,10)加十个月

   next_day(date,'星期五')

   last_day(syadete) ,计算指定日期的最后一天 

   trunc(date)

   round(date,'YY') 本年的第一天

   round(date,'DD')当前日期

   round(date,'mm') 本月第一天

   round(date,'dy') 本周第一天(星期四进位)

   

 

5.转换函数

 

  显示的数据类型转换:

  to_char(date,'fmt')

  to_char(sysdate,'yy-mm-dd')

 

  输出日期的字符串表示格式;

 

6.修改为中文语言环境

   alter session set nls_language='SIMPLIFIED CHINESE'

  

7 日期的英文表示 ddspth

 

  select to_char(sysdate,'year-mm-ddspth-dd') from dual

 

8.时间表示:  hh24-mi-ss 

 AM和PM没有区别

 

 

9.select to_char(sysdate,'month "of" year') from dual

 

10,  字符串转换为日期  (各个部分必须对应)

  to_date('06-4月-2008','dd-mon-yyyy')

  

11.开发中

  select to_char(sysdate,'yyyy-mm-dd')

 

12.to_char(num,'fmt') 将数字转换问为日期

   to_char(3333.45,'9999.999999')

   to_char(3333.45,'9999.999999')

 

13.to_number 将字符转换为数字;

  to_number('333.33','999.99')

 

14.其他函数

  NVL(expr1,expr2) expr1为空,输出expr2 ,expr1不为空,

输出expr1

  支持日期,字符,数字格式

 

二.SQL组合函数

 

1.avg() ique平均工资

组函数:avg,min,max,count,sum

 

2. min和max 适用于任何数据类型

 

3.count(*)  返回符合条件的记录

  count(字段)

  count(distinct(job)) 返回非空不重复的记录

 

4.nvl迫使分组函数可以包括0 ;

 

5.group by 将表分成小组,

  在group by 中的字段可以不出现在select中,

  在select中,但在组函数中的字段必须出现在group by中

 

6.对组函数进行条件过滤必须放在having,例如, having avg(sal)>1400

 

7.执行流程:where先执行,对查询条件进行过滤

            group by 对符合where的查询结果进行分组

            having 对分组后的结果进行过滤

            order by 进行排序

 

8.例子: 查询公司的人数,以及在80,81,82,87年,每年雇用的人数,结果类似下面的格式

total 1980       1981       1982       1987

14       1           10         1         2

 

 select count(*) "total" ,

      sum(decode(to_char(hiredate,'yyyy'),'1980',1,0))  "1980",

      sum(decode(to_char(hiredate,'yyyy'),'1981',1,0))  "1981",

      sum(decode(to_char(hiredate,'yyyy'),'1982',1,0))  "1982",

      sum(decode(to_char(hiredate,'yyyy'),'1987',1,0))  "1987"

 from emp

 

SQL组合函数在其他的数据库也可通用,

 

 

 

总结:

今天学的以下函数有一定的难度,需要课下多加练习:

 1.decode( ) ,他的使用规则是需要记忆的

 2.case :

      When   then

      When   then

End

3,group by 与count 的组合用法,容易让人产生混淆,需多加记忆

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值