Как удалить '\ t', '\ n' или лишние пробелы из строки в Postgres? - PullRequest
0 голосов
/ 23 апреля 2020

У меня есть таблица в Postgres со следующим столбцом:

col1
     The study was terminated early by the sponsor on 13 January 2014 due to a decision to modify     the drug development plan.   
     Due to positive preliminary results from other palifermin studies.   
     Asset terminated by PIB   
     Inconsistent training status of sniffer dogs   
     This study was terminated early due to poor recruitment   
     The study was terminated due to lack of recruitment.   
     The scientific director decided to terminate: low priority study with slow accrual   
     See Termination Reason in Detailed Description.   
     Investigator moved to new institution   
     This study was terminated for administrative reasons   
     The app was not completed in time to conduct a clinical trial on it within the funding grant's     award period 

В строке есть начальные и запаздывающие пробелы и между ними '\ n' или '\ t'. Я попробовал следующие запросы, но, похоже, ничего не получается.

select btrim(col1, '\s') from table;

update table
SET col1 = upper(substring(REGEXP_REPLACE(col1, '(\s+)', '') from 1 for 1)) || lower(substring(REGEXP_REPLACE(why_stopped, '(\s+)', '') from 2));

update table
set col1= regexp_replace(col1, E'[\\n\\r\\f\\u000B\\u0085\\u2028\\u2029]+', ' ', 'g' );

select distinct replace( replace( replace( col1, E'\n', '\n' ), E'\t', '\t' ), E'\r', '\r' )
from table;

Любое предложение будет очень полезно здесь.

Ответы [ 3 ]

1 голос
/ 23 апреля 2020

Чтобы использовать экранирование backsla sh в строковых литералах, вам необходимо добавить к ним E; см. документацию .

Так что попробуйте

btrim(col1, E' \t\n')
0 голосов
/ 23 апреля 2020

Я решил проблему следующим образом:

regexp_replace (why_stopped, '\ s {2,}', '', 'g')

0 голосов
/ 23 апреля 2020

вы можете использовать функцию trim () SQL Строковые функции и операторы

TRIM([LEADING | TRAILING | BOTH] [characters] FROM string)

пример вы можете найти здесь:

postgresql трим-функция функция триммера

...