转换类型,如:把 string 转为 integer
select cast('1234' as INTEGER) as num
第一个参数有 year,month,day,hour 等…
第二个参数是具体时间,
函数名:时间的一部分,意义是取这个时间的 年月日时…
例子:获取当前时间的 月,日,时
select now() as now,date_part('month',now())
select now() as now,date_part('day',now())
select now() as now,date_part('hour',now())
查询2020-1-1距离现在多少天
select date_part('day',now()-'2020-1-1')
例子:综合应用1,2函数
select staff.password,
staff.password_random,
cast(now() as TIMESTAMP) as now,
cast(staff.pwd_mod_date as TIMESTAMP) as mod_date,
cast(now() as TIMESTAMP)-cast(staff.pwd_mod_date as TIMESTAMP) as cha,
staff.pwd_mod_date,
date_part('day',cast(now() as TIMESTAMP)-cast(staff.pwd_mod_date as TIMESTAMP)) as pwdModDuration
from pm_staff staff
where staff.username ='zso'
select now()
2022-03-22 09:47:25.827377+08
select current_timestamp
2022-03-22 09:48:25.330858+08
select current_date
2022-03-22
select current_time
09:48:44.248972+08
不要毫秒
select now()::TIMESTAMP(0)
2022-12-21 16:15:17
select current_timestamp::TIMESTAMP(0)
2022-12-21 16:15:17
1、now() 和 current_timestamp 都能正常操作,interval关键字 可省略
select now() +interval '2 y'
select now() +interval '2 month'
select now() +'2 w'
select now() +'2 d'
select now() +'2 h'
select now() +'2 m'
select now() +'2 s'
如果想减掉,只能加负数
select now() +'-2 d'
2、select current_timestamp + '2 y'
······同now()函数
3、操作不同,结果不同
1)加数字默认是天
select current_date+2
结果:2022-03-24
2)'参数'可以是年,月,周,日,时,分,秒;这里interval 不能省略
select current_date +interval '2 y';
select current_date +interval '2 month';
select current_date +interval '2 s';
结果:2022-03-24 00:00:02
......
4、interval 不能省略,参数时,分,秒;
参数如果是年,月,日,虽然没有报错,但是没有效果
select current_time+ interval '1 h'
select current_time+ interval '1 m'
select current_time+ interval '1 s'
select to_char(now(),'yyyy-MM-dd hh24:mi:ss')
current_date只精确到天,可以加减。当前的时间减掉一天,再格式化
now(),current_timestamp加减数字不行。
这个可以执行成功
select to_char(current_date-1,'yyyy-MM-dd hh24:mi:ss')
这两个不行,报错
select to_char(now()-1,'yyyy-MM-dd hh24:mi:ss')
select to_char(current_timestamp-1,'yyyy-MM-dd hh24:mi:ss')
错误详情 ERROR: operator does not exist: timestamp with time zone - integer
截取到小时
select date_trunc('hour', current_timestamp)
截取到天
select date_trunc('day', current_timestamp)
select round(123.13243242,2)
select round(20.2342,2)||'%'
substr(列名,开始截取的下标,截取的长度),下标从1开始。
1、创建序列
create sequence md_collector_task_log_sequence increment by 1 minvalue 1 no maxvalue start with 1;
2、查询表中的序列(参数:表名,自增的列名)
select pg_get_serial_sequence('pm_menu', 'id');
3、查询序列的下一值
select nextval('md_collector_task_log_sequence')
4、删除序列
DROP SEQUENCE IF EXISTS md_collector_task_log_sequence
给update加limit条件
with t1 as
(select drzt2
from accfault_drzt_detail
where drzt34 = '2022-11-16' limit 200)
update accfault_drzt_detail
SET drzt73 = '是'
WHERE drzt2 in (select * from t1)
create index info_iom_oltport_eqp_id on info_iom_oltport (eqp_id)
1、方法一
select * into map_zyyj_statistic_bak_20221021
from map_zyyj_statistic
2、方法二
create table map_zyyj_statistic_bak_20221021 as
select * from map_zyyj_statistic
把其他表备的一些字段关联更新到主表
update map_zyyj_statistic set dklylfxx =
(
select dklylfxx from map_zyyj_statistic_update_dklylfxx where
map_zyyj_statistic.countyname = map_zyyj_statistic_update_dklylfxx.countyname
)
持续记录中…