String Concatenation + Replace - sql

I need to remove accentes, spaces and special characters in Oracle SQL.
(I thought about using pipe and replace)
My code so far:
SELECT NR_CONHEC || ' - ' || RAZAO_SOCIAL_TRANSP FROM geq_gl_ctms_frete
For example:
7590 - J. T. TRANSPORTES LTDA. - ME
Needed output:
7590-JTTRANSPORTESLTDA-ME

You can try
select regexp_replace('7590 - J. T. TRANSPORTES LTDA. - ME','[[:space:]]|\.')
as "Result String"
from dual;
Result String
-------------------------
7590-JTTRANSPORTESLTDA-ME
For your case, replace with the following query :
SELECT regexp_replace( NR_CONHEC || ' - ' || RAZAO_SOCIAL_TRANSP,'[[:space:]]|\.')
FROM geq_gl_ctms_frete;
and if you want to remove more special chars. and accentes other than dot, add whichever you want after |\. such as |\%|\&|\è..., then those characters %, & or è are removed from the string.

this will work:
SELECT NR_CONHEC||'-'|| reexp_replace(regexp_replace(RAZAO_SOCIAL_TRANSP
RAZAO_SOCIAL_TRANSP,'.',''),' ','') FROM geq_gl_ctms_frete;

How about this?
SQL> with test (col) as
2 (select '7590 - J. T. TRANSPORTES LTDA. - ME' from dual)
3 select regexp_replace(col, '[^([:alpha:][:digit:]-)]') result
4 from test;
RESULT
-------------------------
7590-JTTRANSPORTESLTDA-ME
SQL>
I.e. replace anything that isn't a letter, a number or a hyphen with nothing.

try:
select regexp_replace (NR_CONHEC|| ' - ' || RAZAO_SOCIAL_TRANSP, '[^-0-9a-zA-Z]') as rez from geq_gl_ctms_frete ;

Related

Oracle Regexp_substr String

I have String like a123bcd-e2343fg-hij-dfgh
and I want OUTPUT e2343fg-hij-dfgh using Regular_expression in oracle.
select regexp_substr('abcd-efg-hij','-[^-]+'1) from dual;
You may apply regexp_substr with [^-]+[-^] pattern and then ltrim as :
select ltrim('a123bcd-e2343fg-hij-dfgh',
regexp_substr('a123bcd-e2343fg-hij-dfgh','[^-]+[-^]')) as output_string
from dual;
or better to call with bind variable :
select ltrim('&str', regexp_substr('&str','[^-]+[-^]')) as output_string
from dual;
where &str may be replaced with a123bcd-e2343fg-hij-dfgh after prompted.
Rextester Demo
Why regular expression, when a trivial SUBSTR + INSTR does the job nicely & quickly? True, it will look smarter, but I can't see any other benefit.
SQL> with test (col) as
2 (select 'a123bcd-e2343fg-hij-dfgh' from dual)
3 select substr(col, instr(col, '-') + 1) result
4 from test;
RESULT
----------------
e2343fg-hij-dfgh
SQL>
select substr('abcd-efg-hij',
regexp_instr('abcd-efg-hij','-[^-]+')+1,length('abcd-efg-hij'))
from dual;
try this
For the sake of argument, regexp_replace works too. This regex matches anything up to and including the first dash, and remembers the rest which it returns.
with tbl(str) as (
select 'a123bcd-e2343fg-hij-dfgh' from dual
)
select regexp_replace(str, '^.*?-(.*)', '\1')
from tbl;
Keep in mind if regexp_substr() does not find a match, it returns NULL but if regexp_replace() does not find a match it return the original string.

Oracle SQL 12.1 removing a changing substring from a string

I have the following example
05.04.2018 at 11:10:37 AEST
My goal is to remove all alpha chars from the string.
The expected result is to remove the ' at' and ' AEST' sub-strings:
Note: There should be only one space between the date and the time. There should be no space at the end of the string
05.04.2018 11:10:37
The 'AEST' sub-string is a timezone and can change.
This is my current SQL query:
select SUBSTR(REGEXP_REPLACE(REGEXP_REPLACE('05.04.2018 at 11:10:37 AEST',' at',''), ' EST| AEDT| AEST', ''),1) from dual;
I'm looking to enhance my query (preferably using regex) so I will not have to specify explicitly all potential values for timezone (as currently being done in the query)
Thanks
You may use \s*[a-zA-Z]+ / \s*[[:alpha:]]+ regex:
select REGEXP_REPLACE('05.04.2018 at 11:10:37 AEST','\s*[a-zA-Z]+','') as Result from dual
The pattern matches
\s* - 0+ whitespace chars
[a-zA-Z]+ - 1+ ASCII letters ([[:alpha:]]+ will match any letters).
See an online Oracle demo. Output:
Something like this?
SQL> with test as (select '05.04.2018 at 11:10:37 AEST' col from dual)
2 select regexp_replace(col, '\s*[[:alpha:]]+') result
3 from test;
RESULT
-------------------
05.04.2018 11:10:37
SQL>
You can use:
select trim(regexp_replace(col, '[a-zA-Z]', ''))
I assume you want to remove the final space as well.
Keep it simple! Why not without regexp? The date part and the time part are always at the same position.
select substr(col,1,10) -- the date part
||' '|| -- the blank
substr(col,15,8) -- the time part
from tab;
e.g.
SQL> select substr(col,1,10)
||' '||
substr(col,15,8) "date+time"
from (
select '05.04.2018 at 11:10:37 AEST' col
from dual) tab;
date+time
-------------------
05.04.2018 11:10:37

Replace all occurrences of more than one whilespace character with a single one

Input:
Rammar Narayana raja Rani.
Output:
Rammar Narayana raja Rani.
In C# code I oculd do this:
while (name.Contains(" "))
{
name = name.Replace(" ", " ");
}
That replaces the double with single spaces and that should store in same variable. But here I want to replace more than one whitespace in string to one whitespace, for all occurance. How can I do that in Oracle SQL?
Try this way:
SELECT REGEXP_REPLACE('aa b cc d e f ', '( ){2,}', ' ')
FROM DUAL;
For details, see here; it's one of the examples.
SELECT REGEXP_REPLACE ( 'Rammar Narayana raja Rani.', ' +', ' ' )
FROM DUAL;
or if you want to replace any white spaces then:
SELECT REGEXP_REPLACE ( 'Rammar Narayana raja Rani.', '\s+', ' ' )
FROM DUAL;

How to show leading/trailing whitespace in a PostgreSQL column?

I can't see leading/trailing whitespace in the following following SQL statement executed with psql:
select name from my_table;
Is there a pragmatic way to see leading/trailing whitespace?
One of option is to use format() function.
With given query case: select format( '"%s"', name ) from my_table;
PoC:
SELECT format( '"%s"', name )
FROM ( VALUES ( ' a ' ), ( ' b ' ) ) v(name);
format
--------
" a "
" b "
(2 rows)
Turn off "aligned mode" in psql: \a
\a
select * from my_table;
id|col1|col2
12|foo|bar
I'd append surrounding quotes:
select '"' || name || '"' from my_table;
If you don't mind substituting all whitespace characters whether or not they are leading/trailing, something like the following will do it:
SELECT REPLACE(REPLACE(REPLACE(REPLACE(txt, ' ', '_'),
E'\t', '\t'),
E'\r', '\r'),
E'\n', '\n') AS txt
FROM test;
This is using an underscore to mark the spaces but of course you are free to choose your own. See SQL fiddle demo.
If you strictly only want to show up the leading/trailing ones it will get more complex - but if this is really desired, something may be possible using regex_replace.
You can try this:
select name from my_table
where name like '% '

Trim Whitespaces (New Line and Tab space) in a String in Oracle

I need to trim New Line (Chr(13) and Chr(10) and Tab space from the beginning and end of a String) in an Oracle query. I learnt that there is no easy way to trim multiple characters in Oracle. "trim" function trims only single character. It would be a performance degradation if i call trim function recursivelly in a loop using a function. I heard regexp_replace can match the whitespaces and remove them.
Can you guide of a reliable way to use regexp_replace to trim multiple tabspaces or new lines or combinations of them in beginning and end of a String. If there is any other way, Please guide me.
If you have Oracle 10g, REGEXP_REPLACE is pretty flexible.
Using the following string as a test:
chr(9) || 'Q qwer' || chr(9) || chr(10) ||
chr(13) || 'qwerqwer qwerty' || chr(9) ||
chr(10) || chr(13)
The [[:space:]] will remove all whitespace, and the ([[:cntrl:]])|(^\t) regexp will remove non-printing characters and tabs.
select
tester,
regexp_replace(tester, '(^[[:space:]]+)|([[:space:]]+$)',null)
regexp_tester_1,
regexp_replace(tester, '(^[[:cntrl:]^\t]+)|([[:cntrl:]^\t]+$)',null)
regexp_tester_2
from
(
select
chr(9) || 'Q qwer' || chr(9) || chr(10) ||
chr(13) || 'qwerqwer qwerty' || chr(9) ||
chr(10) || chr(13) tester
from
dual
)
Returning:
REGEXP_TESTER_1: "Qqwerqwerqwerqwerty"
REGEXP_TESTER_2: "Q qwerqwerqwer qwerty"
Hope this is of some use.
This how I would implement it:
REGEXP_REPLACE(text,'(^[[:space:]]*|[[:space:]]*$)')
How about the quick and dirty translate function?
This will remove all occurrences of each character in string1:
SELECT translate(
translate(
translate(string1, CHR(10), '')
, CHR(13), '')
, CHR(09), '') as massaged
FROM BLAH;
Regexp_replace is an option, but you may see a performance hit depending on how complex your expression is.
You could use both LTRIM and RTRIM.
select rtrim(ltrim('abcdab','ab'),'ab') from dual;
If you want to trim CHR(13) only when it comes with a CHR(10) it gets more complicated. Firstly, translated the combined string to a single character. Then LTRIM/RTRIM that character, then replace the single character back to the combined string.
select replace(rtrim(ltrim(replace('abccccabcccaab','ab','#'),'#'),'#'),'#','ab') from dual;
TRANSLATE (column_name, 'd'||CHR(10)||CHR(13), 'd')
The 'd' is a dummy character, because translate does not work if the 3rd parameter is null.
For what version of Oracle? 10g+ supports regexes - see this thread on the OTN Discussion forum for how to use REGEXP_REPLACE to change non-printable characters into ''.
I know this is not a strict answer for this question, but I've been working in several scenarios where you need to transform text data following these rules:
No spaces or ctrl chars at the beginning of the string
No spaces or ctrl chars at the end of the string
Multiple ocurrencies of spaces or ctrl chars will be replaced to a single space
Code below follow the rules detailed above:
WITH test_view AS (
SELECT CHR(9) || 'Q qwer' || CHR(9) || CHR(10) ||
CHR(13) || ' qwerqwer qwerty ' || CHR(9) ||
CHR(10) || CHR(13) str
FROM DUAL
) SELECT
str original
,TRIM(REGEXP_REPLACE(str, '([[:space:]]{2,}|[[:cntrl:]])', ' ')) fixed
FROM test_view;
ORIGINAL FIXED
---------------------- ----------------------
Q qwer Q qwer qwerqwer qwerty
qwerqwer qwerty
1 row selected.
If at all anyone is looking to convert data in 1 variable that lies in 2 or 3 different lines like below
'Data1
Data2'
And you want to display data as 'Data1 Data2' then use below
select TRANSLATE ('Data1
Data2', ''||CHR(10), ' ') from dual;
it took me hrs to get the right output. Thanks to me I just saved you 1 or 2 hrs :)
In cases where the Oracle solution seems overly convoluted, I create a java class with static methods and then install it as a package in Oracle. This might not be as performant, but you will eventually find other cases (date conversion to milliseconds for example) where you will find the java fallback helpful.
Below code can be used to Remove New Line and Table Space in text column
Select replace(replace(TEXT,char(10),''),char(13),'')
Try the code below.
It will work if you enter multiple lines in a single column.
create table products (prod_id number , prod_desc varchar2(50));
insert into products values(1,'test first
test second
test third');
select replace(replace(prod_desc,chr(10),' '),chr(13),' ') from products where prod_id=2;
Output :test first test second test third
TRIM(BOTH chr(13)||chr(10)||' ' FROM str)
Instead of using regexp_replace multiple time use (\s) as given below;
SELECT regexp_replace('TEXT','(\s)','')
FROM dual;
Fowloing code remove newline from both side of string:
select ltrim(rtrim('asbda'||CHR(10)||CHR(13) ,''||CHR(10)||CHR(13)),''||CHR(10)||CHR(13)) from dual
but in most cases this one is just enought :
select rtrim('asbda'||CHR(10)||CHR(13) ,''||CHR(10)||CHR(13))) from dual
UPDATE My_Table
SET Mycolumn1 =
TRIM (
TRANSLATE (Mycolumn1,
CHR (10) || CHR (11) || CHR (13),
' '))
WHERE ( INSTR (Mucolumn1, CHR (13)) > 0
OR INSTR (Mucolumn1, CHR (10)) > 0
OR INSTR (Mucolumn1, CHR (11)) > 0);