Removing spaces from a string, SQL - sql

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?

how about:
regexp_replace(:P14_search_text, '[[:space:]]*', '');

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

Hope this helps you,
SELECT REGEXP_REPLACE(' Any String ','( ){1,}','') "REGEXP_REPLACE" FROM DUAL;
SELECT REGEXP_REPLACE(' ','( ){1,}','') "REGEXP_REPLACE" FROM DUAL;

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

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?

Related

SQL SELECT WHERE without underscore and more

I want to select where 2 strings but without taking
underscore
apostrophe
dash..
Hello !
I want to select an option in my SQL database who look like this :
Chef d'équipe aménagement-finitions
With an original tag who look like this
chef-déquipe-aménagement-finitions
Some results in database had a - too
SELECT *
FROM table
WHERE REPLACE(name, '-', ' ') = REPLACE('chef-déquipe-aménagement-finitions', '-', ' ')
didnt work because of missing '
And a double replace didn't work too.
I want the string be able to compare without taking
underscore
apostrophe
dash
and all things like that
is this possible ?
Thanks for your help
Have good day !
Depends on your rdbms, but here's how I would perform in MySQL 8. If using a different version or rdbms, then first determine how to escape the single quote and modify as needed.
with my_data as (
select 'Chef d''équipe aménagement-finitions' as name
)
select name,
lower(replace(replace(name, '\'', ''), ' ', '-')) as name2
from my_data;
name
name2
Chef d'équipe aménagement-finitions
chef-déquipe-aménagement-finitions
Sql-server and Postgres version:
lower(replace(replace(name, '''', ''), ' ', '-')) as name
After posting, this, I re-read and noticed you are also looking to replace other characters. You could either keep layering the replace function, or, look into other functions.

How to remove a specific character from the string

Using SQL Server 2008
String like: 'C/123232323' or '/343434343443' or 'C2323232322'
From the string i want to remove C and /
Tried Query
Select replace ('/1233434', 'C/', '')
The above query is working if C/ both is there. if / only there then the replace is not working. if C only there then the replace is not working. How to achieve for both condition
Expected output
123232323
343434343443
2323232322
Need Query output
You can achieve this by nesting replace() like so:
select replace(replace('C/12341234','/',''),'C','')
Probably not the prettiest but it works :)
You could use two nested REPLACE:
WITH SampleData(string) AS(
SELECT 'C/123232323' UNION ALL
SELECT '/343434343443' UNION ALL
SELECT 'C2323232322'
)
SELECT REPLACE(REPLACE(string,'C',''),'/','')
FROM SampleData
Use Patindex + Substring
DECLARE #str VARCHAR(50)='/343434343443' ---or '/343434343443' or 'C2323232322'
SELECT Substring(#str, Patindex('%[0-9]%', #str), Len(#str))

Remove all whitespaces in string

I have to remove all whitespaces in String '5 000 000,5' to '5000000,5'.
I tried 3 below but it did not work
select replace('5 000 000,5',' ','') from dual;
select regexp_replace('5 000 000,5', '[[:space:]]*','') from dual;
select regexp_replace('5 000 000,5', ' ','') from dual;
Or anyone know how to convert this String '5 000 000,5' to number because TO_NUMBER failed.
Thanks
Using REGEXP_REPLACE and SPACE class.
Select regexp_replace('your_value', '[[:space:]]+', '') from dual:
Using REPLACE
Select REPLACE('your_value', chr(32), '') from dual:
I think the proplem is your NLS_NUMERIC_CHARACTERS, that should work
select to_number('5 000 000,5', '9G999G999D0', 'NLS_NUMERIC_CHARACTERS = '', ''')
from dual
You can try this and remove any non-numeric chars like comma (,)
SELECT to_number(regexp_replace('5 000 000,5', '[^0-9]', '')) FROM dual;

Best way to parse and concatenate a string with SQL

I'm trying to turn object_type,ABC,00,DEF,XY string into ABC-00-DEF-XY-
Here's what I've got, I'm wondering if there is a more efficient way?
CONCAT(
REPLACE(
SUBSTR(a.object_name,
INSTR(a.object_name, ',',1,1)+1,
INSTR(a.object_name, ',',1,2)+1
),',','-'
),'-'
)
Clarification: I need to strip off everything up to and including the first comma, replace all remaining commas with dashes, and then add a dash onto the end.
Try this
replace(substr(a.object_name,instr(a.object_name,',',1,1) + 1),',','-') ||'-'
Rexexp_replace() regular expression function can come in handy in this situation as well:
select ltrim(
regexp_replace( col
, '([^,]+)|,([^,]+)', '\2-'
)
, '-'
) as res
from t1
Result:
RES
--------------
ABC-00-DEF-XY-
SQLFiddle Demo
I suggest using the following code:
REPLACE(SUBSTRING(a.object_name,13,LEN(#object_name)-11),',','-') + '-'

Oracle Query - Get only strings in the select field

Maybe this sounds a little bit crazy, but I need to come up with a query to retrieve only letters out of an alphanumeric field.
For example:
TABLE
1234ADD
3901AC
1812OPA
82711AUU
RESULTS EXPECTED
ADD
AC
OPA
AUU
Thank you!
Looks like you only want to remove numbers. You can use REGEXP_REPLACE for that in 10g or 11g:
SELECT REGEXP_REPLACE( your_column, '[0-9]*', '' ) FROM your_table;
SELECT REGEXP_REPLACE('1234ADD 3901AC 1812OPA 82711AUU', '[0-9]', '')
FROM dual
Try
SELECT TRANSLATE('1234ADD 3901AC 1812OPA 82711AUU', 'A1234567890', 'A') FROM dual;
and in general see: http://www.psoug.org/reference/translate_replace.html