PostgreSQL中日期类型与字符串类型的转换方法
示例如下:
postgres=# select current_date;
date
------------
2015-08-31
(1 row)
postgres=# select to_char(current_date,‘YYYYMMDD‘);
to_char
----------
20150831
(1 row)
字符串转换为日期
postgres=# select to_date(‘20150831‘,‘YYYYMMDD‘);
to_date
------------
2015-08-31
(1 row)
PostgreSQL命令行中使用变量的问题
postgres=# create table test_tb(id int);
CREATE TABLE
postgres=# select * from test_tb ;
id
(0 rows)
postgres=# \set var 10
postgres=# \echo :var
10
postgres=# insert into test_tb values (:var);
INSERT 0 1
postgres=# select * from test_tb ;
id
10
(1 row)
PostgreSQL中SELECT INTO操作创建临时表的问题
postgres=# select * from child ;
no | student_name | age | class_no
------------------+---------
1 | zhangsan | 11 | 1
2 | zhaosi | 11 | 1
3 | xiaoshenyang | 12 | 21
4 | songxiaobao | 13 | 21
5 | zhaobenshan | 31 | 1
6 | huoya | 41 | 2
7 | haha | 81 | 2
(7 rows)
postgres=# select no,age into temporary table temp01 from child where
age=11;
SELECT 2
postgres=# select * from temp01 ;
no | age
---+----
1 | 11
2 | 11
(2 rows)
PostgreSQL中是否有表示错误,SQL状态的全局变量
PostgreSQL处理事务中错误的方法为抛出异常并捕获之
示例如下:
postgres=# create function test_sqlerrm()returns void as
$$
declare v_val int :=0;
begin
raise notice ‘%‘,‘1111‘;
v_val :=1/0;
exception
when others then
raise notice ‘%‘,sqlerrm;
end;
$$ language plpgsql;
CREATE FUNCTION
postgres=# select test_sqlerrm();
NOTICE: 00000: 1111
LOCATION: exec_stmt_raise, pl_exec.c:3035
NOTICE: 00000: division by zero
LOCATION: exec_stmt_raise, pl_exec.c:3035
test_sqlerrm
--------------
(1 row)
PostgreSQL中 CASE WHEN 语法问题
PostgreSQL同样支持case when +表达式 then +操作的语法。
示例如下:
postgres=# select job,ename from emp;
job | ename
----------+-------
CLERK | SMITH
SALESMAN | ALLEN
SALESMAN | WARD
MANAGER | JONES
SALESMAN | MARTIN
MANAGER | BLAKE
MANAGER | CLARK
ANALYST | SCOTT
PRESIDENT | KING
SALESMAN | TURNER
CLERK | ADAMS
CLERK | JAMES
ANALYST | FORD
CLERK | MILLER
(14 rows)
下面的SQL,意为在job字段,从第二位开始取,取3位,为‘LER‘的则返回LER。
下面还是在job字段,从第二位开始取,取3位,为‘ALE‘的则返回在ename字段,第二位开始取,取两位的结果。
postgres=# select ename, job,case when substring(job,2,3)=‘LER‘ THEN substring(job,2,3)
postgres-# when substring(job,2,3)=‘ALE‘ then substring(ename,2,2)
postgres-# else ‘other‘
postgres-# end
postgres-# from emp;
ename | job | case
----------------------
SMITH | CLERK | LER
ALLEN | SALESMAN | LL
WARD | SALESMAN | AR
JONES | MANAGER | other
MARTIN | SALESMAN | AR
BLAKE | MANAGER | other
CLARK | MANAGER | other
SCOTT | ANALYST | other
KING | PRESIDENT | other
TURNER | SALESMAN | UR
ADAMS | CLERK | LER
JAMES | CLERK | LER
FORD | ANALYST | other
MILLER | CLERK | LER
(14 rows)
原文:http://www.cnblogs.com/songyuejie/p/4877881.html