Update select statement result in a column conditioned by another column (Oracle SQL) - oracle

I'm trying to write a Select query in Oracle SQL that will return first_name column as empty if last_name column is empty(Regarless what's in the first_name column value).
For the first row "John" should be return as empty since he has no last_name
I don't want to update the table, I just want a Select query that return empty first_name if last_name is empty. Thanks in advance!!!
THIS SHOULD BE THE OUTCOME.

You can use CASE expression:
SELECT customer_id,
CASE
WHEN last_name IS NULL
THEN NULL
ELSE first_name
END AS first_name.
last_name.
age,
country
FROM table_name

Related

Oracle 12 equal and not equal (=, !=, <>) behavior about nvarchar2 working problem [duplicate]

This question already has answers here:
Why does Oracle 9i treat an empty string as NULL?
(10 answers)
Closed 2 years ago.
In oracle an empty varchar2 and null are treated the same because Oracle internally changes empty string to NULL values.
Please consider following example :
--1- create table
CREATE TABLE persons(
person_id NUMBER GENERATED BY DEFAULT AS IDENTITY,
first_name VARCHAR2(50) ,
last_name VARCHAR2(50) ,
PRIMARY KEY(person_id)
);
--2-insert sample data
insert into persons (first_name,last_name) values('n1','l1');
insert into persons (first_name,last_name) values('n2',null);
insert into persons (first_name,last_name) values('n3','');
insert into persons (first_name) values('n4');
the following query has result :
select * from persons where last_name is null;
PERSON_ID FIRST_NAME LAST_NAME
2 n2
3 n3
4 n4
and
select * from persons where last_name is not null;
PERSON_ID FIRST_NAME LAST_NAME
1 n1 l1
My question :
1-Why this queries result are “no rows selected”
select * from persons where last_name =''; -- no rows selected
select * from persons where last_name !=''; -- no rows selected
2-More important question is wonderful behavior for the following query:
select * from persons where last_name !='' or last_name =''; -- no rows selected
1-Why this queries result are “no rows selected”
Because the answer lies in your question itself.
select * from persons where last_name =''; -- here '' is converted to NULL
-- so last_name = NULL
-- it will always fail as NULL can not match with other NULL also
select * from persons where last_name !=''; -- - here '' is converted to NULL
-- so last_name != NULL
-- it will always fail as NULL can not mismatch with other NULL also
2-More important question is wonderful behavior for the following query:
select * from persons where last_name !='' or last_name ='';
-- equal or not equal with NULL will always return false.
-- so the result is no rows for this query also
Please note that in any expression if one side is NULL, it will always return false. (IS NULL or IS NOT NULL need to be used for checking null values)

Update data from one table to another using rownum

I have one table temp_name and it has seq_no , first_name,last_name and now I have another table called BEN_NAME in that also there is field first_name, last_name. I have to update the BEN_NAME table from the first_table by matching seq_num = rownum(of ben_name) table

How to check if a person exsist and then insert into another table using pl/sql?

Create an anonymous PL/SQL block, which looks for an employee by last name based on a SQL parameter (e.g. &variable) that the user responds to with a valid last_name in the EMPLOYEES table (i.e. King, or Kochhar, or De Haan, or Hunold, or Ernst, etc….…). If the employee last_name exists in the EMPLOYEES table, then insert into the OUTPUT_LOG table, the following string:
'Employee is is Found'. Test your PL/SQL block by checking the content of the OUTPUT_LOG table. You should find the string the you inserted in the OUTPUT_LOG table.
employee table has these columns
EMPLOYEE_ID ,
FIRST_NAME ,
LAST_NAME ,
EMAIL ,
PHONE_NUMBER ,
HIRE_DATE ,
JOB_ID ,
SALARY ,
COMMISSION_PCT ,
MANAGER_ID ,
DEPARTMENT_ID
output_log table only has one column VARCHAR2(250) called data and id column as PRIMARY KEY
I am struggling to figure check if the name exist in the table. I thought about using
begin
SELECT e.LAST_NAME as LAST_NAME, o.data
INTO OUTPUT_LOG
FROM EMPLOYEES as e, OUTPUT_LOG as O
WHERE e.employee_id = o.id
end;
but i dont think it would be wise and make sense to do so
Since this appears to be a homework question, I'm not going to solve it all for you but (to help you on your way) you could just use SELECT ... INTO and if a row is not found it will raise a NO_DATA_FOUND exception which you can catch (and use this to bypass the INSERT). Something like this:
DECLARE
p_found NUMBER(1,0);
BEGIN
SELECT 1
INTO p_found
FROM Employees
WHERE ROWNUM = 1
AND last_name = 'King'; -- replace with your where condition
-- then do the insert here (since it will only reach this point if
-- a row was found).
EXCEPTION
WHEN NO_DATA_FOUND THEN
NULL;
END;
/

Oracle Comma Separated Value (ID) in a Column. How to get Description for each Value in a Comma Separated string.

Sorry for the Confusing title.I myself did not understand it when i read it second time.
So here is the details description.
I have a table say "Awards" which have following Column:
Name,
Amount,
Employee
and Another table "Employee" which have following column:
Emp_Id,
Emp_Name
and in employee column of "Awards" table i have value "01,20" which are actually the Employee ID referenced to "Employee" table.
So is there any way i can get Employee Name in select "Awards" query?
Here is one method:
select a.*, e.EmpName
from Awards a join
Employees e
on ','||a.employee||',' like '%,'||e.emp_id||',%';
This will return the employee names on separate lines. If you want them in a list, then you would need to concatenate them together (and the best function for doing that depends on your version of Oracle).
By the way, this is a very bad data structure, You should have an association table AwardEmployee that has one row for each row and each employee.
Given below is the query to get comma seperated employee ids in form of rows which I put in subquery to get their name. Please edit as per your ewquirements.
Select Ename from employee where employee_id in (
SELECT trim(x.column_value.extract('e/text()')) COLUMNS
from awards t, table (xmlsequence(xmltype('<e><e>' || replace(Employee,':','</e><e>')||
'</e></e>').extract('e/e'))) x )
I have changed the Database (added one more table). and already started changing the CODE, as for the said report i have used following
WITH t AS
(
Select emp_name from employee where emp_id in (
select regexp_substr(Employee ,'[^,]+', 1, level) from awards
connect by regexp_substr((select Employee from awards ), '[^,]+', 1, level) is
not null)
)
SELECT LTRIM(SYS_CONNECT_BY_PATH(emp_name, ','),',') emp_name
FROM ( SELECT emp_name,
ROW_NUMBER() OVER (ORDER BY emp_name) FILA
FROM t )
WHERE CONNECT_BY_ISLEAF = 1
START WITH FILA = 1
CONNECT BY PRIOR FILA = FILA - 1
Which is temporary and i understand very less of it.
Thanks for you help and suggestion.

How to update single table in oracle?

I am new to oracle. I have created one table in oracle i want to update that tables with all columns without any content..I want all columns in that table.How to do this?Can anyone explain me?
Regards,
Raman
DESC tablename ?
I don't understand what you want... (update the table without any content ?)
describe your_table_name;
should print the table scheme like this :
Name Null? Type
------------------------------------------- -------- -------------
EMPLOYEE_ID NOT NULL NUMBER(6)
SALARY NUMBER(8,2)
HIRE_DATE NOT NULL DATE
TERMINATION_DATE DATE
TERMINATION_DESC VARCHAR2(4000)
you can see some example there.
I think you mean that you want to insert entire rows from one table (call it T1) into another table (call it T2) where there is a NULL anywhere in the row. Is that right?
INSERT INTO t2
SELECT *
FROM t1
WHERE t1.col1 IS NULL
OR t1.col2 IS NULL
OR t1.col3 IS NULL
... etc.... unfortunately, you need to name all the columns in t1 by hand for this to work. There's no shortcut way to say "WHERE anything IN t2 IS NULL". The closest you could do would be to use a query to help write your query:
SELECT 'OR t1.' || column_name || ' IS NULL' || chr(10)
FROM all_tab_columns
WHERE owner = 'DATA_TABLE_OWNER'
AND table_name = 'T1';

Resources