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
Related
My update query checks whether the column “Houses” is null in any of the rows in my source table by joining the id between the target & source table (check query one). The column Houses being null in this case indicates that the row has expired; thus, I need to expire the row id in my target table and set the expired date. The query works fine, but I was wondering if it can be improved; I'm new to SQL, so I don't know if using two joins is the best way to accomplish the result I want. My update query will later be used against millions of rows. No columns has been indexed yet.
Query:
(Query one)
Update t
set valid_date = GETDATE()
From Target T
JOIN SOURCE S ON S.ID = T.ID
LEFT JOIN SOURCE S2 ON S2.Houses = t.Houses
WHERE S2.Houses is null
Target:
ID
namn
middlename
Houses
date
1
demo
hello
2
null
2
demo2
test
4
null
3
demo3
test1
5
null
Source:
ID
namn
middlename
Houses
1
demo
hello
null
3
demo
world
null
Expected output after running update query :
ID
namn
middlename
Houses
date
1
demo
hello
2
2022-12-06
2
demo2
test
4
null
3
demo3
test1
5
2022-12-06
I would recommend exists:
update t
set valid_date = getdate()
from target t
where exists (select 1 from source s where s.id = t.id and s.houses is null)
Note that your original query does not exactly do what you want. It cannot distinguish source rows that do not exist from source rows that exist and whose houses column is null. In your example, it would update row 2, which is not what you seem to want. You would need an INNER JOIN instead of the LEFT JOIN.
With EXISTS, you want an index on source(id, houses) so the subquery can execute efficiently against target rows. This index is probably worthwhile for the the JOIN as well.
I don't see why you'd need to join on the column houses at all.
Find all rows in source that have value NULL in the column houses.
Then update all rows in target that have the IDs of the source rows.
I prefer to write these kind of complex updates using CTEs. It looks more readable to me.
WITH
CTE
AS
(
SELECT
Target.ID
,Target.Date
FROM
Source
INNER JOIN Target ON Target.ID = Source.ID
WHERE Source.Houses IS NULL
)
UPDATE CTE
SET Date = GETDATE();
To efficiently find rows in source that have value NULL in the column houses you should create an index, something like this:
CREATE INDEX IX_Houses ON Source
(
Houses
);
I assume that ID is a primary key with a clustered unique index, so ID would be included in the IX_Houses index implicitly.
I am trying to update a column in a table with values already in that same column. The table has many records, but there are values they may share with one or two other records in an ID Column. So say there are two records with the same ID value in the ID column. Then I want to update another column called Details. The two Records with the same ID do not have the same Details value. One has an integer in that column, The other has a Null. My question is, how can I replace the Null with the integer for each set of records containing the same ID. Below I will list a sample.
ID
Details
1234
55.60
1567
85.40
1234
Null
4569
58.09
1567
Null
8965
77.90
I would like a query that would populate those nulls based on ID.
ID
Details
1234
55.60
1567
85.40
1234
55.60
4569
58.09
1567
85.40
8965
77.90
The table I am working with is much larger in reality and has many more columns. These are the two I think are relevant to what I am trying to do. I have tried self joining it seems to error out. There is another column that differentiates IDs that are the same by being populated with 'Open' or 'Closed' that I did not include in the sample if that helps at all. This is on SQL Server 2019. Thanks in advance.
considering that there is only two same id and one has value in details:
update t1
set
t1.Details = t2.Details
from
tablename t1
join tablename t2
on t1.details is null
and t1.id = t2.id
and t2.details is not null
You can use an updatable CTE and window functions in SQL Server:
with toupdate as (
select t.*, max(details) over (partition by id) as max_details
from t
)
update toupdate
set details = max_details
where details is null and max_details is not null;
I have two tables.Table A has 4 columns. And table B has two columns.I want to insert value of one column of tabel A from one column of table B based on condtion if id matches.
how i can do this ? For example if [Movieid] in 1st table =[IMDBid] in second table then insert [count] of table 1=[CB] in table 2.
i want to do it once for full table.
[column] -> these are colums
i m using sql server.
Tabel 1 : Movieid,count,
Tabel 2: IMDBid, CB
Results which i want: i want to insert values of CB column in count where Movieid=IMDBid
Tabel 1 :
(Nick Id,MovieId,Rating,MovId)-> (1,4972,6.25,?)(1,24216,7.25,?)
Tabel 2 :
(Imdbid,Title,ImdbPyId,Id)-> (4972,hello,32450,1)(24216,hi,62450,2)
Insert /fill value of MovId(tabel1) using values Id(tabel2)where MovieId(tabel1)==Imdbid(tabel2)
You can do it like,
UPDATE tabl1
SET count = (SELECT CB FROM tabl2 WHERE IMDBid = Movieid)
But it is gonna blow up if you have multiple values returning from the subquery.
So make sure to use the appropriate function to get the single value from that subquery whichever meets your requirements.
If your tables have 1:1 relationship then it should be fine. But if it is 1:n then you need to use either aggragate functions or the TOP 1 clause in that subqyery.
solution is UPDATE tabl1
SET count = (SELECT CB FROM tabl2 WHERE IMDBid = Movieid)
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
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