I have this value '45465,6464,654' And I want to remove second comma and string after it. So basically I want ''45465,6464' . I found a solution but its for Mssql. How can I make this query for Oracle I couldnt do it even with substring. Can you help me?
This for MSSQL;
`declare #S varchar(20) = '45465#6464#654';
select left(#S, charindex('#', #S, charindex('#', #S)+1)-1);`
You can use something very similar:
WITH s AS (SELECT '45465#6464#654' s FROM dual)
SELECT SUBSTR(s,1,INSTR(s,'#',1,2)-1) FROM s
or you can use regular Expression:
SELECT regexp_substr('45465#6464#654','([^#]*#)?[^#]*') from dual
Related
In my table, I have data like PAT5DSA-(ALRP), LAR6DAOP-(RAH) etc..But I want to remove the strings like -(xxxx) or -(xxx) which can be any alphabets inside braces. Tried using the below:
select replace(:code,'-(Aa-Zz)',null) from employee;
But this didn't work..Can anyone please help?
We can do a regex replacement using REGEXP_REPLACE:
SELECT REGEXP_REPLACE('PAT5DSA-(ALRP)', '-\(.*?\)', '')
FROM dual;
PAT5DSA
The plain replace() doesn't understand patterns. You could use a regular expression replace, e.g.:
-- CTE for sample data
with cte (code) as (
select 'PAT5DSA-(ALRP)' from dual
union all
select 'LAR6DAOP-(RAH)' from dual
)
select code, regexp_replace(code, '-\(.*?\)$') as result
from cte;
CODE RESULT
-------------- --------------
PAT5DSA-(ALRP) PAT5DSA
LAR6DAOP-(RAH) LAR6DAOP
This will remove anything inside a pair of parentheses which is preceded by a dash, at the end of the original string. If the parentheses to be removed could be anywhere in the string then remove the $.
Use INSTR and SUBSTR:
WITH cteVals AS (SELECT 'PAT5DSA-(ALRP)' AS COL_VAL FROM DUAL UNION ALL
SELECT 'LAR6DAOP-(RAH)' AS COL_VAL FROM DUAL)
SELECT SUBSTR(COL_VAL, 1, INSTR(COL_VAL, '-')-1)
FROM cteVals;
Best of luck.
I need help
i have records 123,456,789 in rows when i am execute like
this one is working
select * from table1 where num1 in('123','456')
but when i am execute
select * from table1 where num1 in(select value from table2)
no resultset found - why?
Check the DataType varchare2 or Number
try
select * from table1 where num1 in(select to_char(value) from table2)
Storing comma separated values could be the cause of problem.
You can try using regexp_substr to split comma.
First and foremost, an important thing to remember: Do not store numbers in character datatypes. Use NUMBER or INTEGER. Secondly, always prefer VARCHAR2 datatype over CHAR if you wish to store characters > 1.
You said in one of your comments that num1 column is of type char(4). The problem with CHAR datatype is that If your string is 3 characters wide, it stores the record by adding extra 1 space character to make it 4 characters. VARCHAR2 only stores as many characters as you pass while inserting/updating and are not blank padded.
To verify that you may run select length(any_char_col) from t;
Coming to your problem, the IN condition is never satisfied because what's actually being compared is
WHERE 'abc ' = 'abc' - Note the extra space in left side operator.
To fix this, one good option is to pad the right side expression with as many spaces as required to do the right comparison.The function RPAD( string1, padded_length [, pad_string] ) could be used for this purpose.So, your query should look something like this.
select * from table1 where num1 IN (select rpad(value,4) from table2);
This will likely utilise an index on the column num1 if it exists.
The other one is to use RTRIM on LHS, which is only useful if there's a function based index on RTRIM(num1)
select * from table1 where RTRIM(num1) in(select value from table2);
So, the takeaway from all these examples is always use NUMBER types to store numbers and prefer VARCHAR2 over CHAR for strings.
See Demo to fully understand what's happening.
EDIT : It seems You are storing comma separated numbers.You could do something like this.
SELECT *
FROM table1 t1
WHERE EXISTS (
SELECT 1
FROM table2 t2
WHERE ',' ||t2.value|| ',' LIKE '%,' || rtrim(t1.num1) || ',%'
);
See Demo2
Storing comma separated values are bound to cause problems, better change it.
Let me tell you first,
You have stored values in table2 which is comma seperated.
So, how could you match your data with table1 and table2.
Its not Possible.
That's why you did not get any values in result set.
I found the Solution using string array
SELECT T.* FROM TABLE1 T,
(SELECT TRIM(VALUE)AS VAL FROM TABLE2)TABLE2
WHERE
TRIM(NUM1) IN (SELECT COLUMN_VALUE FROM TABLE(FUNC_GETSTRING_ARRAY(TABLE2.VAL)))
thanks
I have a problem in updating a column in oracle which has a single quote.
The following example will clear the problem.
Lets Client name is Lucy'Mark
Now, I want to replace the Single quote with space
After output, it will be Lucy Mark
Now when I tried the following query it is not working as the query will be
select replace (Lucy'Mark , '''', '') from gen_clientvendor_m;
Please let me know the query.
I am using SQL developer
Use the column with client name and add space to the replace statement:
select replace (client_name , '''', ' ') from gen_clientvendor_m;
Multiple single quotes cause headache :) so - have a look at this option:
SQL> with test (name) as
2 (select q'[Lucy'Mark]' from dual)
3 select name,
4 replace(name, chr(39), ' ') result
5 from test;
NAME RESULT
--------- ---------
Lucy'Mark Lucy Mark
SQL>
This is a oracle output records with pipe delimiter
04/22/2015|695|1074795|CRUSE|AXDE|01/29/1963|88359|||||
I want to change like this
"04/22/2015"|"695"|"1074795"|"CRUSE"|"AXDE"|"01/29/1963"|"88359"|||||
What is the query in Perl?
I want to change like this
"04/22/2015"|"695"|"1074795"|"CRUSE"|"AXDE"|"01/29/1963"|"88359"|||||
I won't say this is an elegant way to do it, since it is always better to fix the source itself. In your case, whatever is generating the output, you could simply concatenate the double-quotation marks to the column names.
Anyway, you could do it in SQL using REPLACE and RTRIM:
SQL> WITH DATA AS(
2 SELECT '04/22/2015|695|1074795|CRUSE|AXDE|01/29/1963|88359|||||' str FROM dual
3 )
4 SELECT '"'||rtrim(REPLACE(REPLACE(str, '|', '"|"'), '""',''),'"') str FROM DATA;
STR
---------------------------------------------------------------------
"04/22/2015"|"695"|"1074795"|"CRUSE"|"AXDE"|"01/29/1963"|"88359"|||||
SQL>
check this
select '"04/22/2015"|"695"|"1074795"|"CRUSE"|"AXDE"|"01/29/1963"|"88359"|||||' as str from dual
STR
---------------------------------------------------------------------
"04/22/2015"|"695"|"1074795"|"CRUSE"|"AXDE"|"01/29/1963"|"88359"|||||
Using the Oracle to_char(number) function, is it possible to append ascii characters to the returned string?
Specifically, I need to add a percentage character to the returned string.
"select to_char(89.2244, '999G999G999G999G990D00') from dual" -->
returns "89.22". I need a format pattern that returns "89.22%".
I am using this through reports in Application Express, so cannot simply concatenate "%" to the query, i need to put it in the number format.
So you can't wrap the to_char with a CONCAT?
select concat(to_char(89.2244, '999G999G999G999G990D00'),'%') from dual
You can't do it right in the number format.
If you are able to change NLS_CURRENCY for you session, you can do the following:
SELECT TO_CHAR(1.2, '999G999G999G999G990D00L' /*, 'NLS_CURRENCY=%' */)
FROM dual
---
1,20%
Quick and dirty way:
select to_char(89.2244, '999G999G999G999G990D00L', 'NLS_CURRENCY=''%''') from dual;
SYS # orant11g >select to_char(89.2244, '999G999G999G999G990D00')||'%' from dual;
TO_CHAR(89.2244,'999G999
------------------------
89.22%
Just use the || bars instead of the concat function.