postgresql使用过程中字段转换

这篇博客详细介绍了在PostgreSQL中处理字符串和时间的多种函数,包括interval类型的转换、时间差计算、字符串连接、位数计算、字符计数等。通过实例展示了如concatenation、substring、lower/upper、位数计算、字符串替换和正则表达式等功能的用法,为数据库操作提供了实用参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、interval类型字段换算成时分秒

date_part('hour', visit_times)*60*60+date_part('minute', visit_times)*60+date_part('seconds', visit_times)

2、计算时间差

to_timestamp(max(ddd),'HH24:MI:SS') -to_timestamp(min(ddd),'HH24:MI:SS')

3、查询jsonb字段中的key对应的值

(detail_jsonb->>'date')::varchar
--detail_jsonb为jsonb类型的字段名,date为其中的key

4、update效率的问题

update table1 set column1=(select column1 from table2) where table2.column2=table1.column2;
update table1 set table1.column1=table2.column1 from (select column1 from table2) where table2.column2=table1.column2;

遇到以上情况,优选第二种方式进行update操作。

5、自增序列取值

nextval('table_seq')


6、和字符串处理相关的函数

函数:string || string 
说明:String concatenation 字符串连接操作
例子:select 'Post' || 'greSQL'; = PostgreSQL

 

函数:string || non-string or non-string || string
说明:String concatenation with one non-string input 字符串与非字符串类型进行连接操作
例子:select 'Value: ' || 42; = Value: 42

 

函数:bit_length(string)
说明:Number of bits in string 计算字符串的位数
例子:select bit_length('pmars') = 40

 

函数:char_length(string) or character_length(string)
说明:Number of characters in string 计算字符串中字符个数
例子:select char_length('pmars'); = 5

 

函数:lower(string)
说明:Convert string to lower case 转换字符串为小写
例子:select lower('PmArS'); = "pmars"

 

函数:octet_length(string)
说明:Number of bytes in string 计算字符串的字节数
例子:select octet_length('我是pmars'); = 11  select octet_length('我');  = 3


函数:overlay(string placing string from int [for int])
说明:Replace substring 替换字符串中任意长度的子字串为新字符串
例子:select overlay('I am pmars' placing 'ming' from 6 for 5); = "I am ming"


函数:position(substring in string)
说明:Location of specified substring 子串在一字符串中的位置
例子:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值