Find value having leading and trailing space in DB table column - Oracle - sql

I have a record which has value with leading and space in a column.
eg: column value is ' D0019 '
I want to pass this particular column in where clause.
select * from my_table where my_column='D0019';
Since the value has space, it doesn't detect from the where clause.
How can I select the record even it has leading and trailing spaces in the value?
My DB is ORACLE
========================================
UPDATE :
I get value only when I try
select * from my_table where my_column like '%D0019%'
not even with ' %D0019% '
=============================================
UPDATE 2 :
SELECT my_column ,DUMP(my_column) FROM my_table WHERE my_column like '%D0019';
output is
" D0019" Typ=1 Len=6: 9,68,48,48,49,57

it's not the normal space you have to remove. It's Horizontal Tab character . (Ascii 9).
The below regexp would strip all the charcters from ASCII range 0-32 , which are associated with the white space symbols.
select * from my_table
WHERE
REGEXP_REPLACE(my_column,'['||chr(1)||'-'||chr(32)||']' ) = 'D0019';
More on ASCII table

select *
from my_table
where trim(my_column) = 'D0019';
Edit:
Based on the output of the dump() function your values not just contain (leading) spaces but also a tab character. the trim() function will only remove space characters, but not other whitespace.
In order to get rid of any whitespace at the beginning you will need to use regexp_replace()
select *
from my_table
where regexp_replace(my_column,'^(\s)+','') = 'D0019'
If you need to get rid of leading and trailing whitespace, the regex needs to be expanded:
select *
from my_table
where regexp_replace(my_column,'^(\s)+|(\s)+$','') = 'D0019'
SQLFiddle example: http://sqlfiddle.com/#!4/3326a/1

Seems as simple as (if I get it):
select * from TAB where REGEXP_LIKE (COL,'\s*D0019')
returns values such ' D0019', 'D0019', ' D0019', ' D0019' ...etc.

Try like this;
select * from my_table where my_column like '%D0019';

you can also use this if you want only spaces at start for the select:
select * from my_table where my_column like '% %D0019';
or
select * from my_table where my_column like ' %D0019';

Related

How do I use an ORACLE REGEX function to remove all leading and trailing line break characters and spaces?

How do I use an ORACLE REGEX function to remove all leading and trailing line break characters and spaces?
For example, assume I have the following string where refers to actual invisible carriage return line feed characters. Here's the input:
"
SELECT *
FROM
TABLE
"
And here's the desire output:
"SELECT *
FROM
TABLE"
This would do it if regex_replace() is a requirement:
select regexp_replace('
SELECT *
FROM
TABLE
', '^\s*|\s*$', '') as hello
from dual
See https://www.techonthenet.com/oracle/functions/regexp_replace.php for documentation.
A single regexp_replace is sufficient, eg.
select regexp_replace('
select frut
from prut
','^[[:space:]]*(.*[^[:space:]])[[:space:]]*$','\1',1,1,'mn') from dual;
results in
select frut
from prut

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 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 '% '

How could I remove unnecessary characters in SQL

I need a query that could remove unnecessary characters (a not-so-needed trailing comma as an example) from the string stored in my database table.
So that
EMAIL_ADD
abc#gmail.com,
abc#yahoo.com,def#example.org,
abs-def#ac.uk,
would update it into something like this:
EMAIL_ADD
abc#gmail.com
abc#yahoo.com,def#example.org
abs-def#ac.uk
Using TRIM() function with TRAILING option removes a specific unwanted character from end of string , in your case being a comma present at end.
UPDATE tableName
SET EMAIL_ADD = TRIM(TRAILING ',' FROM EMAIL_ADD)
See documentation here TRIM()
If you have a specific list of characters to filter out at the start and end use trim functions:
select ltrim(ltrim(rtrim(rtrim(email_add, ','), ' '), ','), ' ')
from tableX
Here I nested ltrim and rtrim to remove leading and trailing , and .
Or using trim:
select trim(trim(both ',' from email_add))
from tableX
if you only whant to remove the last character of a string you can use
update mytable set my_column = substr(my_column ,0,len(trim(my_column)-1) where mycolumn like '%,'
It is an untested example.

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 |