I've got the following code:
INSERT INTO DWCUST (DWCUSTID, DWSOURCEIDBRIS, DWSOURCEIDMELB, FIRSTNAME, SURNAME, GENDER, PHONE, POSTCODE, CITY, STATE, CUSTCATNAME)
SELECT dwcustSeq.nextval, cb.custid, Null, cb.fname, cb.sname, UPPER(cb.gender), cb.phone, cb.postcode, cb.city, cb.state, cc.custcatname
FROM a2custbris cb
NATURAL JOIN a2custcategory cc
WHERE cb.rowid IN (SELECT source_rowid FROM A2ERROREVENT where filterid = 5);
I want to adjust it so that before cb.phone (varchar2) values are added to dwcust, all hyphens and spaces are removed from the strings so that they are just numeric.
For instance I want 04-1234 5254 to become 0412345254
Translate can be useful:
translate(cb.phone, 'X- ', 'X')
For example,
select translate(' 04-1234 5254', 'X- ', 'X')
from dual
gives:
TRANSLATE('04-12345254','X-','X')
---------------------------------
0412345254
1 row selected.
About this usage, Oracle docs says:
You cannot use an empty string for to_string to remove all characters
in from_string from the return value. Oracle Database interprets the
empty string as null, and if this function has a null argument, then
it returns null. To remove all characters in from_string, concatenate
another character to the beginning of from_string and specify this
character as the to_string. For example, TRANSLATE(expr,
'x0123456789', 'x') removes all digits from expr.
I would use regexp_replace()
select regexp_replace('04-1234 5254', '[- ]', '')
from dual;
This would still return something that is not a number if it e.g. contains a /
To make sure or to remove everything that is not a number use the following:
select regexp_replace('04-1234 5254', '[^0-9]', '')
from dual;
Just replace cb.phone with regexp_replace(cb.phone, , '[^0-9]', '') in your SELECT list.
SELECT ...., regexp_replace(cb.phone, , '[^0-9]', ''), ....
FROM a2custbris cb
....
Related
I have following Oracle SQL code:
SELECT TO_NUMBER(TRIM(REGEXP_REPLACE(per_growth, '(%)(\s)')),
'FM99999999999999999990D099999999999999999',
'NLS_NUMERIC_CHARACTERS = '', ''') AS per_growth
FROM sometable;
This code supposed to look for percentage sign first then space and exclude them from the result. However, it is showing
ORA-01722: invalid number
error. I am learning sql yet and do not know exact cause. Is it something went wrong with (%)(\s)? The value in the table is 50%
You can use TRANSLATE to get rid of all instances of unwanted characters:
SELECT TO_NUMBER(
TRANSLATE(per_growth, '0% ', '0'),
'FM99999999999999999990D099999999999999999',
'NLS_NUMERIC_CHARACTERS = '', '''
) as per_growth
FROM sometable;
Note: TRANSLATE(expr, from_string, to_string) works by swapping all instances of the characters in from_string with the corresponding characters in to_string and if there are more characters in from_string than to_string then the remaining characters are removed. It is faster than using regular expressions and on a par with using REPLACE but it can handle multiple replacements at once, which REPLACE cannot.
If you did want to use the slower REGEXP_REPLACE then you can replace all whitespace characters and all percent characters, whether together or not, using:
SELECT TO_NUMBER(
REGEXP_REPLACE(per_growth, '[%[:space:]]'),
'FM99999999999999999990D099999999999999999',
'NLS_NUMERIC_CHARACTERS = '', '''
) as per_growth
FROM sometable;
Which, for the sample data:
CREATE TABLE sometable (per_growth) AS
SELECT '1%' FROM DUAL UNION ALL
SELECT '%2' FROM DUAL UNION ALL
SELECT '3 %' FROM DUAL UNION ALL
SELECT '4% ' FROM DUAL UNION ALL
SELECT '5,0%' FROM DUAL UNION ALL
SELECT '%%% 123 456 789,0123456 %' FROM DUAL;
Both output:
PER_GROWTH
1
2
3
4
5
123456789.0123456
db<>fiddle here
Did you try good, old REPLACE?
select replace(replace(per_growth, '%', ''), ' ', '') as result
from your_table
You can use
REGEXP_REPLACE(per_growth, '( )(%)')
in order to get rid of % sign and whitespace(s) together
or
TRIM(REPLACE(per_growth,'%'))
to get rid of % sign first, and then leading and trailing spaces next,
before numerical conversion.
Basically translate will change character to character and Replace string to string , and here i have tried to remove spaces using translate to count the number words .
select translate(' #',' ','') from dual;
select replace(' #',' ','') from dual;
select ename , nvl(length(replace(TRANSLATE(upper(trim(ename)),'ABCDEFGHIJKLMNOPQRSTUVWXYZ'' ',' # '),' ',''))+1,1) NOOFWORDs
from emp;
Unfortunately Oracle has made many bizarre choices around null vs. empty string.
One of those has to do with TRANSLATE. TRANSLATE will return NULL if any of its arguments (including the last one) is NULL, no matter what the logical behavior should be.
So, to remove spaces (say) with TRANSLATE, you must add a character you do NOT want to be removed to both the second and the third argument. I added the lower-case letter z, but you could add anything (a dot, the digit 0, whatever - just make sure you add the same character at the beginning of both arguments)
... translate (input_string, 'z ', 'z') ....
For example:
select translate(' #','z ','z') from dual;
TRANSLATE('#','Z','Z')
------------------------
#
select translate(' #',' ','') from dual;
Returns NULL because in Oracle empty strings unfortunately yield NULLs. Therefore it's equivalent to
SELECT translate(' #', ' ', NULL)
FROM dual;
and translate() returns NULL when an argument is null. Actually this is well documented in "TRANSLATE":
(...)
You cannot use an empty string for to_string to remove all characters in from_string from the return value. Oracle Database interprets the empty string as null, and if this function has a null argument, then it returns null.
If you want to replace one character, use replace() as you already did. For a few but more than one characters you can nest the replace()s.
This however gets unhandy, when you want to replace quite a lot of characters. In such a situation, if the replacement character is only one character or the empty string regexp_replace() using a character class or alternates may come in handy.
For example
SELECT regexp_replace('a12b478c01', '[0-9]', '')
FROM dual;
replaces all the digits so just 'abc' remains and
SELECT regexp_replace('ABcc1233', 'c|3', '')
FROM dual;
removes any '3' or 'c' and results in 'AB12'. In your very example
SELECT regexp_replace(' #', ' ', '')
FROM dual;
would also work and give you '#'. Though in the simple case of your example a simple replace() is enough.
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;
My query is the following:
SELECT id, category FROM table1
This returns the following rows:
ID|category
1 |{IN, SP}
2 |
3 |{VO}
Does anyone know how i can remove the first char and last char of the string in PostgreSQL, so it removes: {}?
Not sure, what you mean with "foreign column", but as the column is an array, the best way to deal with that is to use array_to_string()
SELECT id, array_to_string(category, ',') as category
FROM table1;
The curly braces are not part of the stored value. This is just the string representation of an array that is used to display it.
Either using multiple REPLACE functions.
SELECT id, REPLACE(REPLACE(category, '{', ''), '}', '')
FROM table1
Or using a combination of the SUBSTRING, LEFT & LENGTH functions
SELECT id, LEFT(SUBSTRING(category, 2, 999),LENGTH(SUBSTRING(category, 2, 999)) - 1)
FROM table1
Or just SUBSTRING and LENGTH
SELECT id, SUBSTRING(category, 2, LENGTH(category)-2)
FROM table1
You could replace the {} with an empty string
SELECT id, replace(replace(category, '{', ''), '}', '') FROM table1
select id,
substring(category,charindex('{',category,len(category))+2,len(category)-2)
from table1;
select id
,left(right(category,length(category)-1),length(category)-2) category
from boo
select id
,trim(both '{}' from category)
from boo
Trim():
Remove the longest string containing only the characters (a space by
default) from the start/end/both ends of the string
The syntax for the replace function in PostgreSQL is:
replace( string, from_substring, to_substring )
Parameters or Arguments
string
The source string.
from_substring
The substring to find. All occurrences of from_substring found within string are replaced with to_substring.
to_substring
The replacement substring. All occurrences of from_substring found within string are replaced with to_substring.
UPDATE dbo.table1
SET category = REPLACE(category, '{', '')
WHERE ID <=3
UPDATE dbo.table1
SET category = REPLACE(category, '}', '')
WHERE ID <=3
I'm sure this is super easy, but how would I go about converting LastName,FirstName to LastName,FirstInitial?
For example changing Smith,John to Smith,J or Johnson,John to Johnson,J etc.
Thank You!
In case of LastName and FirstName columns:
select LastName,substr(FirstName,1,1)
from mytable
;
In case of a fullname saved in a single column:
select substr(fullname,1,instr(fullname || ',',',')-1) || substr(fullname,instr(fullname || ',',','),2)
from mytable
;
or
select regexp_replace (fullname,'([^,]*,?)(.).*','\1\2')
from mytable
;
Here is one way, using just "standard" instr and substr. Assuming your input is a single string in the format 'Smith,John':
select substr(fullname, 1, instr(fullname, ',')+1) from yourtable;
yourtable is the name of the table, and fullname is the name of the column.
instr(fullname, ',') finds the position of the comma within the input string (it would be 6 in 'Smith,John'); thensubstrtakes the substring that begins at the first position (the1in the function call) and ends at the position calculated byinstr`, PLUS 1 (to get the first initial as well).