Using SQL*Plus I'm trying to use a select union statement to combine the results of two columns from the same table like this:
select substr(startdate,4,3) milestone
from projects
union
select substr(enddate,4,3) milestone
from projects
Using the alias milestone for the column, but for some reason the result shows with the column name mil. It's being truncated for some reason, and I think the substr part is the problem since it grabs 3 characters from the stardate and enddate column. how should i fix this issue?
It's because you are using sql-plus. If I run the same query in my database GUI (PL/SQL developer) it works fine but in sqlplus it just truncates the column header to fit the data.
I'm no guru on sqlplus, but this fixes the issue:
SQL> column milestone format a20;
SQL> select substr(sysdate,4,3) milestone from dual;
results in:
MILESTONE
--------------------
OCT
You most likely need to set the column width.
COLUMN MILESTONE FORMAT A20
SET VERIFY ON
SET HEADING ON
SET PAGES 25
SET LINES 60
select substr(startdate,4,3) milestone
from projects
union
select substr(enddate,4,3) milestone
from projects
Does this Help-
SQL> set linesize 100
SQL> column milestone format a25
SQL> select substr(startdate,4,3) "milestone"
from projects
union
select substr(enddate,4,3) "milestone"
from projects;
Related
DUAL is a system table that is used to get constants and results of system functions.
However it only has one column named "dummy" and only a row with X value in it so this doesnt work:
My question is, is there a system table that can pull this trick off? A (single-column) table that returns a row regardless of how its one column is queried, with the value in the where clause.
What you want to do violates how SQL works. However, if you always wnat to return exactly one row from a "table", you can use aggregation with no group by:
select max(dummy)
from dual
where dummy = '5'
The returns value is NULL.
Perhaps it would be easier to suggest something if you explained what problem you are trying to solve.
As you asked:
A (single-column) table that returns a row regardless of how its one column is queried, with the value in the where clause.
SQL> select dummy from dual where dummy = '5' or 1 = 1;
D
-
X
SQL>
dual is a (single-column) table
this query returned a row
value ('5') is in its where clause
I presume that "regardless of how its one column is queried" is also satisfied
Why "where clause"?
SELECT '5' AS dummy FROM dual
DUMMY|
-----|
5 |
selecting from nothing in Oracle SQL happens while referencing the dual table, like
SELECT sysdate FROM dual;
Now I'd like to have a query that also works for PostgreSQL, but selecting from dual there isn't possible. I know that I can drop the whole FROM part, but then it won't work in Oracle.
I've also tried things like SELECT CURRENT_TIMESTAMP FROM VALUES(1)) V(C);, but Oracle can't do this, either.
So is there a way to select from nowhere without using the dual table in Oracle SQL?
Alternatively, create a table named dual in Postgres, made of 1 row and 1 column
create table dual as (select 1);
and you can use it in Postgres as you would in Oracle
select 'whatever' from dual;
?column?
-----------
whatever
sysdate in Oracle is a built-in function, i.e. not a database table column. You can use any table as long as the query returns precisely one row, e.g.
select sysdate from EMP where rownum < 2
If you have a small table, TABLE_B, with at least one row, you could try selecting a single row from it.
select sysdate from TABLE_B where rownum < 2;
The contents of the table don't matter because you won't be selecting any of its columns.
The below code generates a fake row in both Oracle and Postgres (10+), using the XMLTABLE function. This code is pretty weird but it doesn't require any custom objects.
--Generate a fake row in either Oracle or Postgres.
select *
from xmltable
(
--The expression syntax is different for Oracle and Postgres.
--Oracle can use a literal, Postgres must reference the XML.
--The string '' is null in Oracle but not null in Postgres.
case when '' is null then '1' else '/a' end passing '<a>1</a>'
columns test int path '.'
);
test
----
1
How can you execute the following Oracle/pl-sql code to display two columns side by side?
SELECT table_name, user_cons_columns.column_name
FROM user_cons_columns;
Currently, this is my output formatting:
This is the formatting I hope to see:
Solutions tried:
set long 1000
set linesize 200
Where long and linesize have been changed from 20 to 2000, unsuccessfully. I suspect it's just improper SQL code...but unsure. Thank you in advance!
This has nothing to do with the SQL code (and you should NOT change it, for example by truncating the strings in the SQL query, just to fix a formatting problem).
The issue is that the columns, in the table, are declared of a certain width, say VARCHAR2(1000), and then that's what SQL Plus will reserve by default. You can change that in SQL Plus itself, with SQL Plus commands. In this case, the COLUMN command.
SQL> column column_name format a30
SQL> column table_name format a30
These are SQL Plus commands, so don't end them in semicolon ( ; )
Change a30 to a40 if you want 40 characters per column. Etc.
It is not clear why, if in the output you wanted the table name to appear first, in the query you have the column name first. You should be able to fix that yourself. Also, if you select from just one table, there is no need to prefix column names with the table name. However, if you do, be consistent - do it for both columns. And if you do, it is better to give an alias to the table in the FROM clause, and use the alias in SELECT. These are all unrelated to your original question.
Select only the first N (20) characters from the column_name field.
SELECT SUBSTR(column_name, 1, 20) column_name, table_name
FROM user_cons_columns;
I have just created a new database and I'm at the stage now where I am looking to input my data. I've created the tables and the constraints work fine, and I have inserted 1 row successfully. However when running the query select * from mytable; it returns the table once for every field.
I have spent the past 2 hours researching why but cant find anything. Can anyone help please?
Thanks in advance.
I assume that you're using SQL*Plus and that the columns in your table are rather wide (say varchar2(1000) for example).
In this scenario, SQL*Plus's width is to small to horizontally display multiple columns, hence it displays them vertically.
You could get around this with
select
substr(col1, 1, 20) col1_,
substr(col2, 1, 20) col2_
...
from
table;
or, when still in SQL*Plus, with a column format command:
column format col1 a20
column format col2 a20
...
select * from table;
My database table contains one column of varchar2(20,10), but since it is a total amount field I need currency data type. (If I enter 1235.5 it should take it as 1235.50.)
I tried changing to type number(20,2), I am not able to do this with number(20,2) in Oracle 10g.
I guess you need not bother about insert, as long you have a precision of atleast 2. When you are fetching using select query, try using the select query as :
Select to_char(1234.4, 'fm9999999.00') from dual;
Select to_char(1234.45, 'fm9999999.00') from dual;
Select to_char(1234,'fm9999999.00') from dual;
Select to_char(1234.4567, 'fm9999999.00') from dual;