5

I'm trying to write a SQL query that will remove all spaces so that if a string only has spaces the final string is just ''.

I've tried this code but apparently it isn't working for more than one space:

regexp_replace(:P14_search_text, '( ){1,}', '')

Being :P14_search_text the string I want to modify.

Any help?

5 Answers 5

7

how about:

regexp_replace(:P14_search_text, '[[:space:]]*', '');
Sign up to request clarification or add additional context in comments.

1 Comment

It didn't work either :/ I'm starting to think the problem is in my code but I just can't find the flaw...thank you anyway (:
5

try this:

     Select Replace(:P14_search_text, ' ', '');

2 Comments

so, what did you get from that?
I'm applying this on a search text box, so when I type just one space, it's good, but whenever I type in more than one space, it doesn't work.
3

Hope this helps you,

SELECT REGEXP_REPLACE(' Any  String ','( ){1,}','') "REGEXP_REPLACE"  FROM DUAL;
SELECT REGEXP_REPLACE('  ','( ){1,}','') "REGEXP_REPLACE"  FROM DUAL;

Comments

3

I tried the same method as @Don suggested and it works in oracle 10 xe.

select replace('     lkjds  d   s   adkj      ', ' ', '') from dual

result

lkjdsdsadkj

Comments

2

the following query works for me in oracle:

select
    :tst_val AS INPUT,
    regexp_replace(:tst_val, '[[:space:]]*', '') AS MODIFIED
from
    dual

if this query does not work for you, would you show us what results you're getting?

6 Comments

It's not working, this is what I'm trying to accomplish: where bobooks.owner <> :app_user and (:P14_search_text IS NULL or regexp_replace(:P14_search_text, '[[:space:]]*', '') = '' or ((lower(:P14_CATEGORY) <> 'title' or (lower(:P14_CATEGORY) = 'title' and lower(bobooks.title) like '%' || lower(:P14_search_text) || '%')) and (lower(:P14_CATEGORY) <> 'author' or (lower(:P14_CATEGORY) = 'author' and lower(bobooks.author) like '%' || lower(:P14_search_text) || '%'))))
are you in a sql server database or an oracle database?
is the query just not returning anything or is it giving you an error? and what about the query in my answer? what does it return?
It's not returning anything, it's working as if it only changes 1 space, after two spaces I receive nothing, no data, on the other hand when I type in just one space, I receive all available data, just as planned, yoour query does the same
try changing the expression to regexp_replace(:P14_search_text, '[[:space:]]*', '', 1, 0) to explicitly tell it to replace all occurences. it should do this by default, but apparently is not.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.