8c sql手册 二

      1. 位串函数和操作符

本节描述操作类型为bit和bit varying的值的函数和操作符。

操作符

描述

例子

结果

||

连接

B'10001' || B'011'

10001011

&

按位与

B'10001' & B'01101'

00001

|

按位或

B'10001' | B'01101'

11101

#

按位异或

B'10001' # B'01101'

11100

~

按位求反

~ B'10001'

01110

<<

按位左移

B'10001' << 3

01000

>>

按位右移

B'10001' >> 2

00100

      1. 模式匹配

GBase 8c提供了三种独立的实现模式匹配的方法:LIKE操作符、SIMILAR TO操作符和POSIX-风格的正则表达式。

        1. LIKE

string LIKE pattern [ESCAPE escape-character]

string NOT LIKE pattern [ESCAPE escape-character]

如果该string匹配了提供的pattern,那么LIKE表达式返回真。例如:

'abc' LIKE 'abc'    true

'abc' LIKE 'a%'     true

'abc' LIKE '_b_'    true

'abc' LIKE 'c'      false

        1. SIMILAR TO正则表达式

string SIMILAR TO pattern [ESCAPE escape-character]

string NOT SIMILAR TO pattern [ESCAPE escape-character]

SIMILAR TO操作符根据自己的模式是否匹配给定串而返回真或者假。例如:

'abc' SIMILAR TO 'abc'      true

'abc' SIMILAR TO 'a'        false

'abc' SIMILAR TO '%(b|d)%'  true

'abc' SIMILAR TO '(b|c)%'   false

        1. POSIX正则表达式

下表列出了所有可用于POSIX正则表达式模式匹配的操作符:

操作符

描述

例子

~

匹配正则表达式,大小写敏感

'thomas' ~ '.*thomas.*'

~*

匹配正则表达式,大小写不敏感

'thomas' ~* '.*Thomas.*'

!~

不匹配正则表达式,大小写敏感

'thomas' !~ '.*Thomas.*'

!~*

不匹配正则表达式,大小写不敏感

'thomas' !~* '.*vadim.*'

例如:

'abc' ~ 'abc'    true

'abc' ~ '^a'     true

'abc' ~ '(b|d)'  true

'abc' ~ '^(b|c)' false

      1. 数据类型格式化函数

GBase 8c数据库格式化函数提供一套强大的工具用于把各种数据类型 (日期/时间、整数、浮点、数字) 转换成格式化的字符串以及反过来从格式化的字符串转换成指定的数据类型。这些函数都遵循一个公共的调用习惯: 第一个参数是待格式化的值,而第二个是一个定义输出或输入格式的模板。

函数

返回类型

描述

例子

to_char(timestamp, text)

text

把时间戳转成字符串

to_char(current_timestamp, 'HH12:MI:SS')

to_char(interval, text)

text

把间隔转成字符串

to_char(interval '15h 2m 12s', 'HH24:MI:SS')

to_char(int, text)

text

把整数转成字符串

to_char(125, '999')

to_char(double precision, text)

text

把实数或双精度转成字符串

to_char(125.8::real, '999D9')

to_char(numeric, text)

text

把数字转成字符串

to_char(-125.8, '999D99S')

to_date(text, text)

date

把字符串转成日期

to_date('05 Dec 2000', 'DD Mon YYYY')

to_number(text, text)

numeric

把字符串转成数字

to_number('12,454.8-', '99G999D9S')

to_timestamp(text, text)

timestamp with time zone

把字符串转成时间戳

to_timestamp('05 Dec 2000', 'DD Mon YYYY')

      1. 时间/日期函数和操作符

如下表为可用于处理日期/时间的函数:

函数

返回类型

描述

例子

结果

age(timestamp, timestamp)

interval

减去参数,生成一个使用年、月(而不是只用日)的“符号化”的结果

age(timestamp '2001-04-10', timestamp '1957-06-13')

43 年 9 月 27 日

age(timestamp)

interval

current_date(在午夜)减去

age(timestamp '1957-06-13')

43 years 8 mons 3 days

clock_timestamp()

timestamp with time zone

当前日期和时间(在语句执行期间变化)

current_date

date

当前日期

current_time

time with time zone

当前时间(一天中的时间)

current_timestamp

timestamp with time zone

当前日期和时间(当前事务开始时)

date_part(text, timestamp)

double precision

获得子域(等价于extract

date_part('hour', timestamp '2001-02-16 20:38:40')

20

date_part(text, interval)

double precision

获得子域(等价于extract

date_part('month', interval '2 years 3 months')

3

date_trunc(text, timestamp)

timestamp

截断到指定精度

date_trunc('hour', timestamp '2001-02-16 20:38:40')

2001-02-16 20:00:00

date_trunc(text, interval)

interval

截断到指定精度

date_trunc('hour', interval '2 days 3 hours 40 minutes')

2 days 03:00:00

extract(field from timestamp)

double precision

获得子域

extract(hour from timestamp '2001-02-16 20:38:40')

20

extract(field from interval)

double precision

获得子域

extract(month from interval '2 years 3 months')

3

isfinite(date)

boolean

测试有限日期(不是+/-无限)

isfinite(date '2001-02-16')

true

isfinite(timestamp)

boolean

测试有限时间戳(不是+/-无限)

isfinite(timestamp '2001-02-16 21:28:30')

true

isfinite(interval)

boolean

测试有限间隔

isfinite(interval '4 hours')

true

justify_days(interval)

interval

调整间隔这样30天时间周期可以表示为月

justify_days(interval '35 days')

1 mon 5 days

justify_hours(interval)

interval

调整间隔这样24小时时间周期可以表示为日

justify_hours(interval '27 hours')

1 day 03:00:00

justify_interval(interval)

interval

使用justify_daysjustify_hours调整间隔,使用额外的符号调整

justify_interval(interval '1 mon -1 hour')

29 days 23:00:00

localtime

time

当前时间(一天中的时间)

localtimestamp

timestamp

当前日期和时间(当前事务的开始)

make_date(year int, month int, day int)

date

从年、月、日域创建日期

make_date(2013, 7, 15)

2013-07-15

make_interval(years int DEFAULT 0, months int DEFAULT 0, weeks int DEFAULT 0, days int DEFAULT 0, hours int DEFAULT 0, mins int DEFAULT 0, secs double precision DEFAULT 0.0)

interval

从年、月、周、日、时、分、秒域创建 interval

make_interval(days => 10)

10 days

make_time(hour int, min int, sec double precision)

time

从时、分、秒域创建时间

make_time(8, 15, 23.5)

08:15:23.5

make_timestamp(year int, month int, day int, hour int, min int, sec double precision)

timestamp

从年、月、日、时、分、秒域创建时间戳

make_timestamp(2013, 7, 15, 8, 15, 23.5)

2013-07-15 08:15:23.5

make_timestamptz(year int, month int, day int, hour int, min int, sec double precision, [ timezone text ])

timestamp with time zone

从年、月、日、时、分、秒域创建带时区的时间戳。如果没有指定timezone, 则使用当前时区。

make_timestamptz(2013, 7, 15, 8, 15, 23.5)

2013-07-15 08:15:23.5+01

now()

timestamp with time zone

当前日期和时间(当前事务的开始)

statement_timestamp()

timestamp with time zone

当前日期和时间(当前事务的开始)

timeofday()

text

当前日期和时间(像clock_timestamp,但是作为一个text字符串)

transaction_timestamp()

timestamp with time zone

当前日期和时间(当前事务的开始)

to_timestamp(double precision)

timestamp with time zone

把 Unix 时间(从 1970-01-01 00:00:00+00 开始的秒)转换成 timestamp

to_timestamp(1284352323)

2010-09-13 04:32:03+00

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值