8 Greenplum 常用函数

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xfg0218/article/details/86473645

8 Greenplum 常用函数

8.1 字符串函数

8.1.1 常见字符串函数

 

8.1.2 函数常见示例

8.1.2.1 字符串拼接

chinadaas=# \timing

Timing is on.

chinadaas=# select 'green'||'plum' as dbname;

  dbname   

-----------

 greenplum

(1 row)

Time: 33.768 ms

 

\timing 是开启执行时间,可以看出与oracle的用法一直

8.1.2.2 查看字符串的长度

chinadaas=# select length('greenplum');

 length

--------

      9

(1 row)

Time: 7.212 ms

 

可以看出是索引是从1开始计算的

8.1.2.3 查看字符在字符串的位置

chinadaas=# select position('plum' in 'greenplum');

 position

----------

        6

(1 row)

Time: 7.424 ms

 

在以上的结果中可以看出索引是在1开始计算的

8.1.2.4 在制定的位置截取字符串

chinadaas=# select substring('greenplum' from 6 for 4);

 substring

-----------

 plum

(1 row)

Time: 7.362 ms

 

在以上可以看出位置是从1开始计算的,for 4 是向后截取多少位

8.1.2.5 删除字符串的制定字符

chinadaas=# select trim(both 'x' from 'xgreenplumxx');

   btrim   

-----------

 greenplum

(1 row)

Time: 7.250 ms

 

使用关键字both 与from相关联删除字符串中的数据

 

8.1.2.6 字符串转大小写

转大写

chinadaas=# select upper('greenplum');

   upper   

-----------

 GREENPLUM

(1 row)

Time: 9.703 ms

 

转小写

chinadaas=# select lower('GREENPLUM');

   lower   

-----------

 greenplum

(1 row)

Time: 7.324 ms

8.1.2.7 替换制定的字符串

按照索引位置替换

chinadaas=# select overlay('greenxxxm' placing 'plu' from 6 for 3);

  overlay  

-----------

 greenplum

(1 row)

Time: 15.349 ms

 

 

使用关键字替换

chinadaas=# select replace('greenplum','plum','xxx');

 replace  

----------

 greenxxx

(1 row)

Time: 9.509 ms

 

在以上可以看出替换还是比较简单的

8.1.2.8 按照分隔符分割字符串

chinadaas=# select split_part('abc|def|ght','|',3);

 split_part

------------

 ght

(1 row)

Time: 7.254 ms

 

在以上的结果中可以看出分割是以索引1开始计算的

8.2 日期及时间函数

8.2.1 常见日期及时间函数

8.2.2 函数常见使用

8.2.2.1 查看两个日期的

chinadaas=# \timing

Timing is on.

chinadaas=# select age('20181207','20181201');

  age   

--------

 6 days

(1 row)

Time:7.844 ms

 

chinadaas=# select age(timestamp '20181201');

  age   

--------

 6 days

(1 row)

Time: 5.644 ms

 

第一行的是开始时间显示,可以看出比较两个时间的差异还是比较方便的,也可以计算当前的时间相差值

8.2.2.2 查看当前的日期

chinadaas=# select current_date;

    date    

------------

 2018-12-07

(1 row)

Time: 4.614 ms

8.2.2.3 获取当前的时间

chinadaas=# select current_time;

       timetz       

--------------------

 15:21:29.245372+08

(1 row)

Time: 5.214 ms

 

查看数据可以看出精确到毫秒

8.2.2.4 获取精确的时间戳

chinadaas=# select current_timestamp;

             now              

------------------------------

 2018-12-07 15:22:42.59494+08

(1 row)

Time: 7.214 ms

8.2.2.5 获取时间戳的制定参数

以下获取的小时

chinadaas=# select date_part('hour',timestamp'2018-11-07 11:07:30');

 date_part

-----------

        11

(1 row)

Time: 7.083 ms

 

以下获取的是分钟

chinadaas=# select date_part('minute',timestamp'2018-11-07 11:07:30');

 date_part

-----------

         7

(1 row)

Time: 7.209 ms

 

以下获取的是秒

chinadaas=# select date_part('second',timestamp'2018-11-07 11:07:30');

 date_part

-----------

        30

(1 row)

Time: 9.551 ms

 

以下是获取年

chinadaas=# select date_part('year',timestamp'2018-11-07 11:07:30');

 date_part

-----------

      2018

(1 row)

Time: 7.246 ms

 

以下是获取月

chinadaas=# select date_part('month',timestamp'2018-11-07 11:07:30');

 date_part

-----------

        11

(1 row)

Time: 7.271 ms

 

以下获取的是天

chinadaas=# select date_part('day',timestamp'2018-11-07 11:07:30');

 date_part

-----------

         7

(1 row)

 

Time: 7.314 ms

 

在以上参数中可以看出参数可以为:year , month , day , hour , minute , second 

 

 

也可以使用以下语句获取时间的差异

chinadaas=# select extract(hour from timestamp '20181207 10:07:30');

 date_part

-----------

        10

(1 row)

Time: 7.347 ms

 

以上的参数也可以使用:year , month , day , hour , minute , second

 

8.2.2.6 获取当前的时间戳

chinadaas=# select now();

             now              

------------------------------

 2018-12-07 15:38:49.08221+08

(1 row)

Time: 9.838 ms

8.2.2.7 获取当前的时间

chinadaas=# select LOCALTIME;

      time       

-----------------

 15:43:38.282021

(1 row)

Time: 9.423 ms

8.2.2.8 获取当前的时间戳

chinadaas=# select LOCALTIMESTAMP;

         timestamp          

----------------------------

 2018-12-07 15:47:49.748872

(1 row)

Time: 9.545 ms

猜你喜欢

转载自blog.csdn.net/xfg0218/article/details/86473645
今日推荐