How to get coumn names without getting truncated in SQLPLUS - sql

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.

Related

How to customise/format the output of DESCRIBE command in SQL (Oracle)?

I have created a table BORROW with the following attributes and datatypes of the corresponding columns as shown below:
SQL> CREATE TABLE BORROW
2 (
3 LOANNO VARCHAR2(5),
4 CNAME VARCHAR2(20),
5 BNAME VARCHAR2(20),
6 AMOUNT NUMBER(8,2)
7 );
Table created.
SQL> DESCRIBE BORROW;
Name Null? Type
----------------------------------------- -------- ----------------------------
LOANNO VARCHAR2(5)
CNAME VARCHAR2(20)
BNAME VARCHAR2(20)
AMOUNT NUMBER(8,2)
When I am running the DESCRIBE command, I want it to show like it is shown in the picture instead:
(https://i.stack.imgur.com/V001t.jpg)
Does anyone know how to do this?
Thanks in advance :)
DESCRIBE is a SQLPlus command and is not configurable. But you can query the data dictionary yourself and present it any way you want. If you want a single size column you have to do some conditional logic since how Oracle represents "size" and depends on the datatype and what you mean by "size" (character length vs bytes needed to store it, number precision vs. precision + scale vs. bytes needed to store it, etc. etc..) Here's something to start with:
SELECT column_name field_name,
data_type,
CASE WHEN (data_type LIKE '%CHAR%') THEN TO_CHAR(char_length)
WHEN (data_type LIKE '%NUMBER%') THEN '('||NVL(TO_CHAR(data_precision),'*')||','||NVL(TO_CHAR(data_scale),'*')||')'
WHEN (data_type = 'RAW') THEN TO_CHAR(data_length)
ELSE NULL
END "size"
FROM all_tab_columns
WHERE table_name = 'BORROW'
ORDER BY column_id
The DESCRIBE command in Oracle SQL does not have an option to display the field names, data types, and sizes in the format you specified. However, you can use the DESCRIBE command in combination with other SQL commands to achieve the desired output. One way to do this is to query the USER_TAB_COLUMNS view, which contains information about all columns in all tables in the current schema.
You can use the following query to get the desired output:
SELECT column_name as "FIELDNAME", data_type as "DATATYPE", data_length as "SIZE"
FROM user_tab_columns
WHERE table_name = 'BORROW';

A simple question about Oracle database format column

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.

How to handle comma separated decimal values in Hive?

I have one CSV file and metadata for the same. Columns in this CSV is are delimited by pipe | symbol. Sample data is as follows:
name|address|age|salary|doj
xyz | abcdef|29 |567,34|12/02/2001
Here salary column is of type decimal but instead of using period . as decimal separator, comma , is used.
I created Hive external table as below and for this data Hive shows NULL for salary column.
create external table employee as(
name string,
address string,
age int,
salary decimal(7,3),
doj string
) ROW FORMAT DELIMITED FIELDS TERMINATED BY '|'
LOCATION 's3://bucket/folder_having_many_csv_files/';
If I change data type of salary column to String then as expected, Hive works fine.
I would like to know how to tell Hive that this particular column is of type DECIMAL and decimal separator is comma (,) and not a period (.) symbol.
You could easily build table with salary as a string and replace the comma in a view on top. This is probably the easiest thing to do since the data is big and likely someone else owns it.
create view table employee_decimal as
select name
, address
, age
, cast(regexp_replace(salary, ',', '.') as decimal(7,3)) as salary
, doj
from employee;

Add a Column that Represents a Concatenation of Two Other Varchar Columns

I have an employees table and I want to add a third column valued as the concatenation of the first and last name called "FullName". How can I accomplish that without losing any data from either of the first two columns?
Quick preface: this answer was based on the originally incorrect tag that this question was relating to SQL Server. I'm no longer aware of its validity on Oracle SQL Developer.
ALTER TABLE Employees ADD FullName AS (FirstName + ' ' + LastName)
Although in practice I'd advise that you do that operation in your SELECT. That's somewhat personal preference, but I tend to think doing things in your end queries is a bit cleaner, more readable, and easier to maintain than storing extra, calculated columns.
Edit:
This was eventually found as the answer, and listed by the OP as a comment on this post. The following is appropriate syntax for Oracle Sql Database.
ALTER TABLE emps MODIFY (FULL_NAME VARCHAR2(50) GENERATED ALWAYS AS (first_name || ' ' || last_name) VIRTUAL);
If you need fullname column all time when you select from database then you can create computed column at the time of creation of your table employee.
for example:
CREATE TABLE Employee
(
FirstName VARCHAR(20),
LastName VARCHAR(20),
FullName AS CONCAT(FirstName,' ',LastName)
)
INSERT INTO Employee VALUES ('Rocky','Jeo')
SELECT * FROM Employee
Output:
FirstName LastName FullName
Rocky Jeo Rocky Jeo
It depends on your purpose, whether you really need to add a new column to your database, or you just need to query out the "full name" on an as-needed basis.
To view it on the fly, just run the query
SELECT firstname + ' ' + lastname AS FullName FROM employees
Beyond that, you also can create a simple Stored Procedure to store it.
(For single result use equal to in the where condition)
select *
from TABLE_name
where (Column1+Column2) in (11361+280,11365+250)
In addition to #Jacky 's answer, if you are trying to add this to a query and not the table, there is also the CONCAT() function that you can use in the select statement
SELECT CONCAT(FirstName, ' ', LastName) as FullName
FROM table_name

New email address convention [duplicate]

This question already has answers here:
SQL UPDATE SET one column to be equal to a value in a related table referenced by a different column?
(12 answers)
Closed 9 years ago.
I have a database with email addresses in it.
My company is changing our email address convention from:
first_initiallast_name#mycompany.com
to
first_name.last_name#contoso.com
I'd like to write a SQL statement to update all the email addresses in one shot in this database. First and last name are columns in the same table (we'll call it MY_TABLE for simplicity's sake).
How could I do this in an Oracle SQL statement?
It seems like you'd just want
UPDATE my_table
SET email_address = first_name || '.' || last_name || '#contoso.com'
That will update every row in the table and assumes that you have no NULL first or last name values.
You juste want to update the email field with two others fields:
UPDATE my_table SET email= first_name || '.' || last_name || '#contoso.com'
WHERE first_name != NULL AND last_name != NULL
Be aware that the transformation might be incorrect if first_name or last_name is empty...
EDIT: In reality what you want is similar to this question: SQL UPDATE SET one column to be equal to a value in a related table referenced by a different column?