How to remove Blank space from the string? - sql

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.

Related

Oracle replace some duplicated characters (non digits )

anyone can help me to build proper syntax for regexp_replace to remove any multiplicated non-digits and non-letters from string ? If digit/letter is multiplicated - it is not changed
eg.
source and expected result:
'ABBC000001223, ABC00000212,,, '
'ABBC000001223, ABC00000212, '
(removed second occurance of space after comma and second and third comma )
Use this REGEXP_REPLACE to match any non alphanumeric character in the first group
([^[:alnum:]])
followed by one or more same charcters (group 1)
([^[:alnum:]])(\1)+
and replace it with the original character (group 1)
I added some other data to demonstrate the result
with dta as (
select 'ABBC000001223, ABC00000212,,, ' txt from dual union all
select ',.,;,;;;;,,,,,,,,,,,,#''++`´' txt from dual union all
select 'ABBC000001223ABC00000212' txt from dual)
select txt,
regexp_replace(txt,'([^[:alnum:]])(\1)+', '\1') result
from dta
TXT
-------------------------------
RESULT
--------------------------------
ABBC000001223, ABC00000212,,,
ABBC000001223, ABC00000212,
,.,;,;;;;,,,,,,,,,,,,#'++`´
,.,;,;,#'+`´
ABBC000001223ABC00000212
ABBC000001223ABC00000212

Oracle remove special characters

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
)
)

How to convert blank to NULL in oracle query

I have blanks in my name column in sql query. How can I replace to show as null.
SELECT Name from table
The TRIM function provides this feature.
It is used like this:
select TRIM(Name) from table
It will remove leading and trailing spaces from the results for field Name.
Maybe you are talking of spaces?
Here is how to remove any commonly known "blank" chars:
with regexp_replace (interestingly... just to notice the [[:space:]])
select '<'||
regexp_replace(
'a'||CHR(9)||' b'||CHR(10)||CHR(11)
||'c'||CHR(12)||CHR(13)||' d'
, '[[:space:]]','')
||'>'
from dual;
more efficient: avoid regexp_replace...: use TRANSLATE!
select '<'||
TRANSLATE(
'a'||CHR(9)||' b'||CHR(10)||CHR(11)
||'c'||CHR(12)||CHR(13)||' d' -- complicate string with blank
,'A'||CHR(9)||CHR(10)||CHR(11)||CHR(12)||CHR(13)||' '
,'A') -- this 'A' is a trick to replace by null ('')
||'>' -- to show where string stops
from dual;
TRIM removes the blank character from left and right, so if your string only consists of blanks then you get an empty string, which is NULL in Oracle.
select trim(name) from mytable;
This would also change ' Mary Smith ' to 'Mary Smith', but I guess you wouldn't mind :-)
If, however, you want to consider any whitespace, e.g. tabs, too, then TRIM doesn't suffice. You can use REGEXP_REPLACE then to replace all names that only consist of whitespace with null.
regexp_replace(name, '^[[:space:]]*$', null) from mytable;
If you also want to trim whitespace from any names (so ' Mary Smith ' becomes 'Mary Smith' again) then:
select regexp_replace(name, '^[[:space:]]*([^[:space:]]*)[[:space:]]*', '\1') from mytable;

Oracle - Can't delete/replace whitespaces

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

Delete certain character based on the preceding or succeeding character - ORACLE

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 |