Given the following sample statement, I simply can't get rid of the leading and trailing white spaces no matter if using a combination of RTRIM() and LTRIM() or REGEXP_REPLACE():
select
test_column
,length(test_column) len
,regexp_replace(test_column, '(^[[:blank:]]+)|([[:blank:]]+$)','') rxp
,length(regexp_replace(test_column, '(^[[:blank:]]+)|([[:blank:]]+$)','')) len_rxp --22 characters expected, but is 26
,rtrim(ltrim(test_column)) rltrim
,length(rtrim(ltrim(test_column))) len_rltrim --22 characters expected, but is 26
from(
select ' ABCDEF Hijklmnopqr S32 ' test_column --22 characters without and 29 including whitespaces
from dual);
What's the matter?
You can use the following:
select regexp_replace(test_column, '^(\t|\s)*(.*)(\t|\s)*$', '\2')
from (
select ' ABCDEF Hijklmnopqr S32 ' test_column
from dual
);
This should divide your string in 3 parts (leading, meaningful text, ending) and return only the second one, thus cutting away the trailing and ending sequences of spaces and tabs
Related
Like if I have a string "123456,852369,7852159,1596357"
The out put looking for "1234,8523,7852,1596"
Requirement is....we want to collect 4 char after every ',' separator
like split, substring and again concat
select
REGEXP_REPLACE('MEDA,MEDA,MEDA,MEDA,MEDA,MEDA,MEDA,MEDA,MDCB,MDCB,MDCB,MDCB,MDCB,MDCB', '([^,]+)(,\1)+', '\1')
from dual;
we want to collect 4 char after every ',' separator
Here is an approach using regexp_replace:
select regexp_replace(
'123456,852369,7852159,1596357',
'([^,]{4})[^,]*(,|$)',
'\1\2'
)
from dual
Regexp breakdown:
([^,]{4}) 4 characters others than "," (capture that group as \1)
[^,]* 0 to n characters other than "," (no capture)
(,|$) either character "," or the end of string (capture this as \2)
The function replaces each match with capture 1 (the 4 characters we want) followed by capture 2 (the separator, if there is one).
Demo:
RESULT
1234,8523,7852,1596
One option might be to split the string, extract 4 characters and aggregate them back:
SQL> with test (col) as
2 (select '123456,852369,7852159,1596357' from dual)
3 select listagg(regexp_substr(col, '[^,]{4}', 1, level), ',')
4 within group (order by level) result
5 from test
6 connect by level <= regexp_count(col, ',') + 1;
RESULT
--------------------------------------------------------------------------------
1234,8523,7852,1596
SQL>
With REGEX_REPLACE:
select regexp_replace(the_string, '(^|,)([^,]{4})[^,]*', '\1\2')
from mytable;
This looks for
the beginning of the string or the comma
then four characters that are not a comma
then any number of trailing characters that are not a comma
And only keeps
the beginning or the comma
the four characters that follow
Demo: https://dbfiddle.uk/efUFvKyO
I have a column in a table ident_nums that contains different types of ids. I need to remove special characters(e.g. [.,/#&$-]) from that column and replace them with space; however, if the special characters are found at the beginning of the string, I need to remove it without placing a space. I tried to do it in steps; first, I removed the special characters and replaced them with space (I used
REGEXP_REPLACE) then found the records that contain spaces at the beginning of the string and tried to use the TRIM function to remove the white space, but for some reason is not working that.
Here is what I have done
Select regexp_replace(id_num, '[:(),./#*&-]', ' ') from ident_nums
This part works for me, I remove all the unwanted characters from the column, however, if the string in the column starts with a character I don't want to have space in there, I would like to remove just the character, so I tried to use the built-in function TRIM.
update ident_nums
set id_num = TRIM(id_num)
I'm getting an error ORA-01407: can't update ident_nums.id_num to NULL
Any ideas what I am doing wrong here?
It does work if I add a where clause,
update ident_nums
set id_num = TRIM(id_num) where id = 123;
but I need to update all the rows with the white space at the beginning of the string.
Any suggestions are welcome.
Or if it can be done better.
The table has millions of records.
Thank you
Regexp can be slow sometimes so if you can do it by using built-in functions - consider it.
As #Abra suggested TRIM and TRANSLATE is a good choice, but maybe you would prefer LTRIM - removes only leading spaces from string (TRIM removes both - leading and trailing character ). If you want to remove "space" you can ommit defining the trim character parameter, space is default.
select
ltrim(translate('#kdjdj:', '[:(),./#*&-]', ' '))
from dual;
select
ltrim(translate(orginal_string, 'special_characters_to_remove', ' '))
from dual;
Combination of Oracle built-in functions TRANSLATE and TRIM worked for me.
select trim(' ' from translate('#$one,$2-zero...', '#$,-.',' ')) as RESULT
from DUAL
Refer to this dbfiddle
I think trim() is the key, but if you want to keep only alpha numerics, digits, and spaces, then:
select trim(' ' from regexp_replace(col, '[^a-zA-Z0-9 ]', ' ', 1, 0))
regexp_replace() makes it possible to specify only the characters you want to keep, which could be convenient.
Thanks, everyone, It this query worked for me
update update ident_nums
set id_num = LTRIM(REGEXP_REPLACE(id_num, '[:space:]+', ' ')
where REGEXP_LIKE(id_num, '^[ ?]')
this should work for you.
SELECT id_num, length(id_num) length_old, NEW_ID_NUM, length(NEW_ID_NUM) len_NEW_ID_NUM, ltrim(NEW_ID_NUM), length(ltrim(NEW_ID_NUM)) length_after_ltrim
FROM (
SELECT id_num, regexp_replace(id_num, '[:(),./#*&-#]', ' ') NEW_ID_NUM FROM
(
SELECT '1234$%45' as id_num from dual UNION
SELECT '#SHARMA' as id_num from dual UNION
SELECT 'JACK TEST' as id_num from dual UNION
SELECT 'XYZ#$' as id_num from dual UNION
SELECT '#ABCDE()' as id_num from dual -- THe 1st character is space
)
)
I'm trying to identify fields that have more than one space in a comment, e.g. 'this lhas three spaces'
Using this I can get anything with two spaces, but would like to be able to get 2 or more:
select * from labtec.spaces
where REGEXP_LIKE(SPACES, '[[:space:]]{2}');
Any suggestions?
I believe that you can:
select * from labtec.spaces
where REGEXP_LIKE(SPACES, '[[:space:]]{2,}');
Note the comma.
For "Between three and five" you would use {3,5}, for "two or more" {2,}, for "eight or less" {,8}
where REGEXP_LIKE(SPACES, '[[:space:]][[:space:]]+');
You do not need to check for two-or-more characters - checking for two is sufficient to filter the rows since if there are three characters then matching only two of them will work just as well as matching two-or-more.
This will find strings which have two or more (consecutive or non-consecutive) space CHR(32) characters (without using regular expressions):
SELECT *
FROM labtec.spaces
WHERE INSTR( spaces, ' ', 1, 2 ) > 0
This will find where there are two or more consecutive space CHR(32) characters:
SELECT *
FROM labtec.spaces
WHERE INSTR( spaces, ' ' ) > 0
If you want any two (or more) consecutive white-space characters then you only need to check for two matching characters:
SELECT *
FROM labtec.spaces
WHERE REGEXP_LIKE( spaces, '\s\s' ) -- Or, using POSIX syntax '[[:space:]]{2}'
Update - Leading and trailing spaces
SELECT *
FROM labtec.spaces
WHERE SUBSTR( spaces, 1, 2 ) = ' ' -- at least two leading spaces
OR SUBSTR( spaces, -2 ) = ' ' -- at least two trailing spaces
or, using (perl-like) regular expressions:
SELECT *
FROM labtec.spaces
WHERE REGEXP_LIKE( spaces, '^\s\s|\s\s$' )
Hi I have a table created in oracle with field Org varchar2
create table new ( org varchar2(10));
insert three records
insert into new values ('127') ;
insert into new values ('128') ;
insert into new values ('129') ;
Run this sql with parameter : 127, 128,129, you will get 127,129 but not 128 because there is blank space before 128, Is there any way I can get rid of Blank space in this sql and get output 127,128,129 .
SELECT org FROM new WHERE org IN (SELECT REGEXP_SUBSTR (NVL2(:p_org_code,:p_org_code,org),
'[^,]+',
1,
LEVEL)
FROM DUAL
CONNECT BY REGEXP_SUBSTR (NVL2(:p_org_code,:p_org_code,org),
'[^,]+',
1,
LEVEL)
IS NOT NULL)
If you want to remove leading whitespace, use the ltrim() function. If you want to remove trailing whitespace, use the rtrim() function. If you want to remove both leading and trailing whitespace, nest them: ltrim(rtrim(s)).
One might note that using these functions with a single argument, e.g., ltrim( some_string_expression ), these functions remove only leading/trailing spaces (0x20, ASCII SP).
Oracle's PL/SQL has an alternative form: [lr]trim( str_to_trim , str_containing_trim_chars ) where the second string defines the set of characters to be trimmed. The "usual" definition of whitespace is
HT, horizontal tab, (0x09, dec 9)
LF, line feed, (0x0A, dec 10)
VT, vertical tab, (0x0B, dec 11)
FF, form feed, (0x0C, dec 12)
CR, carriage return, (0x0D, dec 13)
SP, space (0x20, dec 32)
so to trim those, you'd say something like:
rtrim(ltrim( source_string ,
chr(9)+chr(10)+char(11)+char(12),+char(13)+char(32) ) ,
chr(9)+chr(10)+char(11)+char(12),+char(13)+char(32) )
If you want to remove all whitespace from a string, you can use PL/SQL's translate() function, Something like
translate(
source_string ,
chr(9)+chr(10)+char(11)+char(12),+char(13)+char(32) , -- whitespace chars
'' -- replacement chars
)
removes all whitespace from the source string.
You can also use PL/SQL's regular expressions:
^ anchors the match to the beginning of the string
$ anchors the match at the end of the string
[:space:] matches a single whitespace character.
You can remove leading/trailing whitespace like this:
regex_replace( source, '(^[:space]+)|([:space:]+$)' , '' )
You can collapse embedded whitespace sequences into a single space character like this:
regex_replace( source , '[:space:]+' , ' ' )
and finally, you can "normalize" a string, by removing leading/trailing whitespace and replacing all embedded sequences of 1 or more whitespace characters with a single space character like this:
ltrim(rtrim(regex_replace( source , '[:space:]+' , ' ' )))
There is, as they say, more than one way to do it.
try
RTRIM(LTRIM('128 '))
The output will be '128'
If you ask us how to get rid of white spaces around the value then:
with t as (
select '123 ' id from dual union all
select ' 124' id from dual union all
select ' 125 ' id from dual
)
select *
from t
where trim(id) in (123, 124, 125)
ID
---
123
124
125
You might try
SELECT *
FROM YOUR_TABLE
WHERE ORG LIKE '%128%';
Share and enjoy.
I have used REPLACE function in order to delete email addresses from hundreds of records. However, as it is known, the semicolon is the separator, usually between each email address and anther. The problem is, there are a lot of semicolons left randomly.
For example: the field:
123#hotmail.com;456#yahoo.com;789#gmail.com;xyz#msn.com
Let's say that after I deleted two email addresses, the field content became like:
;456#yahoo.com;789#gmail.com;
I need to clean these fields from these extra undesired semicolons to be like
456#yahoo.com;789#gmail.com
For double semicolons I have used REPLACE as well by replacing each ;; with ;
Is there anyway to delete any semicolon that is not preceded or following by any character?
If you only need to replace semicolons at the start or end of the string, using a regular expression with the anchor '^' (beginning of string) / '$' (end of string) should achieve what you want:
with v_data as (
select '123#hotmail.com;456#yahoo.com;789#gmail.com;xyz#msn.com' value
from dual union all
select ';456#yahoo.com;789#gmail.com;' value from dual
)
select
value,
regexp_replace(regexp_replace(value, '^;', ''), ';$', '') as normalized_value
from v_data
If you also need to replace stray semicolons from the middle of the string, you'll probably need regexes with lookahead/lookbehind.
You remove leading and trailing characters with TRIM:
select trim(both ';' from ';456#yahoo.com;;;789#gmail.com;') from dual;
To replace multiple characters with only one occurrence use REGEXP_REPLACE:
select regexp_replace(';456#yahoo.com;;;789#gmail.com;', ';+', ';') from dual;
Both methods combined:
select regexp_replace( trim(both ';' from ';456#yahoo.com;;;789#gmail.com;'), ';+', ';' ) from dual;
regular expression replace can help
select regexp_replace('123#hotmail.com;456#yahoo.com;;456#yahoo.com;;789#gmail.com',
'456#yahoo.com(;)+') as result from dual;
Output:
| RESULT |
|-------------------------------|
| 123#hotmail.com;789#gmail.com |