oracle sql - query to find special chars - sql

Is there any SQL SELECT query that can be done in oracle to detect ascii characters such as LF, CR in fields? Basically any characters people have known to cause trouble in a oracle db environment in terms of breaking jobs/procedures.etc
I doubt this would work: - happy to use regex if possible
select * from table
where column like '%chr(13)%'

select * from table
where regexp_like(column, '(' || chr(13) || '|' || chr(10) || ')')
The regex used here is a form of (a|b|c) which matches the string if it contains a OR b OR c

Related

SQL join strings and coalesce()? [duplicate]

What does || do in SQL?
SELECT 'a' || ',' || 'b' AS letter
|| represents string concatenation. Unfortunately, string concatenation is not completely portable across all sql dialects:
ansi sql: || (infix operator)
mysql: concat ( vararg function ). caution: || means 'logical or' (It's configurable, however; thanks to #hvd for pointing that out)
oracle: || (infix operator), concat ( caution: function of arity 2 only ! )
postgres: || (infix operator)
sql server: + (infix operator), concat ( vararg function )
sqlite: || (infix operator)
hopefully the confusion is complete ...
SELECT 'a' || ',' || 'b' AS letter
will combine a letter.
The result become 'a,b'
It is a concat statement. It will concatenate the two strings.
Here is a helpful post!
What is the difference between "||" operator and concat function in Oracle?
In Oracle, SQLite3, and MySQL, it concatenates strings. Please see the Oracle documentation. The MySQL documentation.
Also, it's part of ANSI SQL, but read this for more information.
It's a concatenation operator. So you would get 'a,b' from that.
I think || will work on most RDBMS's. SQL Server requires the + operator (thanks to HVD for setting me straight!).
in oracle its a shortcut for concatenate
http://docs.oracle.com/cd/B19306_01/server.102/b14200/operators003.htm

How to use a column named as keyword in a DB2 concat function

So I got this table FOO which has a column of the type VARCHAR and is named COMMENT (which happens to be a reserved keyword).
When I am trying to use it in a CONCAT function in my select the result is NULL.
How can I fix this?
SELECT
CONCAT(CONCAT(CONCAT(CONCAT('{"NAME":"', NAME), '","COMMENT":"'), COMMENT),'"}')
FROM
SOMESCHEMA.FOO
I also tried to use " or ' around COMMENT, but then it is interpreted as a VARCHAR...
2nd I used ` but that happens to print me the following error.
[Code: -104, SQL State: 42601] ILLEGAL SYMBOL "`". SOME SYMBOLS THAT MIGHT BE LEGAL ARE:.
I also tried to add the SCHEMA and the TABLE name in front of the column like:
CONCAT(CONCAT(CONCAT(CONCAT('{"NAME":"', NAME), '","COMMENT":"'), SOMESCHEMA.FOO.COMMENT),'"}')
But no luck.
Did you try this?
SELECT CONCAT(CONCAT(CONCAT(CONCAT('{"NAME":"', NAME
), '","COMMENT":"'
), "COMMENT"
),
'"}')
FROM SOMESCHEMA.FOO
That is, double quotes only around the column name.
I would find this simpler to read using the infix operator:
SELECT '{"NAME":"' CONCAT NAME CONCAT '","COMMENT":"' CONCAT "COMMENT" CONCAT '"}'
FROM SOMESCHEMA.FOO
or:
SELECT '{"NAME":"' || NAME || '","COMMENT":"' || "COMMENT" || '"}'
FROM SOMESCHEMA.FOO
It seems like DB2 also accept the ANSI/ISO SQL || concatenation:
SELECT
'{"NAME":"' || NAME || '","COMMENT":"' || COMMENT || '"}'
FROM
SOMESCHEMA.FOO

What is the meaning of || '|' || in oracle?

I see a query with this "COUNT(DISTINCT(code || '|' description)":
SELECT ...,..., ...,NULL, COUNT(DISTINCT(code || '|' description)
FROM....
But Im not understanding what this "COUNT(DISTINCT(code || '|' description)" means? Do you know what it is? Thanks
In Oracle, the double pipe (||) stands for string concatenation. This:
select 'a' || 'b' from dual
Yields:
ab
When it comes to this expression: COUNT(DISTINCT(code || '|' description):
it is invalid sql code: parentheses are not balanced, and there is a missing concatenation operator; I suppose you meant COUNT(DISTINCT code || '|' || description)
the latter concatenates code and description with a | separator, and counts distinct resulting values
|| is the string concatenation operator
This code has a slight syntax error but looks like it was supposed to concat the code column with the description column values separated by a pipe
code, description
A123, code for blah blah
Becomes:
A123|code for blahblah
(If the sql is amended to code || '|' || description
Presumably you intend:
COUNT(DISTINCT code || '|' || description)
The purpose of this code is to count the unique combinations of code and description. COUNT(DISTINCT) in Oracle takes only one argument. So, if you want to count distinct combinations, you need to resort to tricks such as concatenating the values together.
This is putting the values together with a vertical bar in-between:
'NY' || '|' || 'New York' --> 'NY|New York'
The vertical bar is a separator that presumably does not occur (or only rarely occurs) in either code or description.

How to search new line char in oracle table?

I have an issue with a file that's created from data (in format of bytes) returned from the database.
the problem is that there is a few newlines in the file.
i would like to know if there is a way to write a where clause to check if some record has a newline character?
Using the CHR function to look for the ASCII value:
select *
from your_table
where instr(your_text_col, chr(10)) > 0;
If you want to search for carriage returns, that would be chr(13).
You can write something like:
SELECT id
FROM table_name
WHERE field_name LIKE '%'||CHR(10)||'%'
;
(|| is the concatenation operator; CHR(10) is the tenth ASCII character, i.e. a newline.)
Depending on the platform, a newline will generally either be a CHR(10) (Unix) or a CHR(13) followed by a CHR(10) (Windows). There are other options for other more esoteric platforms, but 99.999% of the time, it will be one of these two.
You can search the data in a column looking for one or both characters
SELECT instr( column_name, CHR(10) ) position_of_first_lf,
instr( column_name, CHR(13) || CHR(10) ) position_of_first_cr_lf
FROM table_name
WHERE instr( column_name, CHR(10) ) > 0
This worked the best for me.
select * from label_master where regexp_like (text, chr(10));

Trim Whitespaces (New Line and Tab space) in a String in Oracle

I need to trim New Line (Chr(13) and Chr(10) and Tab space from the beginning and end of a String) in an Oracle query. I learnt that there is no easy way to trim multiple characters in Oracle. "trim" function trims only single character. It would be a performance degradation if i call trim function recursivelly in a loop using a function. I heard regexp_replace can match the whitespaces and remove them.
Can you guide of a reliable way to use regexp_replace to trim multiple tabspaces or new lines or combinations of them in beginning and end of a String. If there is any other way, Please guide me.
If you have Oracle 10g, REGEXP_REPLACE is pretty flexible.
Using the following string as a test:
chr(9) || 'Q qwer' || chr(9) || chr(10) ||
chr(13) || 'qwerqwer qwerty' || chr(9) ||
chr(10) || chr(13)
The [[:space:]] will remove all whitespace, and the ([[:cntrl:]])|(^\t) regexp will remove non-printing characters and tabs.
select
tester,
regexp_replace(tester, '(^[[:space:]]+)|([[:space:]]+$)',null)
regexp_tester_1,
regexp_replace(tester, '(^[[:cntrl:]^\t]+)|([[:cntrl:]^\t]+$)',null)
regexp_tester_2
from
(
select
chr(9) || 'Q qwer' || chr(9) || chr(10) ||
chr(13) || 'qwerqwer qwerty' || chr(9) ||
chr(10) || chr(13) tester
from
dual
)
Returning:
REGEXP_TESTER_1: "Qqwerqwerqwerqwerty"
REGEXP_TESTER_2: "Q qwerqwerqwer qwerty"
Hope this is of some use.
This how I would implement it:
REGEXP_REPLACE(text,'(^[[:space:]]*|[[:space:]]*$)')
How about the quick and dirty translate function?
This will remove all occurrences of each character in string1:
SELECT translate(
translate(
translate(string1, CHR(10), '')
, CHR(13), '')
, CHR(09), '') as massaged
FROM BLAH;
Regexp_replace is an option, but you may see a performance hit depending on how complex your expression is.
You could use both LTRIM and RTRIM.
select rtrim(ltrim('abcdab','ab'),'ab') from dual;
If you want to trim CHR(13) only when it comes with a CHR(10) it gets more complicated. Firstly, translated the combined string to a single character. Then LTRIM/RTRIM that character, then replace the single character back to the combined string.
select replace(rtrim(ltrim(replace('abccccabcccaab','ab','#'),'#'),'#'),'#','ab') from dual;
TRANSLATE (column_name, 'd'||CHR(10)||CHR(13), 'd')
The 'd' is a dummy character, because translate does not work if the 3rd parameter is null.
For what version of Oracle? 10g+ supports regexes - see this thread on the OTN Discussion forum for how to use REGEXP_REPLACE to change non-printable characters into ''.
I know this is not a strict answer for this question, but I've been working in several scenarios where you need to transform text data following these rules:
No spaces or ctrl chars at the beginning of the string
No spaces or ctrl chars at the end of the string
Multiple ocurrencies of spaces or ctrl chars will be replaced to a single space
Code below follow the rules detailed above:
WITH test_view AS (
SELECT CHR(9) || 'Q qwer' || CHR(9) || CHR(10) ||
CHR(13) || ' qwerqwer qwerty ' || CHR(9) ||
CHR(10) || CHR(13) str
FROM DUAL
) SELECT
str original
,TRIM(REGEXP_REPLACE(str, '([[:space:]]{2,}|[[:cntrl:]])', ' ')) fixed
FROM test_view;
ORIGINAL FIXED
---------------------- ----------------------
Q qwer Q qwer qwerqwer qwerty
qwerqwer qwerty
1 row selected.
If at all anyone is looking to convert data in 1 variable that lies in 2 or 3 different lines like below
'Data1
Data2'
And you want to display data as 'Data1 Data2' then use below
select TRANSLATE ('Data1
Data2', ''||CHR(10), ' ') from dual;
it took me hrs to get the right output. Thanks to me I just saved you 1 or 2 hrs :)
In cases where the Oracle solution seems overly convoluted, I create a java class with static methods and then install it as a package in Oracle. This might not be as performant, but you will eventually find other cases (date conversion to milliseconds for example) where you will find the java fallback helpful.
Below code can be used to Remove New Line and Table Space in text column
Select replace(replace(TEXT,char(10),''),char(13),'')
Try the code below.
It will work if you enter multiple lines in a single column.
create table products (prod_id number , prod_desc varchar2(50));
insert into products values(1,'test first
test second
test third');
select replace(replace(prod_desc,chr(10),' '),chr(13),' ') from products where prod_id=2;
Output :test first test second test third
TRIM(BOTH chr(13)||chr(10)||' ' FROM str)
Instead of using regexp_replace multiple time use (\s) as given below;
SELECT regexp_replace('TEXT','(\s)','')
FROM dual;
Fowloing code remove newline from both side of string:
select ltrim(rtrim('asbda'||CHR(10)||CHR(13) ,''||CHR(10)||CHR(13)),''||CHR(10)||CHR(13)) from dual
but in most cases this one is just enought :
select rtrim('asbda'||CHR(10)||CHR(13) ,''||CHR(10)||CHR(13))) from dual
UPDATE My_Table
SET Mycolumn1 =
TRIM (
TRANSLATE (Mycolumn1,
CHR (10) || CHR (11) || CHR (13),
' '))
WHERE ( INSTR (Mucolumn1, CHR (13)) > 0
OR INSTR (Mucolumn1, CHR (10)) > 0
OR INSTR (Mucolumn1, CHR (11)) > 0);