postgresql字符串函数和操作符详解

postgresql字符串函数和操作符详解1.string || string 字符串连接hxf=# select * from huangxifeng ;first_name | last_name------------+-----------huang | xifeng(1 row)hxf=# select first_name||'.'||last_name as fullname from huangxifeng ;fullname--------------huang.xifeng(1 row)两个字段连接成名字,中间连接"."。

hxf=# select last_name||'.'||first_name as fullname from huangxifeng ;fullname--------------xifeng.huang(1 row)两个字段连接成名字,中间连接"."。

字段先后可随意调整。

2.bit_length(string) 字符串里二进制位的个数hxf=# select first_name,bit_length(first_name) from huangxifeng ; first_name | bit_length------------+------------huang | 40(1 row)一个字符8位,共5个字符,所以40位长。

hxf=# select last_name,bit_length(last_name) from huangxifeng ;last_name | bit_length-----------+------------xifeng | 48(1 row)一个字符8位,共6个字符,所以48位长。

3.char_length(string) 或 character_length(string) 字符串中的字符个数hxf=# select first_name,char_length(first_name) from huangxifeng ; first_name | char_length------------+-------------huang | 5(1 row)共5个字符hxf=# select last_name,char_length(last_name) from huangxifeng ; last_name | char_length-----------+-------------xifeng | 6(1 row)共6个字符hxf=# select first_name,char_length(first_name) from huangxifeng2; first_name | char_length------------+-------------黄 | 1(1 row)中文1个字符hxf=# select last_name,char_length(last_name) from huangxifeng2; last_name | char_length-----------+-------------锡峰 | 2(1 row)中文2个字符4.octet_length(string) 字符串中的字节数hxf=# select first_name,octet_length(first_name) from huangxifeng; first_name | octet_length------------+--------------huang | 5(1 row)英文5个字符就是5字节hxf=# select last_name,octet_length(last_name) from huangxifeng; last_name | octet_length-----------+--------------xifeng | 6(1 row)英文6个字符就是6字节hxf=# select first_name,octet_length(first_name) from huangxifeng2; first_name | octet_length------------+--------------黄 | 3(1 row)1个中文字符是3字节(数据库环境是utf8编码)hxf=# select last_name,octet_length(last_name) from huangxifeng2; last_name | octet_length-----------+--------------锡峰 | 6(1 row)2个中文字符就是6个字节了(数据库环境是utf8编码)5.overlay(string placing string from int [for int]) 替换子字符串hxf=# select overlay(last_name placing 'GG' from 7 for 2) from huangxifeng;overlay----------xifengGG(1 row)'xifeng' 一共是6位,从第7位开始的2位加GG。

6.position(substring in string) 指定的子字符串的位置hxf=# select last_name,position('x' in last_name) from huangxifeng; last_name | position-----------+----------xifeng | 1(1 row)'x'在last_name中第1位hxf=# select last_name,position('i' in last_name) from huangxifeng; last_name | position-----------+----------xifeng | 2(1 row)'i' 在last_name中第2位hxf=# select last_name,position('f' in last_name) from huangxifeng; last_name | position-----------+----------xifeng | 3(1 row)'f'在last_name中第3位7.substring(string [from int] [for int]) 抽取子字符串hxf=# select last_name,substring(last_name from 1 for 2) from huangxifeng;last_name | substring-----------+-----------xifeng | xi(1 row)提取第1位开始的2个字符hxf=# select last_name,substring(last_name from 3 for 4) from huangxifeng;last_name | substring-----------+-----------xifeng | feng(1 row)提取第3位开始的4个字符8.substring(string from pattern)抽取匹配 POSIX 正则表达式的子字符串。

hxf=# select last_name,substring(last_name from '^..') from huangxifeng;last_name | substring-----------+-----------xifeng | xi00xifeng | 0011xifeng | 1122xifeng | 22AAxifeng | AABBxifeng | BBCCxifeng | CC(7 rows)抽取开始的任意两个字符hxf=# select last_name,substring(last_name from '......$') from huangxifeng;last_name | substring-----------+-----------xifeng | xifeng00xifeng | xifeng11xifeng | xifeng22xifeng | xifengAAxifeng | xifengBBxifeng | xifengCCxifeng | xifeng(7 rows)抽取结尾的任意6个字符。

9.lower(string)把字符串转化为小写,upper(string)把字符串转化为大写hxf=# select last_name,lower(last_name),upper(last_name) from huangxifeng;last_name | lower | upper-----------+----------+----------xifeng | xifeng | XIFENG00xifeng | 00xifeng | 00XIFENG11xifeng | 11xifeng | 11XIFENG22xifeng | 22xifeng | 22XIFENGAAxifeng | aaxifeng | AAXIFENGBBxifeng | bbxifeng | BBXIFENGCCxifeng | ccxifeng | CCXIFENG(7 rows)10.trim([leading | trailing | both] [characters] from string)从字符串 string 的开头/结尾/两边删除只包含 characters 中字符(缺省是一个空白)的最长的字符串hxf=# select char_length(' huang '),trim(' huang '),length(trim(' huang ')) ;char_length | btrim | length-------------+-------+--------7 | huang | 5(1 row)' huang '前后有一空格共7位,trim默认去掉前后空格,去掉后共5位。

hxf=# select last_name,trim('00' from last_name) from huangxifeng; last_name | btrim-----------+----------xifeng | xifeng00xifeng | xifeng(2 rows)第二行前导的00不见了。

合集下载

postgre sql常用函数

postgre sql常用函数

postgre sql常用函数
在PostgreSQL中常用的函数有以下几种:
1. 聚合函数:用于对数据进行分组计算,常见的聚合函数有SUM、AVG、COUNT、MIN、MAX等。

2. 数学函数:用于进行数学运算,常见的数学函数有ABS(取绝对值)、ROUND(四舍五入)、CEIL(向上取整)、FLOOR(向下取整)等。

3. 字符串函数:用于对字符串进行操作,常见的字符串函数有CONCAT(拼接字符串)、SUBSTRING(截取子串)、LENGTH(计算字符串长度)等。

4. 日期函数:用于处理日期和时间数据,常见的日期函数有NOW (获取当前时间)、DATE_PART(提取日期部分)、DATE_TRUNC(截断日期部分)等。

5. 条件函数:用于根据条件进行计算,常见的条件函数有CASE WHEN(条件判断)、COALESCE(返回第一个非空值)等。

6. 转换函数:用于进行数据类型的转换,常见的转换函数有CAST (将数据类型转换为指定类型)、TO_CHAR(将日期转换为字符串)等。

7. 窗口函数:用于在查询结果中进行窗口操作,常见的窗口函数有ROW_NUMBER(返回行号)、RANK(返回排名)等。

这些是PostgreSQL中常用的函数,根据具体的业务需求,还可以使用更多的函数进行数据处理和计算。

Postgresql数据库的一些字符串操作函数

Postgresql数据库的一些字符串操作函数

Postgresql数据库的⼀些字符串操作函数 今天做项⽬遇到客户反映了⼀个⿇烦的事情,有⼀些数据存在,但就是在程序中搜索不出来,后来分析,发现问题为数据前⾯有⼏个空⽩字符,后来⽤SQL查询了⼀下,发现⼋九个数据表中,数千万条数据中有将近三百万条数据存在相同的问题,本想着在查询时添加匹配符'%',后来试运⾏了⼀下,发现不可⾏,因为尚有很多其它页⾯存在类似的搜索问题,并且这样会极⼤地影响到查询的速度,再加上客户迫切需要解决这个问题,由于在⽩天程序需不间断访问,并且不能对其运⾏速度产⽣较⼤的影响,所以排除了JDBC进⾏修改的⽅案,也排除了修改程序搜索代码的⽅案,头痛不⼰ 后来仔细想了⼀下,并尝试去查找相关sql字符串操作函数,确认有没有快捷的⽅式去解决空⽩字符串问题,很快,我们找到了合适的解决⽅案,对其中⼀个测试数据库进⾏了测试,结果⾮常令⼈满意,三百多万的数据只⽤了数分种,便去掉了所有的⽆⽤空格问题,为之兴奋不⼰。

⽤法如下update property set memorial_no = btrim(memorial_no, ' ') where memorial_no like ' %'或update property set memorial_no = trim(both ' ' from memorial_no) where memorial_no like ' %'btrim()⽅法为删除字符串两边的某类字符,可以同时指定多个,在上⾯为' ',意为空格trim()⽅法可以实现所有btrim()能实现的功能,事实上btrim()即为删除两边的某类字符串,trim()可以只指定某⼀边,当然也可以为两边 现把Postgresql的字符串操作函数罗列在以,以便⽇⽅使⽤函数:string || string 说明:String concatenation 字符串连接操作例⼦:'Post' || 'greSQL' = PostgreSQL函数:string || non-string or non-string || string说明:String concatenation with one non-string input 字符串与⾮字符串类型进⾏连接操作例⼦:'Value: ' || 42 = Value: 42函数:bit_length(string)说明:Number of bits in string 计算字符串的位数例⼦:bit_length('jose') = 32函数:char_length(string) or character_length(string)说明:Number of characters in string 计算字符串中字符个数例⼦:char_length('jose') = 4函数:lower(string)说明:Convert string to lower case 转换字符串为⼩写例⼦:bit_length('jose') = 32函数:octet_length(string)说明:Number of bytes in string 计算字符串的字节数例⼦:octet_length('jose') = 4函数:overlay(string placing string from int [for int])说明:Replace substring 替换字符串中任意长度的⼦字串为新字符串例⼦:overlay('Txxxxas' placing 'hom' from 2 for 4) = 4函数:position(substring in string)说明:Location of specified substring ⼦串在⼀字符串中的位置例⼦:position('om' in 'Thomas') = 3函数:substring(string [from int] [for int])说明:Extract substring 截取任意长度的⼦字符串例⼦:substring('Thomas' from 2 for 3) = hom函数:substring(string from pattern)说明:Extract substring matching POSIX regular expression. See Section 9.7 for more information on pattern matching. 利⽤正则表达式对⼀字符串进⾏任意长度的字串的截取例⼦:substring('Thomas' from '...$') = mas函数:substring(string from pattern for escape)说明:Extract substring matching SQL regular expression. See Section 9.7 for more information on pattern matching. 利于正则表达式对某类字符进⾏删除,以得到⼦字符串例⼦:trim(both 'x' from 'xTomxx') = Tom函数:trim([leading | trailing | both] [characters] from string)说明:Remove the longest string containing only the characters (a space by default) from the start/end/both ends of the string 去除尽可能长开始,结束或者两边的某类字符,默认为去除空⽩字符,当然可以⾃⼰指定,可同时指定多个要删除的字符串例⼦:trim(both 'x' from 'xTomxx') = Tom函数:upper(string)说明:Convert string to uppercase 将字符串转换为⼤写例⼦:upper('tom') = TOM函数:ascii(string)说明:ASCII code of the first character of the argument. For UTF8 returns the Unicode code point of the character. For other multibyte encodings. the argument must be a strictly ASCII character. 得到某⼀个字符的Assii值例⼦:ascii('x') = 120函数:btrim(string text [, characters text])说明:Remove the longest string consisting only of characters in characters (a space by default) from the start and end of string 去除字符串两边的所有指定的字符,可同时指定多个字符例⼦:btrim('xyxtrimyyx', 'xy') = trim函数:chr(int)说明:Character with the given code. For UTF8 the argument is treated as a Unicode code point. For other multibyte encodings the argument must designate a strictly ASCII character. The NULL (0) character is not allowed because text data types cannot store such bytes.得到某ACSII值对应的字符例⼦:chr(65) = A函数:convert(string bytea, src_encoding name, dest_encoding name)说明:Convert string to dest_encoding. The original encoding is specified by src_encoding. The string must be valid in this encoding. Conversions can be defined by CREATE CONVERSION. Also there are some predefined conversions. See Table 9-7 for available conversions. 转换字符串编码,指定源编码与⽬标编码例⼦:convert('text_in_utf8', 'UTF8', 'LATIN1') = text_in_utf8 represented in ISO 8859-1 encoding函数:convert_from(string bytea, src_encoding name)说明:Convert string to the database encoding. The original encoding is specified by src_encoding. The string must be valid in this encoding. 转换字符串编码,⾃⼰要指定源编码,⽬标编码默认为数据库指定编码,例⼦:convert_from('text_in_utf8', 'UTF8') = text_in_utf8 represented in the current database encoding函数:convert_to(string text, dest_encoding name)说明:Convert string to dest_encoding.转换字符串编码,源编码默认为数据库指定编码,⾃⼰要指定⽬标编码,例⼦:convert_to('some text', 'UTF8') = some text represented in the UTF8 encoding函数:decode(string text, type text)说明:Decode binary data from string previously encoded with encode. Parameter type is same as in encode. 对字符串按指定的类型进⾏解码例⼦:decode('MTIzAAE=', 'base64') = 123\000\001函数:encode(data bytea, type text)说明:Encode binary data to different representation. Supported types are: base64, hex, escape. Escape merely outputs null bytes as \000 and doubles backslashes. 与decode相反,对字符串按指定类型进⾏编码例⼦:encode(E'123\\000\\001', 'base64') = MTIzAAE=函数:initcap(string)说明:Convert the first letter of each word to uppercase and the rest to lowercase. Words are sequences of alphanumeric characters separated by non-alphanumeric characters. 将字符串所有的单词进⾏格式化,⾸字母⼤写,其它为⼩写例⼦:initcap('hi THOMAS') = Hi Thomas函数:length(string)说明:Number of characters in string 讲算字符串长度例⼦:length('jose') = 4函数:length(stringbytea, encoding name )说明:Number of characters in string in the given encoding. The string must be valid in this encoding. 计算字符串长度,指定字符串使⽤的编码例⼦:length('jose', 'UTF8') = 4函数:lpad(string text, length int [, fill text])说明:Fill up the string to length length by prepending the characters fill (a space by default). If the string is already longer than length then it is truncated (on the right). 对字符串左边进⾏某类字符⾃动填充,即不⾜某⼀长度,则在左边⾃动补上指定的字符串,直⾄达到指定长度,可同时指定多个⾃动填充的字符例⼦:lpad('hi', 5, 'xy') = xyxhi函数:ltrim(string text [, characters text])说明:Remove the longest string containing only characters from characters (a space by default) from the start of string 删除字符串左边某⼀些的字符,可以时指定多个要删除的字符例⼦:trim函数:md5(string)说明:Calculates the MD5 hash of string, returning the result in hexadecimal 将字符串进⾏md5编码例⼦:md5('abc') = 900150983cd24fb0 d6963f7d28e17f72函数:pg_client_encoding()说明:Current client encoding name 得到pg客户端编码例⼦:pg_client_encoding() = SQL_ASCII函数:quote_ident(string text)说明:Return the given string suitably quoted to be used as an identifier in an SQL statement string. Quotes are added only if necessary (i.e., if the string contains non-identifier characters or would be case-folded). Embedded quotes are properly doubled. 对某⼀字符串加上两引号例⼦:quote_ident('Foo bar') = "Foo bar"函数:quote_literal(string text)说明:Return the given string suitably quoted to be used as a string literal in an SQL statement string. Embedded single-quotes and backslashes are properly doubled. 对字符串⾥两边加上单引号,如果字符串⾥⾯出现sql编码的单个单引号,则会被表达成两个单引号例⼦:quote_literal('O\'Reilly') = 'O''Reilly'函数:quote_literal(value anyelement)说明:Coerce the given value to text and then quote it as a literal. Embedded single-quotes and backslashes are properly doubled. 将⼀数值转换为字符串,并为其两边加上单引号,如果数值中间出现了单引号,也会被表⽰成两个单引号例⼦:quote_literal(42.5) = '42.5'函数:regexp_matches(string text, pattern text [, flags text])说明:Return all captured substrings resulting from matching a POSIX regular expression against the string. See Section 9.7.3 for more information. 对字符串按正则表达式进⾏匹配,如果存在则会在结果数组中表⽰出来例⼦:regexp_matches('foobarbequebaz', '(bar)(beque)') = {bar,beque}函数:regexp_replace(string text, pattern text, replacement text [, flags text])说明:Replace substring(s) matching a POSIX regular expression. See Section 9.7.3 for more information. 利⽤正则表达式对字符串进⾏替换例⼦:regexp_replace('Thomas', '.[mN]a.', 'M') = ThM函数:regexp_split_to_array(string text, pattern text [, flags text ])说明:Split string using a POSIX regular expression as the delimiter. See Section 9.7.3 for more information. 利⽤正则表达式将字符串分割成数组例⼦:regexp_split_to_array('hello world', E'\\s+') = {hello,world}函数:regexp_split_to_table(string text, pattern text [, flags text])说明:Split string using a POSIX regular expression as the delimiter. See Section 9.7.3 for more information. 利⽤正则表达式将字符串分割成表格例⼦:regexp_split_to_table('hello world', E'\\s+') =helloworld(2 rows)函数:repeat(string text, number int)说明:Repeat string the specified number of times 重复字符串⼀指定次数例⼦:repeat('Pg', 4) = PgPgPgPg函数:replace(string text, from text, to text)说明:Replace all occurrences in string of substring from with substring to 将字符的某⼀⼦串替换成另⼀⼦串例⼦:('abcdefabcdef', 'cd', 'XX') = abXXefabXXef函数:rpad(string text, length int [, fill text])说明:Fill up the string to length length by appending the characters fill (a space by default). If the string is already longer than length then it is truncated. 对字符串进⾏填充,填充内容为指定的字符串例⼦:rpad('hi', 5, 'xy') = hixyx函数:rtrim(string text [, characters text])说明:Remove the longest string containing only characters from characters (a space by default) from the end of string去除字符串右边指定的字符例⼦:rtrim('trimxxxx', 'x') = trim函数:split_part(string text, delimiter text, field int)说明:Split string on delimiter and return the given field (counting from one) 对字符串按指定⼦串进⾏分割,并返回指定的数值位置的值例⼦:split_part(, , 2) = def函数:strpos(string, substring)说明:Location of specified substring (same as position(substring in string), but note the reversed argument order) 指定字符串在⽬标字符串的位置例⼦:strpos('high', 'ig') = 2函数:substr(string, from [, count])说明:Extract substring (same as substring(string from from for count)) 截取⼦串例⼦:substr('alphabet', 3, 2) = ph函数:to_ascii(string text [, encoding text])说明:Convert string to ASCII from another encoding (only supports conversion from LATIN1, LATIN2, LATIN9, and WIN1250 encodings)将字符串转换成ascii编码字符串例⼦:to_ascii('Karel') = Karel函数:to_hex(number int or bigint)说明:Convert number to its equivalent hexadecimal representation 对数值进⾏⼗六进制编码例⼦:to_hex(2147483647) = 7fffffff函数:translate(string text, from text, to text)说明:Any character in string that matches a character in the from set is replaced by the corresponding character in the to set 将字符串中某些匹配的字符替换成指定字符串,⽬标字符与源字符都可以同时指定多个例⼦:translate('12345', '14', 'ax') = a23x5。

常用PostgreSQL函数

常用PostgreSQL函数

常用PostgreSQL函数PostgreSQL常用函数介绍1.数学函数abs() 返回绝对值pi() 返回圆周率值sqrt() 返回非负数的二次方根mod(x,y) 返回x被y除(x/y)后的余数,x也可以为小数ceil(x) 或 ceiling(x) 返回不小于x最小整数值floor(x) 返回不大于x的最大整数值round(x) 返回最接近于x的整数round(x,y) 返回最接近于x的数,其值保留小数点后y位,若y 为负值,则保留小数点左边y位sign(x) x为负,零,正时返回结果依次为:-1,0,1pow(x,y) 或 power(x,y) 返回x的y次乘方的结果值exp(x) 返回e的x乘方后的值log(x) 返回x的自然对数radians(90) 将角度值90转变为弧度值1.5707...degrees(pi()) 将弧度值转变为角度值180sin()asin()cos()acos()tan()atan()cot()2.字符串函数char_length(str) 返回str包含字符的个数length(str) 返回字符串的字节长度,使用utf8编码时,一个汉字占三个字节,一个数字或字母占一个字节。

concat(s1,s2,...,sn) 把这些字符串连接起来。

当有null时忽略,当有任一二进制字符串,结果为一个二进制字符串。

concat_ws(x,s1,s2,...,sn) 第一个参数x是其他参数的分隔符。

left(s,n) 返回字符串s最左边n个字符right(s,n) 返回字符串s最右边n个字符lpad(s1,len,s2):返回长度len的s1字符串,若s1长于len,则截取前len个;否则,在左边填充s2至长度为len.rpad(s1,len,s2):返回长度len的s1字符串,若s1长于len,则截取前len个;否则,在右边填充s2至长度为len.ltrim(s): 字符串左边空格被删除rtrim(s):字符串右边空格被删除trim(s):字符串左右两边边空格被删除trim(s1 from s) : 删除字符串s两端所有的子字符串s1repeat(s,n): 返回重复n次s组成的字符串。

PostgreSQL教程(七):函数和操作符详解(3)

PostgreSQL教程(七):函数和操作符详解(3)

PostgreSQL教程(七):函数和操作符详解(3)九、序列操作函数:序列对象(也叫序列⽣成器)都是⽤CREATE SEQUENCE创建的特殊的单⾏表。

⼀个序列对象通常⽤于为⾏或者表⽣成唯⼀的标识符。

下⾯序列函数,为我们从序列对象中获取最新的序列值提供了简单和并发读取安全的⽅法。

函数返回类型描述nextval(regclass)bigint递增序列对象到它的下⼀个数值并且返回该值。

这个动作是⾃动完成的。

即使多个会话并发运⾏nextval,每个进程也会安全地收到⼀个唯⼀的序列值。

currval(regclass)bigint 在当前会话中返回最近⼀次nextval抓到的该序列的数值。

(如果在本会话中从未在该序列上调⽤过 nextval,那么会报告⼀个错误。

)请注意因为此函数返回⼀个会话范围的数值,⽽且也能给出⼀个可预计的结果,因此可以⽤于判断其它会话是否执⾏过nextval。

lastval()bigint 返回当前会话⾥最近⼀次nextval返回的数值。

这个函数等效于currval,只是它不⽤序列名为参数,它抓取当前会话⾥⾯最近⼀次nextval使⽤的序列。

如果当前会话还没有调⽤过nextval,那么调⽤lastval将会报错。

setval(regclass, bigint)bigint重置序列对象的计数器数值。

设置序列的last_value字段为指定数值并且将其is_called字段设置为true,表⽰下⼀次nextval将在返回数值之前递增该序列。

setval(regclass, bigint, boolean)bigint重置序列对象的计数器数值。

功能等同于上⾯的setval函数,只是is_called可以设置为true或false。

如果将其设置为false,那么下⼀次nextval将返回该数值,随后的nextval才开始递增该序列。

对于regclass参数,仅需⽤单引号括住序列名即可,因此它看上去就像⽂本常量。

postgres date 方法

postgres date 方法

postgres date 方法PostgreSQL中有许多用于处理日期和时间的内置函数和操作符。

下面我将介绍一些常用的日期方法:1. 获取当前日期和时间:使用now()函数可以获取当前日期和时间,例如:SELECT now();2. 提取日期部分:使用date_part()函数可以提取日期的特定部分,例如年、月、日等,例如:SELECT date_part('year', my_date_column) ASyear_part FROM my_table;3. 格式化日期:使用to_char()函数可以将日期格式化为特定的字符串,例如:SELECT to_char(my_date_column, 'YYYY-MM-DD') AS formatted_date FROM my_table;4. 计算日期差值:使用日期相减可以计算日期之间的差值,例如:SELECT date1 date2 AS date_diff FROM my_table;5. 添加或减去时间间隔:使用interval关键字可以添加或减去特定的时间间隔,例如:SELECT my_date_column + interval '1 day' AS new_date FROM my_table;6. 比较日期:使用比较操作符(如=、<、>等)可以比较日期,例如:SELECT FROM my_table WHERE my_date_column > '2022-01-01';这些是一些常用的日期方法,当然在实际应用中还有更多的方法可以处理日期和时间数据。

希望这些信息对你有所帮助。

PostgreSQL替换字符串方法及字符串操作函数

PostgreSQL替换字符串方法及字符串操作函数

函数返回类型描述例⼦结果string || string text字串连接'Post' || 'greSQL'PostgreSQL bit_length(string)int字串⾥⼆进制位的个数bit_length('jose')32char_length(string)int字串中的字符个数char_length('jose')4convert(string using conversion_name)text使⽤指定的转换名字改变编码。

convert('PostgreSQL' usingiso_8859_1_to_utf8)'PostgreSQL'lower(string)text把字串转化为⼩写lower('TOM')tom octet_length(string)int字串中的字节数octet_length('jose')4overlay(string placing string from int [for int])text替换⼦字串overlay('Txxxxas' placing 'hom'from 2 for 4)Thomasposition(substring instring)int指定的⼦字串的位置position('om' in 'Thomas')3 substring(string [fromint] [for int])text抽取⼦字串substring('Thomas' from 2 for 3)homsubstring(string from pattern)text抽取匹配 POSIX 正则表达式的⼦字串substring('Thomas' from '...$')massubstring(string from pattern for escape)text抽取匹配SQL正则表达式的⼦字串substring('Thomas' from'%#"o_a#"_' for '#')omatrim([leading | trailing |both] [characters] from string)text从字串string的开头/结尾/两边/ 删除只包含characters(缺省是⼀个空⽩)的最长的字串trim(both 'x' from 'xTomxx')Tomupper(string)text把字串转化为⼤写。

postgresql 的 函数

postgresql 的函数PostgreSQL是一种开源的关系型数据库管理系统,它提供了许多强大的函数来处理数据。

这些函数可以帮助用户更有效地管理和操作数据,从而提高数据库的性能和可靠性。

在本文中,我们将介绍一些常用的PostgreSQL函数及其用途。

1. COUNT函数COUNT函数用于计算指定列中的行数。

它可以用于统计表中的记录数,或者用于计算满足特定条件的记录数。

例如,以下查询将返回表中所有记录的数量:SELECT COUNT(*) FROM table_name;2. SUM函数SUM函数用于计算指定列中的值的总和。

它可以用于计算表中某一列的总和,或者用于计算满足特定条件的记录的总和。

例如,以下查询将返回表中所有记录的总和:SELECT SUM(column_name) FROM table_name;3. AVG函数AVG函数用于计算指定列中的值的平均值。

它可以用于计算表中某一列的平均值,或者用于计算满足特定条件的记录的平均值。

例如,以下查询将返回表中所有记录的平均值:SELECT AVG(column_name) FROM table_name;4. MAX函数MAX函数用于计算指定列中的最大值。

它可以用于计算表中某一列的最大值,或者用于计算满足特定条件的记录的最大值。

例如,以下查询将返回表中所有记录的最大值:SELECT MAX(column_name) FROM table_name;5. MIN函数MIN函数用于计算指定列中的最小值。

它可以用于计算表中某一列的最小值,或者用于计算满足特定条件的记录的最小值。

例如,以下查询将返回表中所有记录的最小值:SELECT MIN(column_name) FROM table_name;6. CONCAT函数CONCAT函数用于将两个或多个字符串连接在一起。

它可以用于将表中的两个或多个列连接在一起,或者用于将字符串与其他值连接在一起。

例如,以下查询将返回将两个列连接在一起的结果:SELECT CONCAT(column1, column2) FROM table_name;7. SUBSTRING函数SUBSTRING函数用于从字符串中提取子字符串。

pgsql函数语法

pgsql函数语法PostgreSQL (常被称为"pgsql") 是一种强大的开源对象-关系数据库管理系统。

除了SQL 标准语法外,PostgreSQL 还提供了大量的内置函数和操作符,用于处理各种数据操作和转换。

以下是PostgreSQL 中一些常见函数的概述和示例:1. 字符串函数:•length(string): 返回字符串的长度。

sql`SELECT length('PostgreSQL'); -- 返回10`* upper(string): 将字符串转换为大写。

sql`SELECT upper('hello'); -- 返回'HELLO'`* lower(string): 将字符串转换为小写。

sql`SELECT lower('HELLO'); -- 返回'hello'`2. 数值函数:•abs(number): 返回数的绝对值。

sql`SELECT abs(-10); -- 返回10`* ceiling(number): 返回大于或等于给定数的最小整数。

sql`SELECT ceiling(10.75); -- 返回11`* floor(number): 返回小于或等于给定数的最大整数。

sql`SELECT floor(10.25); -- 返回10`3. 日期和时间函数:•now(): 返回当前日期和时间。

sql`SELECT now();`* age(timestamp, timestamp): 返回两个时间戳之间的差异。

sql`SELECT age('2023-10-23 10:00:00', '2023-10-23 09:00:00');`4. 数组函数:•array_append(anyelement, array): 向数组添加一个元素。

sql`SELECT array_append(5, ARRAY[1,2,3]); -- 返回ARRAY[1,2,3,5]`5. 聚合函数: 如sum(), avg(), max(), min(), 和count() 等,常用于对查询结果进行统计。

postgresql数据类型转换,日期操作函数

postgresql数据类型转换,⽇期操作函数各种数据类型(⽇期/时间、integer、floating point和numeric)转换成格式化的字符串以及反过来从格式化的字符串转换成指定的数据类型。

下⾯列出了这些函数,它们都遵循⼀个公共的调⽤习惯:第⼀个参数是待格式化的值,⽽第⼆个是定义输出或输出格式的模板。

函数返回类型描述例⼦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把numeric转换成字串to_char(-125.8, '999D99S')to_date(text, text)date把字串转换成⽇期to_date('05 Dec 2000', 'DD Mon YYYY')to_timestamp(text, text)timestamp把字串转换成时间戳to_timestamp('05 Dec 2000', 'DD Mon YYYY')to_timestamp(double)timestamp把UNIX纪元转换成时间戳to_timestamp(200120400)to_number(text, text)numeric把字串转换成numeric to_number('12,454.8-', '99G999D9S')⽤于⽇期/时间格式化的模式:模式描述HH⼀天的⼩时数(01-12)HH12⼀天的⼩时数(01-12)HH24⼀天的⼩时数(00-23)MI分钟(00-59)SS秒(00-59)MS毫秒(000-999)US微秒(000000-999999)AM正午标识(⼤写)Y,YYY带逗号的年(4和更多位)YYYY年(4和更多位)YYY年的后三位YY年的后两位Y年的最后⼀位MONTH全长⼤写⽉份名(空⽩填充为9字符)Month全长混合⼤⼩写⽉份名(空⽩填充为9字符)month全长⼩写⽉份名(空⽩填充为9字符)MON⼤写缩写⽉份名(3字符)Mon缩写混合⼤⼩写⽉份名(3字符)mon⼩写缩写⽉份名(3字符)MM⽉份号(01-12)DAY全长⼤写⽇期名(空⽩填充为9字符)Day全长混合⼤⼩写⽇期名(空⽩填充为9字符)day全长⼩写⽇期名(空⽩填充为9字符)DY缩写⼤写⽇期名(3字符)Dy缩写混合⼤⼩写⽇期名(3字符)dy缩写⼩写⽇期名(3字符)DDD⼀年⾥的⽇⼦(001-366)DD⼀个⽉⾥的⽇⼦(01-31)D⼀周⾥的⽇⼦(1-7;周⽇是1)W⼀个⽉⾥的周数(1-5)(第⼀周从该⽉第⼀天开始)WW⼀年⾥的周数(1-53)(第⼀周从该年的第⼀天开始)下⾯是PostgreSQL中⽀持的时间/⽇期操作符的列表:。

PostgreSQL各数据类型的内置函数

PostgreSQL各数据类型的内置函数参考《PostgreSQL实战》3.1.2 数字类型操作符和数学函数PostgreSQL 支持数字类型操作符和丰富的数学函数例如支持加、减、乘、除、模取取余操作符SELECT 1+2, 2*3, 4/2, 8%3;按模取余SELECT mod(8,3);结果:2四舍五入函数:SELECT round(10.4) , round(10.5);结果:10, 11返回大于或等于给出参数的最小整数SELECT ceil(3.6) , ceil(-3.6);结果:4, -3返回小于或等于给出参数的最小整数floor(3.6) 结果:33.2.2 字符类型函数PostgreSQL 支持丰富的字符函数,下面举例说明计算字符串中的字符数select char_length('abcd');结果: 4计算字符串占用的字节数select octet_length('abcd');结果: 4指定字符在字符串中的位置(首次出现)select position('a' in 'abcda')结果: 1select position('x' in 'abcda')结果: 0提取字符串中的子串select substring('hello' from 3 for 4)结果: lloselect substring('hello' from 4 for 1)结果: l拆分字符串split_part (string text, delimiter text,field int )根据delimiter 分隔符拆分字符串string,并返回指定字段,字段从1 开始select split_part('abc@def1@nb', '@',1)结果: abc3.3.2 时间/日期类型操作符时间、日期数据类型支持的操作符有加、减、乘、除,日期相加select date '2017-07-29' + interval'1days'结果: 2017-07-30 00:00:00日期相减select date '2017-07-29' - interval'1hour'结果: 2017-07-28 23:00:00日期相乘select 100* interval '1 second'结果: 00:01:40日期相除select interval '1 hour' / doubleprecision '3'结果: 00:20:003.3.3 时间/日期类型常用函数显示当前时间select current_date, current_time;结果: 2019-11-23,20:20:55.635115+08另一个非常重要的函数为 EXTRACT 函数,可以从日期、时间数据类型中抽取年、月、日、时、分、秒信息EXTRACT(field FROM source)field 值可以为century、year、month、day、hour 、minute、 second等, source类型为timestamp、 time、 interval的值的表达式。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
相关文档
最新文档