I am using PostgreSQL. Data in the table looks like below there is ''. I would like to remove '' with the empty value.
Query:
select 'Patriots Colony Family Monthly'
Actual Result Screenshot:
Expected result:
Abcd
You can use regex_replace to remove non alphanumeric chars
select regexp_replace(yourColumn, '[^[:alnum:]]', ' ', 'g')
from yourTable;
You can also do like this
select regexp_replace(yourColumn, '[^a-zA-Z0-9]+',' ','g')
from yourTable;
Related
I would like to keep rows only with Numeric Character i.e. 0-9. My source data can have any type of character e.g. 2,%,( .
Input (postcode)
3453gds sdg3
454232
sdg(*d^
452
Expected Output (postcode)
454232
452
I have tried using WHERE REGEXP_LIKE(postcode, '^[[:digit:]]+$');
however in my version of Oracle I get an error saying
function regexp_like(character varying, "unknown") does not exist
You want regexp_like() and your version should work:
select t.*
from t
where regexp_like(t.postcode, '^[0-9]+$');
However, your error looks more like a Postgres error, so perhaps this will work:
t.postcode ~ '^[0-9]+$'
For Oracle 10 or higher you can use regexp functions. In earlier versions translate function will help you :
SELECT postcode
FROM table_name
WHERE length(translate(postcode,'0123456789','1')) is null
AND postcode IS NOT NULL;
OR
SELECT translate(postcode, '0123456789' || translate(postcode,'x123456789','x'),'0123456789') nums
FROM table_name ;
the above answer also works for me
SELECT translate('1234bsdfs3#23##PU', '0123456789' || translate('1234bsdfs3#23##PU','x123456789','x'),'0123456789') nums
FROM dual ;
Nums:
1234323
For an alternative to the Gordon Linoff answer, we can try using REGEXP_REPLACE:
SELECT *
FROM yourTable
WHERE REGEXP_REPLACE(postcode, '[0-9]+', '') IS NULL;
The idea here is to strip away all digit characters, and then assert that nothing were left behind. For a mixed digit-letter value, the regex replacement would result in a non-empty string.
My query is not working as expected :
SELECT REGEXP_REPLACE('This is testing data SIL(TM)T for once ',
'SIL(TM)T', 'SIL<REFERENCE ID="8208" TYPE="trademark"/>T')as
Newdescriptiontext from dual
output should be:
This is testing data SIL<REFERENCE ID="8208" TYPE="trademark"/>T for once
which is not the case .Need guidance .
try replace instead of regexp_replace
SELECT REPLACE('This is testing data SIL(TM)T for once ',
'SIL(TM)T', 'SIL<REFERENCE ID="8208" TYPE="trademark"/>T')as
Newdescriptiontext from dual;
You simply have to escape the parentheses:
SELECT REGEXP_REPLACE('This is testing data SIL(TM)T for once ',
'SIL\(TM\)T', 'SIL<REFERENCE ID="8208" TYPE="trademark"/>T')as Newdescriptiontext
from dual
In a regexp, they are used to delimit a "subexpression", so '(TM)' matches 'TM'; if you escape them, they'll be interpreted as plain characters, thus having '\(TM\)' matching '(TM)'
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;
I have sql query and want to replace all characters except hyphen(-) , alphabets and numbers.
How can I do that in sql query?
You can represent non hyphen or alphanumeric characters by the class:
[^\-a-zA-Z0-9]
Then use REGEXP_REPLACE to remove these characters from your column:
SELECT REGEXP_REPLACE (col, '[^\-a-zA-Z0-9]', '')
FROM dual;
This would remove all the alphabets and numbers from the input string and will leave '-'.
SELECT 'Rajkakla-53535-' As Strng, REGEXP_REPLACE(REGEXP_REPLACE ('Rajkakla-53535-', '[A-Za-z]',''), '[0-9]','') As No_Alphnum
FROM dual;
Or you can use:
SELECT REGEXP_REPLACE ('Rajkakla-53535-', '[a-zA-Z0-9]', '')
FROM dual;
I have column in a table which contains special characters with an attached string. The same column contains numbers also. I am only intresed in extracting numbers out of that column e.g
Name-3445 => 3445; Out-90 => 90; 786 => 786
How would i do this in SQL or PL/SQL?
SELECT regexp_replace(some_column, '[^0-9]*', '') as clean_value
FROM your_table
PL/SQL has a REGEX_REPLACE function, which you could use to replace anything that isn't a digit with an empty string. Details on REGEX_REPLACE can be found here: http://psoug.org/reference/regexp.html
Without knowing the integrity of your data, something like this might do what you're asking:
select CAST(SUBSTRING(_COLUMNNAME_,CHARINDEX('-', _COLUMNNAME_),1000), Integer) as ColumnName
from tblTable where _COLUMNNAME_ like '%-%'
union all select CAST(_COLUMNNAME, Integer) as ColumnName
from tblTable where _COLUMNNAME_ not like '%-%'
You can use the regexp_substr function:
http://docs.oracle.com/cd/B14117_01/server.101/b10759/functions116.htm
REGEXP_REPLACE(<Your_String>,'[^[:alnum:]'' '']', NULL)
Example:
SELECT REGEXP_REPLACE('##$$$123&&!!__!','[^[:alnum:]'' '']', NULL) FROM dual;
Output:
123