I'm using the DBMS_SQL package that returns the value '12345' as a value for a column name.
How do you trim the apostrophes out from the value so that it could be converted into a number?
Something like the following should trim the apostrophes:
substr(columnName, 2, length(columnName) - 2)
(As an aside, it's a pretty odd DB with a column name that's an integer, isn't it?)
This will remove leading and trailing apostrophes from a value:
RTRIM(LTRIM(value,''''),'''')
or if you prefer:
RTRIM(LTRIM(value,chr(39)),chr(39))
you could just use the "REPLACE" function to replace all single-quotes in the string with NULLs.
eg .
with quotes :
select 'Hello,''World''' from dual
quotes removed:
select replace('Hello,''World''','''',NULL) from dual
But, a columnname in Oracle cannot begin with a digit, so 12345 is invalid as a column-name anyway.
Related
I have a case where I am getting the data from DB and converting the string to a number using TO_NUMBER, but this case fails when the string is an empty string with unknown or space char like
columnA
------
4444
333333
The string '4444' and '333333' is converted to number by there is and error "ora-01722 invalid number" for the 2nd string.
Can this be handled with DECODE or CAST in any way, because I need to use TO_NUMBER any how for further processing?
I hope this could be Insight of your issue.
select
TO_NUMBER(trim(colA)),
TO_NUMBER(REGEXP_REPLACE(colA,'(^[[:space:]]*|[[:space:]]*$)')),
regexp_instr(colA, '[0-9.]')
from
(
select ' 123' colA from dual
union all
select ' ' colA from dual
union all
select '.456' colA from dual
)
This is similar issue : Trim Whitespaces (New Line and Tab space) in a String in Oracle
If all the data within that column is composed of integers, integers with leading and/or trailing whitespaces, null values and only whitespaces then only using TRIM() function will suffice such as
SELECT TRIM(columnA)
FROM t
and that would be more performant than using functions of regular expressions
But
If the data contains decimal numbers, letters, punctiations and special characters along with whitespaces and null values, then use
SELECT TRIM('.' FROM REGEXP_REPLACE(columnA,'[^[:digit:].]'))
FROM t
where there is at most one dot character assumed to be between the starting and ending digits. All of the leading and trailing dots are trimmed at the end of the operation provided there is any of them. The other characters are already removed by the regular expression.
If you're sure that there's no trailing or leading dots, then using
SELECT REGEXP_REPLACE(columnA,'[^[:digit:].]')
FROM t
would be enough
Demo
You can wrap up any of the expressions with TO_NUMBER() function depending on your case at the end
i tried to remove special characters and numeric number from string of sentence but it should ignore white spaces if there is a more than one it should replace with one
SQL developer,oracle 11g
select REGEXP_REPLACE ('Annapurna1# Poojari675&^','(\W|\d)','') from dual;
actually output is AnnapurnaPoojari but i need as Annapurna Poojari
You can be more explicit about the characters you want to keep:
select REGEXP_REPLACE('Annapurna1# Poojari675&^', '([^a-zA-Z ])', '')
from dual;
You can alternatively use [^[:alpha:] ]+ pattern to remove non-alphabetic characters and keep spaces :
select regexp_replace('Annapurna1# Poojari675&^','[^[:alpha:] ]','') as "Result String"
from dual;
Result String
-----------------
Annapurna Poojari
I have the string '1_A_B_C_D_E_1_2_3_4_5' and I am trying to extract the data 'A_B_C_D_E'. I am trying to remove the _1_2_3_4_5 & the 1_ portion from the string. Which is essentially the numeric portion in the string. any special characters after the last alphabet must also be removed. In this example the _ after the character E must also not be present.
and the Query I am trying is as below
SELECT
REGEXP_SUBSTR('1_A_B_C_D_E_1_2_3_4_5','[^0-9]+',1,1)
from dual
The Data I get from the above query is as below: -
_A_B_C_D_E_
I am trying to figure a way to remove the underscore towards the end. Any other way to approach this?
Assuming the "letters" come first and then the "digits", you could do something like this:
select regexp_substr('A_B_C_D_E_1_2_3_4_5','.*[A-Z]') from dual;
This will pull all the characters from the beginning of the string, up to the last upper-case letter in the string (.* is greedy, it will extend as far as possible while still allowing for one more upper-case letter to complete the match).
I have the string '1_A_B_C_D_E_1_2_3_4_5' and I am trying to extract the data 'A_B_C_D_E'
Use REGEXP_REPLACE:
SQL> SELECT trim(BOTH '_' FROM
2 (REGEXP_SUBSTR('1_A_B_C_D_E_1_2_3_4_5','[0-9]+', ''))) str
3 FROM dual;
STR
---------
A_B_C_D_E
How it works:
REGEXP_REPLACE will replace all numeric occurrences '[0-9]+' from the string. Alternatively, you could also use POSIX character class '[^[:digit:]]+'
TRIM BOTH '_' will remove any leading and lagging _ from the string.
Also using REGEXP_SUBSTR:
SELECT trim(BOTH '_' FROM
(REGEXP_SUBSTR('1_A_B_C_D_E_1_2_3_4_5','[^0-9]+'))) str
FROM dual;
STR
---------
A_B_C_D_E
I need to include the special character "%" in my LIKE clause in a SQL query, e.g.:
Select * From Some_Table Where Field_Name Like 'bla%bla&2';
How do I write that?
If you want to match Field_Name values that contain 'bla%bla&2', then you need to write this:
set define off
Select * From Some_Table Where Field_Name Like '%bla\%bla&2%' escape '\';
You get to specify which character you want to use to escape a following character (thanks should go to mathguy, not me). You also have to set define off to prevent sqlplus from trying to substitute values in a string.
If, however, you want to match Field_Name values that exactly equal the given string, then you do this instead:
set define off
Select * From Some_Table Where Field_Name = 'bla%bla&2';
If I am not mistakend you escape them with a backslash (\)
Select * From Some_Table Where Field_Name Like 'bla\%bla&2' ESCAPE '\';
Use escape \ to treat is a literal
SELECT *
FROM Some_Table
WHERE Field_Name LIKE 'blah\%'|| 'blah' ||'&'|| '2';
I'll guess that you're using a tool which treats &n, where n is a digit, as a variable marker. If you're using SQL*Plus or SQL Developer you'd need to issue the SQL*Plus command SET DEFINE OFF. Other tools may use other methods to accomplish this.
Best of luck.
I do not think the backslash escape character will work here for the ampersand. Instead, you will want to divide your search into concatenated strings using double pipes. Use single quotes around each piece of literal text. Next, replace the & with chr(38) which is the ampersand. You can see this with:
select chr(38) from dual
You will still want to include the backslash before the % and finish your statement with escape '\'. Notice, I did not quote the chr(38).
select * From Some_Table Where Field_Name Like 'bla\%bla'||chr(38)||'bla' escape '\'
How can I split a string by comma using oracle sql?
Here I have a column which has values like below
123Lcq
Lf32i
jkp32m
I want to split it by comma
1,2,3,L,c,q
L,f,3,2,i
j,k,p,3,2,m
You can achieve the desired output using REGEXP_REPLACE:
SELECT
rtrim(regexp_replace(text, '(.)', '\1,'), ',') result
FROM (
SELECT '123Lcq' text FROM dual UNION ALL
SELECT 'Lf32i' text FROM dual UNION ALL
SELECT 'jkp32m' text FROM dual)
You could use regexp_replace:
SELECT substr(regexp_replace(mycol, '(.)', ',\1'), 2)
FROM mytable
The regular expression finds every character, and those matching characters are then all prefixed with commas. Finally a simple substr is used to eliminate the first comma.
Note that trimming commas could be an alternative to substr, but the behaviour is different when the original value already has commas at the end of the string: when trimming, you also trim away these original commas.