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;
Related
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".
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?
C.ADDRESS1 || ',' || C.ADDRESS2 || ',' || C.ADDRESS3
pa.address_line_1,
pa.address_line_2,
pa.address_line_3 from per_addresses_f pa ;
how to join three column as single address column with oracle SQL concatenate
How to write query for joining 3 columns as single column?
You can write the concatenation as you've written it, but you have to make sure that all the components are strings. If some of them isn't, put it in TO_CHAR function. It's also good to put an alias to your result of concatenated columns:
SELECT C.ADDRESS1 || ',' || C.ADDRESS2 || ',' || C.ADDRESS3 as concatenated_adress
FROM your_table c
EDIT : After question was edited with concrete columns that are used, table name and with desired alias stated in question columns:
SELECT pa.address_line_1 || ',' || pa.address_line_2 || ',' || pa.address_line_3
as "Emergency Address"
FROM per_addresses_f pa
You can do this:
CONCAT(C.ADDRESS1, ',', C.ADDRESS2, ',', C.ADDRESS3) AS newColumnName
Try this :
SELECT firstname ||' '|| lastname AS employee_name FROM employee;
You can also try nested CONCAT assuming all address_line fields have char datatype:
SELECT CONCAT(CONCAT(CONCAT(pa.address_line_1,','),CONCAT(pa.address_line_2,',')),pa.address_line_3) AS address FROM per_addresses_f pa
But clearly pipe operator as suggested by #Goran Kutlaca is much simpler.
There are multiple ways to Concate the column in BIP.
1.
SELECT pa.address_line_1 || ',' || pa.address_line_2 || ',' ||
pa.address_line_3
as "Emergency Address" FROM per_addresses_f pa
2.
SELECT
CONCAT(CONCAT(CONCAT(pa.address_line_1,','),CONCAT(pa.address_line_2,',')),pa.address_line_3)
AS address FROM per_addresses_f pa
3.
select
CONCAT(pa.address_line_1,',',pa.address_line_2,',',pa.address_line_3)
as address FROM per_addresses_f pa
I have been trying to select all columns from one table into a single column. The problem is I can't seem to get the foreign key "department_id" to show up. If I run this code:
select employee_id || ',' || ' ' || last_name || ',' || ' ' || job_id || ',' || ' ' ||TO_CHAR(hire_date, 'DD Mon YY' ) || ',' || ' '|| department_id
AS "The_Output"
FROM employee;
The information from the "department_id" simply doesn't show. While this code:
select employee_id || ',' || ' ' || last_name || ',' || ' ' || job_id || ',' || ' ' ||TO_CHAR(hire_date, 'DD Mon YY' ) || ',' || ' '|| department_id
AS "The_Output"
FROM employee, department;
gives me this error:
ORA-00918: column ambiguously defined
I have tried to UNION them but that didn't work
That is because department_id exists in both employee and department table. So use alias while selecting.
Also your query is missing the join condition, so you will get cartesian product from both tables. Use something like below.
SELECT E.EMPLOYEE_ID || ',' || ' ' || E.LAST_NAME || ',' || ' ' || E.JOB_ID || ',' || ' ' ||TO_CHAR(E.HIRE_DATE, 'DD Mon YY' ) || ',' || ' '|| E.DEPARTMENT_ID
AS "The_Output"
FROM EMPLOYEE E INNER JOIN DEPARTMENT D
ON E.DEPARTMENT_ID=D.DEPARTMENT_ID
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".