溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

replace與translate怎么在pg中使用

發(fā)布時(shí)間:2021-01-27 13:50:48 來源:億速云 閱讀:325 作者:Leah 欄目:開發(fā)技術(shù)

本篇文章給大家分享的是有關(guān)replace與translate怎么在pg中使用,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

1.首先創(chuàng)建students表

CREATE TABLE students
(
 id integer NOT NULL,
 name character varying(255),
 sex character varying(255),
 class character varying(255),
 "like" character varying(255),
 school character varying(255),
 phone character varying(255)
)

2.插入數(shù)據(jù)

INSERT INTO "public"."students" ("id", "name", "sex", "class", "like", "school", "phone") 
VALUES ('1', '大貓', '女', '一年級', '繪畫', '第三小學(xué)', '2345');
INSERT INTO "public"."students" ("id", "name", "sex", "class", "like", "school", "phone") 
VALUES ('2', '小厭', '男', '三年級', '書法', '第四小學(xué)', '2346');
INSERT INTO "public"."students" ("id", "name", "sex", "class", "like", "school", "phone") 
VALUES ('3', '庫庫', '女', '二年級', '繪畫', '第三小學(xué)', '2342');
INSERT INTO "public"."students" ("id", "name", "sex", "class", "like", "school", "phone") 
VALUES ('4', '艾琳', '女', '四年級', '書法,鋼琴', '第四小學(xué)', '2349');

結(jié)果:

select * from students

如下圖:

replace與translate怎么在pg中使用

3.replace 的用法

replace(string text, from text, to text)

返回類型:text

解釋:把字串string里出現(xiàn)地所有子字串from替換成子字串to

示例1:

select replace('一條黑色的狗','黑','黑白相間')

結(jié)果:一條黑色的狗 變成了 一條黑白相間色的狗

如下圖:

replace與translate怎么在pg中使用

示例2:

update students set name=replace(name,'大貓','小貓咪的姐姐')

結(jié)果:name為 ‘大貓'的這條數(shù)據(jù)name='小貓咪的姐姐'

replace與translate怎么在pg中使用

示例3:

select * from students where school='第四小學(xué)' ORDER BY replace(name,'艾琳','1')

結(jié)果:

replace與translate怎么在pg中使用

4.translate的用法

translate(string text, from text, to text)

返回類型:text

解釋:把在string中包含的任何匹配from中的字符的字符轉(zhuǎn)化為對應(yīng)的在to中的字符。

示例1:

select translate('她真是好看', '好看','漂亮')

結(jié)果:

replace與translate怎么在pg中使用

示例2:

select * from students where phone like '2%' 
ORDER BY translate(class, '一二三四','1234')

結(jié)果:

replace與translate怎么在pg中使用

示例3:

select * from students where phone like '2%' 
ORDER BY translate(name, '庫小厭貓咪艾','1234')

結(jié)果

replace與translate怎么在pg中使用

結(jié)論:

補(bǔ)充:pg中position、split_part、translate、strpos、length函數(shù)

select position('.' in '1.1.2.10');
select split_part('1.1.2.10','.',length('1.1.2.10') - length(translate('1.1.2.10','.',''))+1);
select split_part('1.1.2','.',length('1.1.2') - length(translate('1.1.2','.',''))+1);
select length(translate('1.1.2.10','.','a'))+1 as num
select translate('1.1.2.10','.','')
select strpos('1.1.2.10','.')
select instr('1.1.2.10','.',1,3) 
select length('1.1.2.10') - length(translate('1.1.2.10','.',''))

以上就是replace與translate怎么在pg中使用,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI