Oracle SQL Lesson (3) - 使用单行函数自定义输出

大小写转换函数
LOWER('SQL Course') = sql course
UPPER('SQL Course') = SQL COURSE
INITCAP('SQL Course') = Sql Course
SELECT employee_id, last_name, department_id
FROM employees
WHERE last_name = 'higgins';

SELECT employee_id, last_name, department_id
FROM employees
WHERE LOWER(last_name) = 'higgins';

任何用户均可以使用dual,用于计算,输出字符串,输出系统日期
select lower('SQL Course') from dual;
select 56+9 from dual;
select sysdate from dual;

字符操作函数
CONCAT('Hello', 'World') = HelloWorld
SUBSTR('HelloWorld',1,5) = Hello
LENGTH('HelloWorld') = 10
INSTR('HelloWorld', 'W') = 6
LPAD(salary,10,'*') = *****24000
RPAD(salary, 10, '*') = 24000*****
REPLACE('JACK and JUE','J','BL') = BLACK and BLUE
TRIM('H' FROM 'HelloWorld') = elloWorld

concat功能不及||强大,仅能连接两列,不能连接字符串
select substr('HelloWorld', 6, 5) from dual;
select substr('HelloWorld', -5, 5) from dual;
第二个参数为负数表示从后往前数
第三个参数不指定表示到结束
conn scott/tiger;
select ename, lpad(ename, 10, '*'),rpad(ename, 10, '*') from emp;
select trim('H' from 'HelloWorld') from dual;
select trim('H' from 'HHHHHHHelloWorldHH') from dual;
select trim('H' from 'HelloHHWorldHH') from dual;

Trim的作用
create table t1(id number, name char(10));
create table t2(id number, name varchar2(10));
insert into t1 values(1, 'a');
insert into t1 values(2, 'b');
insert into t2 values(1, 'a');
insert into t2 values(2, 'b');
select * from t1, t2 where t1.name = t2.name;
select * from t1, t2 where trim(t1.name) = t2.name;

char(n)和varchar2(n)的区别
n代表长度
char固定长度:长度不够n,仍然分配n个字符空间,用空格补齐
varchar2可变长度:根据实际长度分配空间

clob和long的区别: 表中只能有一列为long,建议使用clob
clob:字符长度非常多的
blob:图片,影音

数字函数
ROUND(45.926, 2) = 45.93
ROUND(45.926, -1) = 50
TRUNC(45.926, 2) = 45.92
TRUNC(45.926, -1) = 40
MOD(1600, 300) = 100
MOD(300, 1600) = 300
负数均表示往前数

日期函数
日期在数据库中以数字格式方式存储
The default date display format is DD-MON-RR.
Enables you to store 21st-century dates in the 20th century by specifying only the last two digits of the year
Enables you to store 20th-century dates in the21st century in the same way
RR日期格式
返回离当前年份较近的日期
alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss';
alter session set nls_date_format='DD-MON-RR';
alter session set nls_date_format='dd-mon-rr';
select sysdate from dual;
select sysdate+2 from dual;(天数)
select sysdate+2/24 from dual;(小时)
select hiredate,sysdate,sysdate-hiredate from emp;(天数)

MONTHS_BETWEEN('01-SEP-95','11-JAN-94') = 19.6774194
ADD_MONTHS ('31-JAN-96','1') = '29-FEB-96'
NEXT_DAY ('01-SEP-95','FRIDAY') = '08-SEP-95'
LAST_DAY ('01-FEB-95') = '28-FEB-95'

Assume SYSDATE = '25-JUL-03':
ROUND(SYSDATE,'MONTH') = 01-AUG-03
ROUND(SYSDATE ,'YEAR') = 01-JAN-04

TRUNC(SYSDATE ,'MONTH') = 01-JUL-03
TRUNC(SYSDATE ,'YEAR') = 01-JAN-03

上一篇:linux查看内存free


下一篇:C#反射—解决类的实例化问题