I'm using Oracle 10g. I want to append # to all values in 'sal' column. To acomplish this first I'm trying to change data type of 'sal' column from numeric to varchar but getting following error
What am I doing wrong ?
You should use modify keyword instead of your second alter .
alter table
emp
modify
(
sal varchar2(10)
);
When modifying a tables column datatype , you need to use modify keyword.
Of course, you must deal with existing column data. When modifying a tables column datatype you may want to export the rows, redefine the table and then re-import you data.
In this you would need to follow these steps to alter a column data type:
Create the new column at the end of the table.
Run an update to populate the new table column
Drop the old table column
Re-name the new column to the original column name
It does not seem a smart idea to take a perfectly good number and ruin in for the rest of the user by appending a piece of string.
Just add the string on the select directly or through a view?
Something like:
SQL> create view emp_horked
as
select ename, sal, sal || '#' hash, to_char(SAL,'9999.99') || '#' sal_padded
from emp;
View created.
SQL> select * from emp_horked where rownum < 5;
ENAME SAL HASH SAL_PADDE
---------- ---------- ----------------------------------------- ---------
SMITH 800 800# 800.00#
ALLEN 1600 1600# 1600.00#
WARD 1250 1250# 1250.00#
JONES 2975 2975# 2975.00#
More on format models for to_char
Related
I'm facing some problems while inserting and query data from the oracle SQL table. my table name is Table_Name
and it has two column names (id number, password varchar2). I make the inserting operation finely but when I query data then it as "1.14662E+10" this format.
so whats the problem is?
In SQL*Plus, You can set the format of the column to display the content of the column. For your case, You need something like this:
column your_column_name format 9999999999
The number of 9s in the above command should be the number of digits in your column's max value.
See below example(ROWNM column):
SQL> select * from T;
ROWNM NAME TOTAL COLUMN1
---------- ---------- ---------- ----------
1.2346E+17 Tejash ########## test
SQL> column rownm format 999999999999999999
SQL>
SQL> select * from T;
ROWNM NAME TOTAL COLUMN1
------------------- ---------- ---------- ----------
123456789123456789 Tejash ########## test
SQL>
The data is correctly stored and it's only a question of the way the client program (here SQL*Plus) is displaying the data.
As already answered you can use SQL*Plus formatting commands or use TO_CHAR function:
SQL> select x from t;
X
----------
1
1234567890
1.2346E+19
SQL> select to_char(x) from t;
TO_CHAR(X)
------------------------------------------------------------------------------------------------------------------------
1
1234567890
12345678901234567890
SQL>
One more trick for SQL*Plus (and SQLcl and SQL Developer) if you have several large number columns and don't want to set them all with column. There is a default display width for numbers, and beyond that size the formatting changes to scientific notation; but you can change that:
SQL> show numwidth
numwidth 10
SQL> select 12345, 123456789123456789 from dual;
12345 123456789123456789
---------- ------------------
12345 1.2346E+17
SQL> set numwidth 20
SQL> select 12345, 123456789123456789 from dual;
12345 123456789123456789
-------------------- --------------------
12345 123456789123456789
SQL>
It affects all number columns, even for smaller numbers, as you can see - so it's a broader approach and less targeted than individual column settings, but that can be useful sometimes.
You could also use set numformat to do more involved formatting, like adding group separators. Read more about set options in the documentation.
make a java program that queries the inbuilt table “emp” and display the first two columns (empno using column index and ename using column name ) of all the rows.
But problem here is :
ERROR:
ORA-04043: object emp does not exist
I am using oracle 19c on windows platform and using pluggable database orclpdb user "hr".
please tell me where to find this emp table.
And second question :
Even if I manually create a new Emp table in hr user ,what will be the sql command for the given
/*
* Using
* queries the inbuilt table “emp” and
* displays the first two columns
* (empno using column index and ename using column name )
* of all the rows.
*/
just tell me the sql command only.
I have two columns in lets say employees table.
1) emp_id which is a uuid autogenerated.
2) emp_name which is a string.
Now i want to put some default values in emp_name column at the time of insert and i want lets say first 6 characters of the corresponding emp_id added after 'emp_' string.
And i want all of it as a single insert query statement.
Is there a way to do such a thing in postgres.
Try this:
WITH emp as (SELECT public.gen_random_uuid() AS uuid)
insert into employees(emp_id, emp_name) (select
emp.uuid,'emp_'||substring(emp.uuid::varchar from 0 for 6) from emp);
You can generate the uuid, save it in a variable and use it in the insert statement.
To understand how you can create it and the different type of uuid, you can read this page:
http://www.postgresqltutorial.com/postgresql-uuid/
i want to create a table similar to other table and also want to declare few more new columns at same time.
I tried like
create table emp2 as ((select * from emp)),age varchar2(3))
But its not working.
If you want to add additional columns in your CTAS (Create Table As Select), you'll have to provide data for them. If you want to leave the column empty, you can use CAST(null as <your datatype>):
create table dual_copy as
select
d.*,
cast(null as varchar2(3)) as age
from dual d;
A word of advice, though: Explicitly storing the age of people is never a good idea (people tend to get older over time :-) ). Just store the birth date and compute the age on-the-fly. Alternatively (if you're running 11g or later), you could add a virtual column for the age.
And if you insist on explicitly storing the age, you should at least use a numeric datatype instead of a varchar2.
You could add the age column in another statement:
CREATE TABLE emp2 AS SELECT * FROM emp;
ALTER TABLE emp2 ADD age VARCHAR2(32);
I create a view
SQL> create view DEPT20 AS
2 select empno AS Employee_ID_ID,ename AS Employee, deptno as Department_ID from emp
3 where deptno = 20
4 with check option constraint emp_dept_20;
View created.
I expect to get this error
ORA-01402: view WITH CHECK OPTION where-clause violation
But I dont get an error I get
SQL> update dept20
2 set department_ID=30
3 where Employee='Smith';
0 rows updated.
SQL>
Normally I like no errors but in this one I want the error when someone tries to go outside the allowed.
What happens if you run this SQL?
update dept20
set department_ID=30
where Employee='SMITH';
If you're using the canonical EMP table, all the employees' names are in uppercase.