ORA-00923 FROM keyword not found where expected - sql

I am trying to concatenate some fields to return a single string for each row from an oracle table. This is in 10g. Here is my query:
SELECT t.value || '|' || t.label || '|' t.label_abbrv || '||' "mylist"
FROM list_value t
WHERE t.value BETWEEN 195001 AND 195300;
I'm getting the "FROM keyword not found where expected" error. This is really annoying. It's a simple query. I'm sure it's something simple I'm missing.

If you used SQLPLUS client, it would have saved you a little time:
SQL> SELECT value || '|' || label || '|' label_abbrv || '||' "mylist"
2 from list_value where (value between 195001 and 195300);
SELECT value || '|' || label || '|' label_abbrv || '||' "mylist"
*
ERROR at line 1:
ORA-00923: FROM keyword not found where expected
You can break up your query to multiple lines to isolate the problem:
SQL> edit
Wrote file afiedt.buf
1 SELECT value || '|'
2 || label ||
3 '|' label_abbrv ||
4 '||' "mylist"
5 from list_value
6 where
7* (value between 195001 and 195300)
SQL> /
'|' label_abbrv ||
*
ERROR at line 3:
ORA-00923: FROM keyword not found where expected
You might find SQLPLUS to be "primitive," but, hmmm, that's good for another question. Let me see if anyone else has asked about it yet.

D'oh! I found the problem. I'm missing a concat!
SELECT value || '|' || label || '|' ****||**** label_abbrv || '||' "mylist"
from list_value where (value between 195001 and 195300);

I think your answer to your own question is still wrong - it should be:
SELECT value || '|' || label || '|' || label_abbrv || '||' "mylist"
^^^^

Related

Formatting Spaces, punctuation marks in Oracle SQL [duplicate]

So I basically wanna display this (whole row in ONE column):
I like [type column] cake with [icing column] and a [fruit column].
The result should be:
Cake_Column
----------------
I like chocolate cake with whipped_cream and a cherry.
I like strawberry cake with vanilla_cream and a lemon_slice.
etc.
etc.
I need some sort of TO_CHAR statement that does ([column] "some text" [column]) "new_column_name";
What am I supposed to know?
You have two options for concatenating strings in Oracle:
CONCAT
Using ||
CONCAT example:
CONCAT(
CONCAT(
CONCAT(
CONCAT(
CONCAT('I like ', t.type_desc_column),
' cake with '),
t.icing_desc_column),
' and a '),
t.fruit_desc_column)
Using || example:
'I like ' || t.type_desc_column || ' cake with ' || t.icing_desc_column || ' and a ' || t.fruit_desc_column
Did you try the || operator ?
Concatenation Operator Documentation from Oracle >>>
select 'i like' || type_column || ' with' ect....
Below query works for me #Oracle 10G ----
select PHONE, CONTACT, (ADDR1 || '-' || ADDR2 || '-' || ADDR3) as Address
from CUSTOMER_DETAILS
where Code='341';
O/P -
1111 abc#gmail.com 4th street-capetown-sa
The Oracle/PLSQL CONCAT function allows to concatenate two strings together.
CONCAT( string1, string2 )
string1
The first string to concatenate.
string2
The second string to concatenate.
E.g.
SELECT 'I like ' || type_column_name || ' cake with ' ||
icing_column_name || ' and a ' fruit_column_name || '.'
AS Cake FROM table;
Try this:
SELECT 'I like ' || type_column_name || ' cake with ' ||
icing_column_name || ' and a ' fruit_column_name || '.'
AS Cake_Column FROM your_table_name;
It should concatenate all that data as a single column entry named "Cake_Column".

I have a plsql procedure which gives a table type output. I need to get all the rows in json format using a select statement but it is returning null

This is what I tried.
I have a plsql procedure that gives a table type output. So I created a plsql function to call the procedure and convert the output to a clob (tried VARCHAR2 as well) which contains the json formatted table rows. I tested the function using a plsql script and it is returning the expected result but when I'm trying to call the function using a select statement, I'm getting a null value every time. Please help.
Procedure signature is like procedure(p1 IN, p2 IN, ..., x OUT table)
Function is like
func(p1 IN, p2 IN, ... ) return CLOB
AS res CLOB;
t table;
BEGIN
proc(p1, p2,....,t)
FOR cursor1 IN (SELECT * FROM TABLE(CAST(t AS table)))
LOOP
res := res || '{ ' ||
'"column1" : ' || '"' || cursor1.column1 || '"' || ',' ||
'"column2" : ' || '"' || cursor1.column2 || '"' || ',' ||
'"column3" : ' || '"' || cursor1.column3 || '"' || ',' ||
'"column4" : ' || '"' || cursor1.column4 || '"' || ',' ||
'}' || ',';
END LOOP;
return res;
END
Did you try json objects of oracle?

Oracle 12c Concatenate with brackets where nulls are involved

We have a set of columns within a table we need to concatenate, and we need brackets around the third, fourth, fifth and sixth value, but also need nothing to appear if the column is null.
SELECT "ID",
NVL(PART || '.'|| SECTION ||'(' ||SUB1||')'|| '(' ||SUB2|| ')' || '('||SUB3||')' || '('||SUB4||')', '') as concatenated
FROM table1;
Places the values exactly right as long as there are values. When any one or more columns return null, we are getting an empty set of brackets for each null value.
Such as: 113.203()()()() when there are four null values
in this case we would need: 113.203
Or 113.450(h)(2)(iv)() when there is one null value.
here the desired results
would be 113.450(h)(2)(iv)
How can I change the script to leave out all the empty brackets when a null value is returned?
Thank you.
Hmmm, I think you want:
select id,
(part || '.' || section ||
(case when sub1 is not null then '(' || sub1 || ')' end) ||
(case when sub2 is not null then '(' || sub2 || ')' end) ||
(case when sub3 is not null then '(' || sub3 || ')' end) ||
(case when sub4 is not null then '(' || sub4 || ')' end)
) as concatenated
from table1;

format column headers during concat,oracle

I need to format column headers in the output of sql while using concat
Eg:
SELECT '' || to_char(sysdate,'ddmmyyyy') as DATE || ',' || ENO|| ',' || NAME|| ''
FROM EMP;
would retrieve me
ORA-00923: FROM keyword not found where expected.
Need the output as:
DATE ENO NAME
-----------------
251013 7560 RAM
251013 7561 ROSS
This format works
SELECT to_char(sysdate,'ddmmyyyy') || ',' || ENO || ',' || NAME as "DATE,ENO,NAME"
FROM EMP
but I have an issue with
ORA-00972: identifier is too long
when the length of column names inside as "" exceeds 30 characters
Eg:
SELECT to_char(sysdate,'ddmmyyyy') || ',' || ENO || ',' || NAME ||
',' || EMPLOYEE_IDENTIFICATION_NUMBER as "DATE,ENO,NAME,EMPLOYEE_IDENTIFICATION_NUMBER"
FROM EMP;
To achieve this output you have to build your query like this
SELECT to_char(sysdate,'ddmmyyyy') || ',' || ENO || ',' || NAME as "DATE,ENO,NAME" FROM EMP
You need to move the alias, if you really need it, at the end of the SELECT clause. Also the empty strings ('') can be removed:
SELECT to_char(sysdate,'ddmmyyyy') || ',' || ENO || ',' || NAME as DATE FROM EMP;

Concatenation of two columns in Oracle [duplicate]

So I basically wanna display this (whole row in ONE column):
I like [type column] cake with [icing column] and a [fruit column].
The result should be:
Cake_Column
----------------
I like chocolate cake with whipped_cream and a cherry.
I like strawberry cake with vanilla_cream and a lemon_slice.
etc.
etc.
I need some sort of TO_CHAR statement that does ([column] "some text" [column]) "new_column_name";
What am I supposed to know?
You have two options for concatenating strings in Oracle:
CONCAT
Using ||
CONCAT example:
CONCAT(
CONCAT(
CONCAT(
CONCAT(
CONCAT('I like ', t.type_desc_column),
' cake with '),
t.icing_desc_column),
' and a '),
t.fruit_desc_column)
Using || example:
'I like ' || t.type_desc_column || ' cake with ' || t.icing_desc_column || ' and a ' || t.fruit_desc_column
Did you try the || operator ?
Concatenation Operator Documentation from Oracle >>>
select 'i like' || type_column || ' with' ect....
Below query works for me #Oracle 10G ----
select PHONE, CONTACT, (ADDR1 || '-' || ADDR2 || '-' || ADDR3) as Address
from CUSTOMER_DETAILS
where Code='341';
O/P -
1111 abc#gmail.com 4th street-capetown-sa
The Oracle/PLSQL CONCAT function allows to concatenate two strings together.
CONCAT( string1, string2 )
string1
The first string to concatenate.
string2
The second string to concatenate.
E.g.
SELECT 'I like ' || type_column_name || ' cake with ' ||
icing_column_name || ' and a ' fruit_column_name || '.'
AS Cake FROM table;
Try this:
SELECT 'I like ' || type_column_name || ' cake with ' ||
icing_column_name || ' and a ' fruit_column_name || '.'
AS Cake_Column FROM your_table_name;
It should concatenate all that data as a single column entry named "Cake_Column".