PostgreSQL-字符串函数

字符串连接

SELECT 'A'||'B';  字符串连接,适用于字符串与数字连接

SELECT CONCAT(10,'a','hello',20.0);  连接所有参数,个数不限,类型不限

字母转换

SELECT LOWER('ABCD');  将字符转换成小写

SELECT UPPER('ABCD');  将字符转换成大写

SELECT INITCAP('abcd');  将字符串中每个单词的首字母大写

字符串替换

SELECT REPLACE('aaaxxx','a','_');  替换字符串:SELECT REPLACE('aaaxxx','a','_');

SELECT TRIM('  asd ');  删除头尾部空格 → TRIM(BOTH '   asd  ')

SELECT TRIM(LEADING '  asd '),LENGTH(TRIM(LEADING '  asd ')); 删除头部空格

SELECT TRIM(TRAILING '  asd '),LENGTH(TRIM(TRAILING '  asd ')); 删除尾部空格

SELECT TRIM('aaahelloworldaaa','a');  可以指定删除字符,默认删除空格

字符串查找与截取

SELECT POSITION('lo' in 'hello');  给定子字符串在字符串的位置,查询不到时返回0。

SELECT SUBSTRING('hello world' FROM 3 FOR 10);  截取字符串,从from位置截取长度for

SELECT LENGTH('ABCD');  字符串中字符的个数

SELECT LEFT('HELLO',2);  返回字符串前n个字符,n为负数时返回除最后|n|个字符以外的所有字符

SELECT RIGHT('HELLO',2);  返回字符串后n个字符,n为负数时返回除最前|n|个字符意外的所有字符

strpos(string, substring);例如:strpos('high','ig'),返回2

字符串类型转换

SELECT CAST(10.0 AS text); 

SELECT CAST(salary AS int) FROM company3;  类型转换

字符串判断null

COALESCE(b.price, 其它值)

字符串前缀填充

select lpad(‘256’, 6, ‘0’) //256不满6位数时,在左边补0,最终达到6位长度

字符串后缀填充

select rpad(‘256’, 6, ‘0’) //256不满6位数时,在右边补0,最终达到6位长度

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

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

函数: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+') = 
hello
world
(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('abc~@~def~@~ghi', '~@~', 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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/14187.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

项目顺利交付,几个关键阶段

年前离放假还有10天的时候,来了一个应急项目, 需要在放假前一天完成一个演示版本的项目,过年期间给甲方领导看。 本想的最后几天摸摸鱼,这么一来,非但摸鱼不了,还得加班。 还在虽然累,但也是…

昇思打卡营第五期(MindNLP特辑)番外:硅基流动 x 华为云DeepSeek V3 API推理MindTinyRAG

1.前言 前脚,DeepSeek面临的巨头企业官宣加入vs多国政府下场质疑的冰火两重天局势尚未平静(DeepSeek在美两重天:五大巨头接入,政府诚惶诚恐);后脚,OpenAI被逼急,凌晨亮出全新推理…

MYSQL索引与视图

一、新建数据库 mysql> create database mydb15_indexstu; mysql> use mydb15_indexstu; 二、新建表 (1)学生表Student mysql> create table Student(-> Sno int primary key auto_increment,-> Sname varchar(30) not null unique,-…

使用java代码操作rabbitMQ收发消息

SpringAMQP 将来我们开发业务功能的时候,肯定不会在控制台收发消息,而是应该基于编程的方式。由于RabbitMQ采用了AMQP协议,因此它具备跨语言的特性。任何语言只要遵循AMQP协议收发消息,都可以与RabbitMQ交互。并且RabbitMQ官方也…

介绍10个比较优秀好用的Qt相关的开源库

记录下比较好用的一些开源库 1. Qt中的日志库“log4qt” log4qt 是一个基于 Apache Log4j 设计理念的 Qt 日志记录库,它为 Qt 应用程序提供了强大而灵活的日志记录功能。Log4j 是 Java 领域广泛使用的日志框架,log4qt 借鉴了其优秀的设计思想&#xff…

【远程控制】安装虚拟显示器

todesk远程发现没显示器的机器有问题 电脑如果不外接一个显示器那么会默认为1024 768 分辨率需要安装虚拟显示器参考 竟然是一个隐私屏幕的解决方案。 虚拟显示器 Parsec-vdd 项目地址 Parsec-vdd 最大的优点是:支持 4K 高刷、可添加多个虚拟屏、 H-Cursor&#…

嵌入式面试题 C/C++常见面试题整理_7

一.什么函数不能声明为虚函数? 常见的不能声明为虚函数的有:普通函数(非成员函数):静态成员函数;内联成员函数;构造函数;友元函数。 1.为什么C不支持普通函数为虚函数?普通函数(非成员函数)只能被overload,不能被override,声明为虚函数也没有什么意思…

赛博算命之 ”梅花易数“ 的 “JAVA“ 实现 ——从玄学到科学的探索

hello~朋友们!好久不见! 今天给大家带来赛博算命第三期——梅花易数的java实现 赛博算命系列文章: 周易六十四卦 掐指一算——小六壬 更多优质文章:个人主页 JAVA系列:JAVA 大佬们互三哦~互三必回!&#xf…

UNI-MOL: A UNIVERSAL 3D MOLECULAR REPRESENTATION LEARNING FRAMEWORK

UNI-MOL: A UNIVERSAL 3D MOLECULAR REPRESENTATION LEARNING FRAMEWORK Neurips23 推荐指数:#paper/⭐⭐⭐#​(工作量不小) 动机 在大多数分子表征学习方法中,分子被视为 1D 顺序标记或2D 拓扑图,这限制了它们为下游任务整合…

【DeepSeek论文精读】3. DeepSeekMoE:迈向混合专家语言模型的终极专业化

欢迎关注[【AIGC论文精读】](https://blog.csdn.net/youcans/category_12321605.html)原创作品 【DeepSeek论文精读】1. 从 DeepSeek LLM 到 DeepSeek R1 【DeepSeek论文精读】2. DeepSeek LLM:以长期主义扩展开源语言模型 【DeepSeek论文精读】3. DeepS…

AI 编程工具—Cursor 进阶篇 文章改写生成整理爬取

AI 编程工具—Cursor 进阶篇 文章改写生成整理爬取 其实对做自媒体的人而言,整理素材其实是一件非常耗时的事情,今天我们来看一下如何使用Cursor来帮我们解决这些问题,首先我们要建一个单独的项目,因为这个项目不涉及任何代码操作,只是文字相关的事情,还有就是这个项目需…

【Android开发AI实战】基于CNN混合YOLOV实现多车牌颜色区分且针对车牌进行矫正识别(含源码)

文章目录 引言单层卷积神经网络(Single-layer CNN)📌 单层 CNN 的基本结构📌 单层 CNN 计算流程图像 透视变换矫正车牌c实现🪄关键代码实现:🪄crnn结构图 使用jni实现高级Android开发&#x1f3…

LSSVM最小二乘支持向量机多变量多步光伏功率预测(Matlab)

代码下载:LSSVM最小二乘支持向量机多变量多步光伏功率预测(Matlab) LSSVM最小二乘支持向量机多变量多步光伏功率预测 一、引言 1.1、研究背景与意义 随着全球能源危机和环境问题的日益严重,可再生能源的开发利用成为了世界各国…

设计模式Python版 代理模式

文章目录 前言一、代理模式二、代理模式示例三、远程代理四、虚拟代理五、虚拟代理示例 前言 GOF设计模式分三大类: 创建型模式:关注对象的创建过程,包括单例模式、简单工厂模式、工厂方法模式、抽象工厂模式、原型模式和建造者模式。结构型…

自动化测试(selenium篇)

🍅 点击文末小卡片,免费获取软件测试全套资料,资料在手,涨薪更快 一、什么是自动化测试 自动化测试通俗一些来讲,就是相当于将人工测试手段进行转换,让代码去自动执行。 自动化测试主要分为:单元…

【大模型】DeepSeek与chatGPT的区别以及自身的优势

目录 一、前言二、核心技术对比2.1 模型架构设计2.1.1 ChatGPT的Transformer架构2.1.2 DeepSeek的混合架构 2.2 训练数据体系2.2.1 ChatGPT的数据特征2.2.2 DeepSeek的数据策略 三、应用场景对比3.1 通用场景表现3.1.1 ChatGPT的强项领域3.2.2 DeepSeek的专项突破 3.3 响应效率…

RK3568平台开发系列讲解(ConfigFS篇)ConfigFS核心数据结构

🚀返回专栏总目录 文章目录 一、数据结构二、结构体关系三、案例3.1、configfs_subsystem 实例3.2、config_group 实例化四、属性和方法五、config_item实例化沉淀、分享、成长,让自己和他人都能有所收获!😄 理解 ConfigFS 的核心数据结构对于深入使用和定制 ConfigFS 非…

Spring Boot Web 入门

目录 Spring Boot Web 是 Spring Boot 框架的一个重要模块,它简化了基于 Spring 的 Web 应用程序的开发过程。以下是一个 Spring Boot Web 项目的入门指南,涵盖了项目创建、代码编写、运行等关键步骤。 1. 项目创建 使用 Spring Initializr 使用 IDE …

网络工程师 (22)网络协议

前言 网络协议是计算机网络中进行数据交换而建立的规则、标准或约定的集合,它规定了通信时信息必须采用的格式和这些格式的意义。 一、基本要素 语法:规定信息格式,包括数据及控制信息的格式、编码及信号电平等。这是协议的基础,确…

【AI】在Ubuntu中使用docker对DeepSeek的部署与使用

这篇文章前言是我基于部署好的deepseek-r1:8b模型跑出来的 关于部署DeepSeek的前言与介绍 在当今快速发展的技术环境中,有效地利用机器学习工具来解决问题变得越来越重要。今天,我将引入一个名为DeepSeek 的工具,它作为一种强大的搜索引擎&a…