Query:
select * from tableA;
result:
column1 column2
------- -------
row1-1 row1-2
row2-1 row2-2
row3-1 row3-1
How can I turn the result above into something below.
column1|column2
------- -------
row1-1 | row1-2
row2-1 | row2-2
row3-1 | row3-1
The assumptions here is that the user will not know what are the columns in the table in advance. This means that using || to do string concatenation on the result set is not preferred.
select column1 ||,|| column2 from tableA <---- This is not what I am looking for.
Note: These tables are already created and I do not have the admin rights to modify the table schema.
If you are running your query in sqlplus, you can do
set colsep '|'
before your query and you should get the output as listed, though the header separator line will also have the same column separator -------|-------
This will apply to all subsequent queries
Related
Suppose if the table name is ABC_XYZ_123. I want to extract the integer values after _.
The output should be integer values after _.
In the above case, the output should be 123.
I have used the below sql query.
select from table_name like 'XXX_%';
But I am not getting required output. Can anyone help me with this query.
Thanks
Using REGEXP_SUBSTR with a capture group we can try:
SELECT REGEXP_SUBSTR(name, '_(\d+)$', 1, 1, NULL, 1)
FROM yourTable;
The question is somewhat unclear:
it looks as if you're looking for table names that contain number at the end, while
query you posted suggests that you're trying to select those numbers from one of table's columns
I'll stick to
Suppose if the table name is ABC_XYZ_123
If that's so, it is the data dictionary you'll query. USER_TABLES contains that information.
Let's create that table:
SQL> create table abc_xyz_123 (id number);
Table created.
Query selects numbers at the end of table names, for all my tables that end with numbers.
SQL> select table_name,
2 regexp_substr(table_name, '\d+$') result
3 from user_tables
4 where regexp_like(table_name, '\d+$');
TABLE_NAME RESULT
-------------------- ----------
TABLE1 1
TABLE2 2
restore_point-001 001
ABC_XYZ_123 123 --> here's your table
SQL>
Apparently, I have a few of them.
I have oracle 12c and trying to export query result to csv or text file but I dont want any enclosure of my data. I have tried SET SQLFORMAT csv which creates csv file but data comes in double quotes then I tried SET SQLFORMAT delimited | but that also comes with double quoted. I also tried SET MARKUP csv on delimeter | quote off it also gave me same result. I dont think MARKUP command works on 12c but it did not give me error. Here is my script:
SET SQLFORMAT delimited | ;
spool 'C:\Temp\MyResults.csv';
select 1 AS Col1, 'Data Line 1' AS Col2 from dual UNION select 2 AS Col1, 'Data Line 2' AS Col2 from dual;
spool off;
This gives me result:
"COL1"|"COL2"
1|"Data Line 1"
2|"Data Line 2"
But I want without double quotes on string data.
COL1|COL2
1|Data Line 1
2|Data Line 2
I would appreciate if someone can give me any poption.
Thanks.
I have the following working with Oracle 19 (client and server on Linux):
SQL> set markup CSV on quote off
SQL> desc t;
Name Null? Type
----------------------------------------- -------- ----------------------------
OBJECT_ID NUMBER
OBJECT_NAME VARCHAR2(128)
SQL> select * from t where rownum=1;
OBJECT_ID,OBJECT_NAME
16,TS$
SQL> set markup csv on quote on
SQL> select * from t where rownum=1;
"OBJECT_ID","OBJECT_NAME"
16,"TS$"
SQL>
Suppose I have a table definition as follows in Hive(the actual table has around 65 columns):
CREATE EXTERNAL TABLE S.TEST (
COL1 STRING,
COL2 STRING
)
PARTITIONED BY (extract_date STRING)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\007'
LOCATION 'xxx';
Once the table is created, when I run hive -e "describe s.test", I see extract_date as being one of the columns on the table. Doing a select * from s.test also returns extract_date column values. Is it possible to exclude this virtual(?) column when running select queries in Hive.
Change this property
set hive.support.quoted.identifiers=none;
and run the query as
SELECT `(extract_date)?+.+` FROM <table_name>;
I tested it working fine.
I'd like to concatenate values from 2 selected columns and use a result as table name for another select statement:
select a.ColumnA,
a.ColumnB,
b.ColumnG,
(a.ColumnA || '.' || a.ColumnB) "TABLENAME"
(select t.ColumnX from TABLENAME t where t.ColumnY = 'whatever') "GOAL"
from
table a,
table b,
where
....
So assuming that
table a:
ColumnA ColumnB ColumnC ...
dev town 15
table b:
ColumnF ColumnG ColumnH ...
aaa bbb ccc
somewhere there exists table town in schema dev that can be queried using name dev.town:
table dev.town:
ColumnX ColumnY ColumnZ ...
Joe whatever Mr
So "my query" returns
ColumnA ColumnB ColumnG TABLENAME GOAL
--------------------------------------
dev town bbb dev.town Joe
Is there a way to get the results I need?
Thanks.
Not in an SQL statement in Oracle.
If you dived in PL/SQL, then you could use an EXECUTE IMMEDIATE statement to dynamically generate the required table.
Currently I'm using Birt Report to generate report from my system. I'm using input parameter to send parameter from my system to Birt Report. The problem is when I'm trying to send multiple parameter from my system.
Example: SQL statement at Birt Report for one parameter
select column1 from table1 where column2 = ?
When I use this query for one parameter, it work.
What I'm trying to achieve now,
select column1 from table1 where column2 = ? AND column1 = ?
I tried SQL like this
select column1 from table1 where column2 = params["column2"].value AND column1 = params["column1"].value
I already created parameter at data set and report parameter but it's still can't gets to work
No difference if you have one or multiple parameters.
In the 'Edit Data Set' dialog add your query in the 'Query' section with ?
then switch to the 'Parameters' section and add parameters with 'Default Value' params["prmName"].value or even simpler 'Linked To Report Parameter' prmName
e.g. for your example
Query
select column1 from table1 where column2 = ? AND column1 = ?
Parameters
Name Data Type Direction Default Value Linked To Report Parameter
column2 Decimal Input column2
column1 Decimal Input column1
You can move the parameters up and down. Parameters are matched to the ? in your query by this order. The Name is irelevant...