oracle用到的几个sql函数随笔

1.to_char
将数值型或者日期型转化为字符型,其中最常见用到日期转字符。

select to_char(begin_time,'yyyy-MM-dd') from aa a;

2.add_months
这个函数用于计算在时间x的基础上Y个月后的时间值,要是Y的值为负数的话就是在这个时间点之间的时间值(这个时间-Y个月)

add_months(times,months)

下面的例子表示得到的时间为8个月后的时间值

select to_char(add_months(begin_time,8),'yyyy-MM-dd') from aa a;

3.case when then else end
是一个条件判断相当于if–else

SELECT 
  CASE YYEAR 
  WHEN '1995' 
  THEN '生日年'  
  ELSE  '新年' 
  END   YYEAR  
  FROM 
  SD_GSS

4.listagg
它可实现对列值的拼接。

select listagg(ename,',')within group(order by sal)name from emp;

5.||
字符串的拼接,把sql查出来的结果进行拼接得到我们想要的结果

下面的例子得到结果是:八个月后的时间是:2019-09-02

select "八个月后的时间是:"||to_char(add_months(begin_time,8),'yyyy-MM-dd') from aa a;

6.start with connect by prior
递归查询,是根据条件递归查询"树"

例如:查询出节点为1的所有子节点

select * from aa o start with o.parent_id='1' connect by prior o.id=o.parent_id

7.DECODE
DECODE(value, if1, then1, if2,then2, if3,then3, . . . else )
也是一个条件判断相当于if–else

select decode(birthday,'1995','生日年','新年') from aa a

最后附上创建oracle表的例子:

-- Create sequence 
create sequence WJGL_FILE_SEQ
minvalue 1
maxvalue 999999999999999999999999999
start with 61
increment by 1
cache 20
order;


-- Create table
create table WJGL_FILE
(
  wjgl_file_id   NUMBER(16) not null,
  wjgl_id        NUMBER(16) not null,
  file_name      NVARCHAR2(200),
  file_path      NVARCHAR2(500),
  sync_operate   CHAR(1) default 1 not null,
  sync_last_time DATE default sysdate not null
)
tablespace USERS
  pctfree 10
  initrans 1
  maxtrans 255
  storage
  (
    initial 16K
    next 8K
    minextents 1
    maxextents unlimited
  );
-- Add comments to the table 
comment on table WJGL_FILE
  is '文件管理附件表';
-- Add comments to the columns 
comment on column WJGL_FILE.wjgl_file_id
  is '主键ID';
comment on column WJGL_FILE.wjgl_id
  is '文件管理表id';
comment on column WJGL_FILE.file_name
  is '附件名称';
comment on column WJGL_FILE.file_path
  is '附件路径';
comment on column WJGL_FILE.sync_operate
  is '最后操作状态(1新增 2修改 3逻辑删除 默认为1)';
comment on column WJGL_FILE.sync_last_time
  is '最后操作时间';
-- Create/Recreate primary, unique and foreign key constraints 
alter table WJGL_FILE
  add primary key (WJGL_FILE_ID)
  using index 
  tablespace USERS
  pctfree 10
  initrans 2
  maxtrans 255
  storage
  (
    initial 64K
    next 1M
    minextents 1
    maxextents unlimited
  );
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值