I just started learning SQL; I've created a table. Learned insert command and inserted values in 2 rows. However I've inserted null values in 3rd.
Now I want to delete the third row which has 2 columns with no values in it.
I'm using the following query:
delete employee where city=null;
It doesn't seem to be working!
According SQL 92 standard many logical operations with null values like
> null
= null
and null
or null
not null
should always return null (and never true). Some DBMS (e.g. Oracle) follow this rule rigorously, some (MS SQL) can have a mode that null = null returns true, not required null. In order to be compartible with SQL 92 and so with (almost) all DBMSs, you should use is null or is not null standard comparisons, in your case
delete from employee
where city is null -- <- Standard comparison
You need the is null "operator":
delete from employee where city is null;
This is because in SQL, nothing is equal to NULL.
You can't use = with NULL. Instead, use:
delete employee where city is null;
To achieve this
you will have to write this query
DELETE from table_name WHERE city IS NULL;
this query will delete the rows/records WHERE city is null
Here you know you have added Null in city column so checking with is null will work but it's also possible to add an empty string in city column. So if your business condition is like delete all records from employee table where city is either null or empty i.e. with no values you should write as:
delete from employee where isnull(city,'')='';
change it :
delete from table_name where city is null
or
delete from table_name where city = null
Related
How can I replace the unequal columns in a table with a Null value when the table was created with no null values?
Example
employeeid city home
----------------------------------------
1 munich augsburg
|
\--in this case employeeid and city should show a 'null' value
Can I use outer join when I combine 2 tables? Or Inner Join?
I cannot use insert as the table was created without using insert.
I'm not clear with your question but you have to set column attribute nullable first then you can set it through query.
update tablename set employeeid=null,city=null where column=checkvalue
Good morning
I would like to filter out rows based on the values of two columns matching. Both columns are in the same table
Not sure if this can be done in a WHERE clause?
ID = 12345 and Country is null anything when those two columns match, removes the row
Thank you
You could use this:
DELETE
FROM
table_name
WHERE
(ID = 12345 and Country is null);
```
# Or in a more "safe" way, use soft delete (adding a new column):
alter table [your table name] add is_deleted varchar2(1) default 'N';
update [your table name]
set is_deleted = 'Y'
where (ID = 12345 and Country is null);
I figured it out
where (ID != 12345) or (Country is not NULL) this keeps everything with that id and has a Country, but removes rows where Country is null value
I am working on report builder 6i.
I have two tables
1- Company_info
2- Address_info
Comp_addr_code is a foreign key in company_info table which is primary key in
Address_info table.Now I am retrieving data from address_table base on comp_address_Code.But in some cases first three columns are empty in Address_info table.
I want for these columns 'X' should be displayed.What Changes should I make in my SQL Statement.My Sql statement is:
SELECT COMP_CODE, COMP_NAME, COMP_ADDR_CODE,
FROM COMPANY_INFO, ADDRESS_INFO
WHERE COMP_CODE=:P_COMP_CODE
AND COMP_ADDR_CODE=ADDR_CODE
Use COALESCE, it picks the first non-null value. I.e. if the column value is not null, return the column value, else return specified default value:
select coalesce(COMP_CODE,'X'),
coalesce(COMP_NAME,'X'),
etc
As given, only COMP_NAME can be NULL; if COMP_CODE or COMP_ADDR_CODE are null, those rows will not be in the resultset.
NVL or COALESCE are usually used to replace null values with other values, e.g.
SELECT
COMP_CODE,
nvl(COMP_NAME, 'X')
COMP_ADDR_CODE
FROM COMPANY_INFO,ADDRESS_INFO
WHERE COMP_CODE=:P_COMP_CODE
AND
COMP_ADDR_CODE=ADDR_CODE
I can already hear the groans looking at my title but please bear with me a moment. :)
I have two tables that have a few columns in common and are updated through different means. Given a specific identifier I want to update the first table with values from the second table if the first table is missing some information.
Table A looks something like:
Dept_ID Reviewer Reviewer_Team Reviewer_Code
ACM Null Null Null
EOT Null Null Null
QQQ Joe Joe's Group XYZ
ACM Null Null Null
ZZZ Null Null Null
Table B looks something like:
Dept_ID Reviewer Reviewer_Team Reviewer_Code
AAA Al Al's Group 123
BBB Bob Bob's Group 234
ZZZ Zoe Zoe's Group 567
If Reviewer_Code is Null in Table A we want to find Table A's Dept_ID in Table B, and update Table A's other fields to match Table B. Note that Table A might have multiple records with the same Dept_ID in which case we'd expect them to have the same values updated from Table B.
Sounds easy. Using the above tables as an example there are no matches in Table B, so the ACM and EOT records would not be updated at this step. Table A's ZZZ record though would get updated based on Table B's ZZZ record.
However there's a chance that there would be no matches in Table B. So pretend Table A doesn't have the ZZZ record, just the ACM and EOT that have Nulls.
I'm new to Oracle (coming from SQL Server) so maybe I'm testing this wrong, but what I have is a bunch of queries one after another in a .sql window of Oracle SQL Developer. This seems to work for me just fine normally. When it gets to this query though I get the dreaded "single-row subquery" error.
Here's the query I've tried a few different ways:
UPDATE VchrImpDetailCombined vchr
SET (Reviewer, Reviewer_Team, Reviewer_Code) =
(SELECT DISTINCT b.Reviewer, b.Reviewer_Team, b.Reviewer_Code
FROM GlobPMSDeptIdMapping b
WHERE b.Dept_Id = vchr.Dept_Id)
WHERE vchr.Reviewer_Code IS NULL
AND vchr.Business_L1 = 'CF'
AND vchr.Dept_ID IS NOT NULL;
or
UPDATE VchrImpDetailCombined vchr
SET (Reviewer, Reviewer_Team, Reviewer_Code) =
(SELECT DISTINCT b.Reviewer, b.Reviewer_Team, b.Reviewer_Code
FROM GlobPMSDeptIdMapping b
inner join VchrImpDetailCombined a
on b.Dept_Id = a.Dept_Id
WHERE b.Dept_Id = vchr.Dept_Id)
WHERE vchr.Reviewer_Code IS NULL
AND vchr.Business_L1 = 'CF'
AND vchr.Dept_ID IS NOT NULL;
I've tried a few other things as well such as doing "WHERE EXISTS SELECT blahblah", or "WHERE b.Dept_ID IS NOT NULL", etc.
Now, given my example data above, the subquery should have 0 records, keeping in mind there actually isn't a ZZZ record in Table A like my example, just the ACM and EOT. Table B simply doesn't have records with the matching Dept_ID in Table A. So my expectation would be for a 0 record update and happily moving along to the next query.
When I run these queries in a string of other queries I get the error. If I run the query all by its lonesome I simply get a "3 rows updated" which seems odd that anything is updating considering there should be no matches. But the 3 rows updated would seem to match the 3 ACM and EOT records even though Table B has nothing to update from given the criteria.
I must be missing something obvious, but I just can't seem to grasp it. There's a bajillion of these ORA-01427 questions so I was so sure I could find the answer already out there, but couldn't seem to find it.
Any ideas?
You need to instruct Oracle that it should perform the update only when there are data with which to do so (and I would have expected such to be needed for SQL Server as well, but I'm uncertain). This will overcome that obstacle, at the expense of performing an additional subquery:
UPDATE VchrImpDetailCombined vchr
SET (Reviewer, Reviewer_Team, Reviewer_Code) = (
SELECT b.Reviewer, b.Reviewer_Team, b.Reviewer_Code
FROM GlobPMSDeptIdMapping b
WHERE b.Dept_Id = vchr.Dept_Id
)
WHERE vchr.Reviewer_Code IS NULL
AND vchr.Business_L1 = 'CF'
AND vchr.Dept_ID IN (
SELECT Dept_Id
FROM GlobPMSDeptIdMapping
);
Per my comment on the question, I removed the DISTINCT from the (original) subquery, as it's either unneeded or ineffective.
I would like to only include a comparison on an inner join if the value is not null. E.g. I am comparing addresses some addresses have the same house number street address and tag but have a distinct apartment number, but other addresses don't have apartment numbers associated with them so the value is null. These values don't get joined.
A left outer join won't work because I need to join data associated with the values that have null apartment values. Any ideas?
select * from address a
inner join ma_address ma
on a.number=ma.number
and a.street=ma.street
and a.tag=ma.tag
and a.apt=ma.apt
Thanks,
This is because in SQL, NULL does not equal NULL. Try something like this for each join criteria
(a.<col> = ma.<col> OR (a.<col> IS NULL AND ma.<col> IS NULL))
A value of NULL indicates that the value is unknown. A value of NULL is different from an empty or zero value. No two null values are equal. Comparisons between two null values, or between a NULL and any other value, return unknown because the value of each NULL is unknown.
Null values generally indicate data that is unknown, not applicable, or that the data will be added later. For example, a customer's middle initial may not be known at the time the customer places an order.
Following is information about nulls:
1. To test for null values in a query, use IS NULL or IS NOT NULL in the WHERE clause.
2. When query results are viewed in SQL Server Management Studio Code editor, null values are shown as NULL in the result set.
3. Null values can be inserted into a column by explicitly stating NULL in an INSERT or UPDATE statement, by leaving a column out of an INSERT statement, or when adding a new column to an existing table by using the ALTER TABLE statement.
4. Null values cannot be used for information that is required to distinguish one row in a table from another row in a table, such as primary keys.
Source: http://technet.microsoft.com/en-us/library/ms191504(v=sql.105).aspx