Subquery in select statement syntax error - sql

I am trying to run the below query and i keep getting the error:**ORA-01756: quoted string not properly terminated**
SELECT
InnerTable."Cycle ID",
(
SELECT REPLACE(SYS_CONNECT_BY_PATH(CF_ITEM_NAME,'//'),'//','/')
AS "Path1"
FROM CYCL_FOLD
START WITH CF_FATHER_ID = InnerTable."Cycle ID"
CONNECT BY PRIOR CF_ITEM_ID = CF_FATHER_ID
) as "path1",
InnerTable."CSR/RCQ Name",
TEST.TS_DESCRIPTION as "Test Case Description",
FROM
(-- few conditions here
) InnerTable INNER JOIN TEST ON InnerTable."Test Case ID" = TEST.TS_TEST_ID
Can any body tell me what is wrong with the syntax..Thanks so much..

This error means you have an odd number of single quotes.
The snippet you posted only contains balanced pairs of quotes - in the REPLACE(SYS_CONNECT_BY_PATH(CF_ITEM_NAME() chain. So the problem must lie in the excised part of your query:
-- few conditions here
Does the redaction include any string literals? If so check them.
This error is easy to spot with an IDE which supports SQL syntax highlighting.
" i don't find any error with the single or double quotes"
Me neither. Hmmm.
There is the possibility that the / is escaping the quote somehow. That will depend on you client settings. In SQL*Plus ESCAPE is off by default and besides the default escape character is \. So your statement would work on my set-up ...
SQL> sho escape
escape OFFSQL> select '//' from dual
2 /
'/
--
//
SQL> c.//./
1* select '/' from dual
SQL> r
1* select '/' from dual
'
-
/
SQL> set escape on
SQL> r
1* select '/' from dual
'
-
/
SQL>
... but check your client settings anyway.

Related

How to remove hidden double quotes in column values of SQL Developer

Some of the column values in my table are enclosed within double quotes and I need to remove those double quotes. The problem is, I am not able to view the double quotes symbol in SQL Developer (version 4.2.0.16.260). But when I copy the data to Notepad++, there I am getting double quotes. I tried instr to get the position of double quotes, its returning the result 0. Also I tried to replace double quotes with blank, but they are not being removed.
Below is the data I see when copied to Notepad++. 2 records are displayed. I'm trying to take the distinct value from these 2 values. But I am not able to.
"Testdata
"
Testdata
The column value in the database table does not have double quotes.
When you copy the data from the results grid, SQL Developer is adding them as part of the copy operation, to help you out. (There's probably way to ask it not to, but I can't immediately see one.) It's doing that because the first value you're getting has a trailing new line character. I can duplicate what you're seeing if I do:
select 'Testdata' || chr(10) from dual
union all
select 'Testdata' from dual;
If I run as a script, the script output window shows:
'TESTDATA
---------
Testdata
Testdata
Here the newline is lost, and copy and pasting from that doesn't preserve it. If I run as a statement the data in the query result window looks the same:
but if I copy that data from the grid and paste it (anywhere, not just in Notepad++) I also see:
"Testdata
"
Testdata
... so the newline is preserved, and is enclosed in double-quotes so whatever it is pasted into (I'm guessing this is targeted at Excel) understands that it is a single value, including that newline character.
Im trying to take the distinct value from these 2 values
The problem is that they are not, in fact, distinct; one has a newline, the other does not.
If you want to ignore that and treat them as the same you can trim off the trailing newline:
select distinct rtrim(col, chr(10))
from your_table;
Demo with the same sample data:
-- CTE for sample data
with your_table (col) as (
select 'Testdata' || chr(10) from dual
union all
select 'Testdata' from dual
)
select col
from your_table;
COL
---------
Testdata
Testdata
-- CTE for sample data
with your_table (col) as (
select 'Testdata' || chr(10) from dual
union all
select 'Testdata' from dual
)
select distinct rtrim(col, chr(10)) as col
from your_table;
COL
---------
Testdata
Please try this out
SELECT REPLACE(TRIM(column_name), CHR(13)||CHR(10))
FROM table_name;

Escaping single quotes in REDSHIFT SQL

I've lots of string values containing single quotes which I need to insert to a column in REDSHIFT table.
I used both /' and '' to escape the single quote in INSERT statement.
e.g.
INSERT INTO table_Temp
VALUES ('1234', 'O\'Niel'), ('3456', 'O\'Brien')
I also used '' instead of \' but it keeps giving me error that "VALUES list must of same length" i.e. no: of arguments for each record >2.
Can you let know how to have this issue resolved?
The standard in SQL is double single quotes:
INSERT INTO table_Temp (col1, col2) -- include the column names
VALUES ('1234', 'O''Niel'), ('3456', 'O''Brien');
You should also include the column names corresponding to the values being inserted. That is probably the cause of your second error.
You could use CHR(39) and concat the strings. Your name would look like below:
('O' || CHR(39)||'Brian')
I think it may depend on your environment. I'm using Periscope Data's redshift SQL editor, and \ worked as an escape character. '' and \\ did not work.
I was facing similar problem , I was needing send a kind of JSON structure to then decode it into my query but there was a program receiving my string and this program was escaping my escapes, so the query fails, finally I found this :
Put $$ in dollar-quoted string in PostgreSQL
mentioning quote_literal(42.5)
https://www.postgresql.org/docs/current/functions-string.html#FUNCTIONS-STRING-OTHER
This resolves my issue . an example
String is
'LocalTime={US/Central}; NumDays={1}; NumRows={3}; F_ID={[Apple, Orange, Bannana]}'
Select
Param, value , replace(quote_literal(replace(replace(Value,'[',''),']','')),',',quote_literal(',')) ValueList
FROM (
select
SPLIT_PART(split,'=',1) as Param,
replace( replace(SPLIT_PART(split,'=',2),'{',''),'}','') as Value
FROM
(
select
trim(split_part(freeform.txt, ';', number.n)) as split
from
( select
'LocalTime={US/Central}; NumDays={1}; NumRows={3}; F_ID={[Apple, Orange, Bannana]}' as txt
) freeform,
( select 1 as n union all
select 2 union all
select 3 union all
select 4 union all
select 5 union all
select 6 union all
select 7 union all
select 8 union all
select 9 union all
select 10
) number
where split <> ''
) as MY_TMP
) as valuePart
use \\' to escape '
s = s.replace("'", "\\'")

remove & symbolic from Query

we have Oracle query running in Informatica SQ transformation, I have given my query below
SELECT
CAST(T.COLUMN_VALUE.EXTRACT('//text()') AS VARCHAR2(200))
FROM
(SELECT regexp_replace('assaley&lee#direct.wvhin.org','(!##$%^\&*()_+=)*','') as RECIPIENTS FROM DUAL) T1,
TABLE( xmlsequence( XMLTYPE( '<x><x>' || REPLACE(t1.RECIPIENTS, ',', '</x><x>') || '</x></x>' ).EXTRACT('//x/*'))) t
where length(T1.RECIPIENTS) =28 -- and length(T1.RECIPIENTS) > 25
if i run this above query prompting some user input due '&' this symbolic reference, i should turnoff this prompt step.
could any one help me with this one?
Note: 'assaley&lee#direct.wvhin.org' this value is hard-coded value.
Thanks
Pandia
One method is to use UNISTR function and hardcode& as unicode literal: \0026:
https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions204.htm
SELECT unistr( 'assaley\0026lee#direct.wvhin.org' ) x
from dual;
X
----------------------------
assaley&lee#direct.wvhin.org
Regardless of the issue you are currently experiencing, the code should be corrected.
Within the regular expression you should use square brackets instead or round brackets.
Round brackets are intend for expression capturing.
Square brackets are intend for characters set.
SELECT regexp_replace('assaley&lee#direct.wvhin.org','[!##$%^\&*()_+=]+','') as RECIPIENTS
FROM DUAL
;
Or -
remove anything except for letters and digits:
SELECT regexp_replace('assaley&lee#direct.wvhin.org','[^[:alpha:][:digit:]]','') as RECIPIENTS
FROM DUAL
;

Select statement with column contains '%'

I want to select names from a table where the 'name' column contains '%' anywhere in the value. For example, I want to retrieve the name 'Approval for 20 % discount for parts'.
SELECT NAME FROM TABLE WHERE NAME ... ?
You can use like with escape. The default is a backslash in some databases (but not in Oracle), so:
select name
from table
where name like '%\%%' ESCAPE '\'
This is standard, and works in most databases. The Oracle documentation is here.
Of course, you could also use instr():
where instr(name, '%') > 0
One way to do it is using replace with an empty string and checking to see if the difference in length of the original string and modified string is > 0.
select name
from table
where length(name) - length(replace(name,'%','')) > 0
Make life easy on yourselves and just use REGEXP_LIKE( )!
SQL> with tbl(name) as (
select 'ABC' from dual
union
select 'E%FS' from dual
)
select name
from tbl
where regexp_like(name, '%');
NAME
----
E%FS
SQL>
I read the documentation mentioned by Gordon. The relevent sentence is:
An underscore (_) in the pattern matches exactly one character (as opposed to one byte in a multibyte character set) in the value
Here was my test:
select c
from (
select 'a%be' c
from dual) d
where c like '_%'
The value a%be was returned.
While the suggestions of using instr() or length in the other two answers will lead to the correct answer, they will do so slowly. Filtering on function results simply take longer than filtering on fields.

Oracle REGEXP_SUBSTR not working with my pattern

I have the following query:
SELECT DISTINCT A.REZ FROM
(
SELECT REGEXP_SUBSTR(P_EQUATION, '([A-Z|a-z|0-9]+)\{([0-9|\+|\-| |\*|\/\)\(]+)\}#([A-Z|a-z|0-9|_]+)#',1, LEVEL) AS REZ FROM DUAL
CONNECT BY REGEXP_SUBSTR(P_EQUATION, '([A-Z|a-z|0-9]+)\{([0-9|\+|\-| |\*|\/\)\(]+)\}#([A-Z|a-z|0-9|_]+)#',1, LEVEL) IS NOT NULL
) A;
If I supplied the following input:
P_EQUATION := 'A123{(01+02)*2}#ACCOUNT_BALANCE# + B123{(20+10)/20}#ACCOUNT_BALANCE#';
It gives me the following:
REZ
-------------------------------------
A123{(01+02)*2}#ACCOUNT_BALANCE#
B123{(20+10)/20}#ACCOUNT_BALANCE#
But, although the minus sign is included in the pattern, if I have added it inside the curly brackets, it will not recognize the text anymore as a match!
ex:
P_EQUATION := 'A123{(01-02)*2}#ACCOUNT_BALANCE#';
I'm not able to find a solution to this, it is freaking me out, especially, when I tried to match the minus sign alone it works, if I tried to match digits alone it also works :(
Oracle appears to be using POSIX style regexes: https://docs.oracle.com/cd/B12037_01/server.101/b10759/ap_posix001.htm#i690819
The backslash is NOT a metacharacter in a POSIX bracket expression. So in POSIX, the regular expression [\d] matches a \ or a d
> http://www.regular-expressions.info/posixbrackets.html
The backslashes are probably messing it up, and they're not necessary. You also don't realize that | is a literal inside a char class (which the comments also pointed out). I have fixed these problems, and I moved the - to the start of the char class, which allows it to be interpreted as a literal.
Here you go:
([A-Za-z0-9]+)\{([-0-9+ */)(]+)\}#([A-Za-z0-9_]+)#
Couldn't quite figure out the issue with your code but here is one way to do it:
with temp as
(
select 'A123{(01+02)*2}#ACCOUNT_BALANCE# + B123{(20+10)/20}#ACCOUNT_BALANCE#' P_EQUATION from dual union all
select 'A123{(01-02)*2}#ACCOUNT_BALANCE#' P_EQUATION from dual
)
SELECT DISTINCT A.REZ FROM
(
SELECT REGEXP_SUBSTR(P_EQUATION, '[[:alpha:]]+[[:digit:]]+{\([[:digit:]]+\S[[:digit:]]+\)\S[[:digit:]]+}#[[:alpha:]]+_[[:alpha:]]+#',1, LEVEL) AS REZ FROM temp
CONNECT BY REGEXP_SUBSTR(P_EQUATION, '[[:alpha:]]+[[:digit:]]+{\([[:digit:]]+\S[[:digit:]]+\)\S[[:digit:]]+}#[[:alpha:]]+_[[:alpha:]]+#',1, LEVEL) IS NOT NULL
) A;
OUTPUT:
REZ
---------------------------------------
B123{(20+10)/20}#ACCOUNT_BALANCE#
A123{(01-02)*2}#ACCOUNT_BALANCE#
A123{(01+02)*2}#ACCOUNT_BALANCE#