On Oracle, to set the output more nice using sqlplus we did this
column author format a15
column editor format a15
column title format a15...
The question is simple: set column size for 3 column is easy, but if we had 7 or more column? Is possible to set a default column size for all columns?
Well, it is possible - for numbers. You have 3 options available:
COLUMN FORMAT takes precedence over
SET NUMFORMAT which takes precedence over
SET NUMWIDTH
For example:
SQL> set numwidth 2
SQL> select sal from emp where rownum <= 2;
SAL
---
##
##
SQL> set numformat $9990d0
SQL> select sal from emp where rownum <= 2;
SAL
--------
$1000,0
$1600,0
SQL> col sal format $999g990d00
SQL> select sal from emp where rownum <= 2;
SAL
------------
$1.000,00
$1.600,00
SQL>
No such luck for other datatypes, though.
No. By default, the display length of a string column is the size of the column in the database. So if a column is defined, for example, as varchar2(100), then SQL*Plus reserves 100 characters in the resultset. Specific rules apply for datatypes such as dates, CLOB or else, but this is probably not what you are asking for here.
If you want to change that, it needs to be done on a column-per-column basis.
From the documentation:
The default width of datatype columns is the width of the column in the database.
You can change the displayed width of a datatype or DATE, by using the COLUMN command with a format model consisting of the letter A (for alphanumeric) followed by a number representing the width of the column in characters.
Within the COLUMN command, identify the column you want to format and the model you want to use.
Related
Problem: I want to get an average salary of all employees. The query itself is rather simple:
Question: Is it possible to move COUNT(salary) to selected column's title (after AS statement), so the name of the column will be "Average salary of 20 employees"? I've been trying several approaches, but none of them worked. I would really appreciate any help. Thank you in advance.
Screenshot you posted looks like Apex SQL Workshop. I don't know how to do it there, but - if you used SQL*Plus, then you could do something like this (not exactly as you wanted it, though):
SQL> set ver off
SQL> column cnt_emps new_value l_cnt
SQL> select count(*) cnt_emps from emp;
CNT_EMPS
----------
14
SQL> select avg(sal) as "Average salary of &l_cnt employees" from emp;
Average salary of 14 employees
--------------------------------------
2073.21429
SQL>
However, if you're already using Apex, switch to GUI and create a page which will let you format the result any way you want (using colors, large font, bold letters, whatever). SQL*Plus and its descendants are kind of restrictive when such things should be done.
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;
how do I query a table for a column that is clob type? I need to query the table for the column for a certain string (in addition to other conditions - I might need to use Case statement), my initial idea was to query the data in the sub-query and then find a match using Case in the top query. however I am now stuck as I am not sure how to query clob type data in Select!
Edit:
the clob column in the table is set of paragraphs, and the string I am trying to search for may be in any location in the paragraph. And I am unsure of the size of the clob.
Hope this examples illustrates clearly what i am trying to explain.
SET SQLBL ON;
SET DEFINE OFF;
CREATE TABLE CLOB_TEST
(
clob_in CLOB
);
INSERT
INTO CLOB_TEST VALUES
(
'I am working as a DBA and senior database resource in L&T Infotech in Pune India'
);
SELECT DBMS_LOB.SUBSTR(CLOB_IN,3000) ot FROM CLOB_TEST;
-----------------------------OUTPUT------------------------------------------
OT
I am working as a DBA and senior database resource in L&T Infotech in Pune India
-----------------------------OUTPUT------------------------------------------
In many respects, the same way you "query a column" (odd terminology!) of type varchar2.
table structure:
SQL> describe t
Name Null? Type
------------------------- -------- --------------------------------------------
COL1 VARCHAR2(20)
COL2 CLOB
table content:
SQL> select * from t;
COL1 COL2
-------------------- -------------------------------------------------------
abc afdreed
azx; ffare21
query the table (where clause on the CLOB column):
SQL> select * from t where col2 like '%dre%';
COL1 COL2
-------------------- -----------------------------------------------------------
abc afdreed
I would suggest you have a look on the package DBMS_LOB (link). It has some functions useful for working with LOB
for example:
Substr - The function returns amount bytes or characters of a LOB, starting from an absolute offset from the beginning of the LOB.
Insrt - This function returns the matching position of the nth occurrence of the pattern in the LOB, starting from the offset you specify.
GetLength - This function gets the length of the specified LOB. The length in bytes or characters is returned.
Trim - This procedure trims the value of the internal LOB to the length you specify in the newlen parameter. Specify the length in bytes for BLOBs, and specify the length in characters for CLOBs.
Also you can try to work with string functions. Some of them are able to work with not huge lob files.
How to do I get column names without getting truncated in SQLPLUS in Unix for select statement. This might look like duplicate question, but I have been searching for hours but couldn't find a convenient solution.
So far what I have found is
COLUMN COLUMN_NAME FORMAT SIZE;
Or
SELECT COLUMN1|| ',' || COLUMN2 || ',' || COLUMN3 FROM TABLE;
Both involves hardcoding,Is there any simpler solution without hardcoding.
Sorry for making it hard
Query: select * from Employee;
It has column names as Name,Salary,Age
What I get is:
Name Sala Ag
Steve 1000 30
John 2000 25
What I want is:
Name Salary Age
Steve 1000 30
John 2000 25
Setting size (width) of a column in SQL*Plus output.
SQL> column sex format a5
Seeing the current settings in effect.
SQL> column
Getting further help on usage.
SQL> help column
UPDATE
Setting format for all columns (in an awkward way). Assuming my users table defined as follows.
create table users(
id number
, username varchar2(20)
, credentials varchar2(90)
, lastname varchar2(20) not null
, firstname varchar2(20)
, emailaddress varchar2(42)
, emailisvalid number(1)
, sex char(1)
, created date default sysdate
);
We could issue this command putting the output into the file login.sql which is automatically executed every time you start SQL*Plus.
SQL> spool login.sql
SQL> select 'column ' || column_name || ' format a' || length(column_name) || ';'
from user_tab_cols where table_name = 'USERS';
column ID format a2;
column USERNAME format a8;
column PASSWORD format a8;
column LASTNAME format a8;
column FIRSTNAME format a9;
column EMAILADDRESS format a12;
column EMAILISVALID format a12;
column SEX format a3;
column CREATED format a7;
This problem:
Name Sala Ag
Steve 1000 30
John 2000 25
Can be solved like this:
SELECT Name,
CAST(Salary) AS VARCHAR(6) AS Salary,
CAST(Age) AS VARCHAR(3) AS Age
FROM Employee;
Note, once again... this is not what the query is returning, but how the client is displaying the results. Use a different client it would work differently. In this case the client is formatting the columns based on the column data type. When it sees a varchar (like name) it goes to the max data size. So we give it a bigger string data it will look ok.
This is NOT part of what is happening on the Server -- this goes to how the client displays. So if the query is going to be used by a different client (eg web page or application call) this won't matter when it is actually used by those clients.
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;