Delete specific records on MSSQL [duplicate] - sql

This question already has answers here:
Why doesn't "WHERE column = NULL" throw an error in SQL Server? [duplicate]
(5 answers)
Closed 5 years ago.
I'm trying to delete specific records from a table using where clause but unable to do so. The command executes successfully but says zeros rows are modified.enter image description here
I'm still in the starting stage of learning SQL. Thanks for helping out

= NULL never returns true -- almost all comparisons with NULL return NULL, which is treated as false. The correct logic is IS NULL:
delete from Patient
where PatientCountry is null

If you want find or delete null values then dont use "=" but "is".
It should be:
delete from patient where patientcountry is null;

Related

Keep null rows after filtering [duplicate]

This question already has answers here:
SQL not displaying null values on a not equals query?
(5 answers)
Closed 20 hours ago.
Before select
Status could be:
X1, X2, NULL
SELECT * FROM surrtest t Where t.Status != „X2“
It’s deleting also rows where Status was null. But it shouldn’t.
So after query I’m only getting rows where status Is X1 but I also want the NULL rows.
Is it possible?
I don’t know if it’s possible. If not maybe there is a workaround where we replace the null values with empty strings? But I hope it is possible without changing the data.
This should work:
SELECT * FROM surrtest Where Status != ‘X2’ or Status is null
If that doesn’t work then there’s something weird going on with your database.
In most databases, a where clause evaluates to either true, false, or unknown. Any comparison with null will result in an unknown result. Since a where clause filters out all records that don't evaluate to true, you will not see records that evaluate to unknown, as well as false.
In your original query, the where clause was filtering out null records, because they evaluated to unknown. So, you have to add the extra part or Status is null to the where clause to include the null records.
Also, do NOT use or Status = null, since that will always evaluate to unknown, like mentioned before. You must use or Status is null.

PostgreSQL NULL value cannot be found [duplicate]

This question already has answers here:
postgresql NOT ILIKE clause does not include null string values
(2 answers)
Closed 7 months ago.
select * from test;
select * from test where name not in ('amtf');
Why?
As others have said, the problem here is, that you're comparing against a null value, so it returns nothing, because it considers it as false, and I'll go even further that even if you say where name <> 'admf' it wont work, and even if you add more rows it will ignore the null row, and it's not just in PostgreSQL, it doesn't work in SQL-Server or MySQL either.
As you can see in these db<>fiddles SQL-Server, MySQL, and PostgreSQL.
And the reason why it doesn't work is, because you're saying name should not equal a specific value. First name needs to be a value it should not be equal to a value, but when name is null it doesn't have a value, and even more for a side note null itself is not equal null.
The way to solve it is to convert it to a empty string by using COALESCE(name,'') or in SQL-Server you can also use isnull(name,''), and then compare it, or you can add or name is null which will return you all rows, including null, where name <> 'some value'.
Well the condition is right and the response for that as soo .
select * from test where name not in ('amtf');
your query is saying : give me all the records that the name Column is not in ('amtf').
you have 2 column's on is amtf and the other is null.
amtf will no be brought because of the condition and the other column is null -> no name set

SQL Table Linked to Access Displays 0 instead of NULL [duplicate]

This question already has answers here:
IS NULL versus <> 1 SQL bit
(5 answers)
Closed 3 years ago.
If I link an SQL table with a bit field that has NULL values and open up the linked table in Access, the NULL values are showing up as 0. I do want the NULL values to show NULL or blank in Access when opening up the table. I am not sure if this has some recent problem with a Microsoft update but it makes a difference because sometimes we are working on the table directly in Access and want to see if the field value is NULL without doing a search. Thanks.
I do want the NULL values to show NULL or blank in Access
Maybe. However, this is a known limitation, and it won't happen - except if you change the data type to Integer.

Run a sql query that deletes a column before a date [duplicate]

This question already has answers here:
Delete from a table based on date [closed]
(5 answers)
Closed 6 years ago.
I am running SQL Server 2008 R2. I want to run a query that will delete data in column referralSource before a specific referralDate in the dbo.Referral table.
Here is a screenshot of what I am talking about:
update dbo.Referral
set referralSource = null
where referralDate < some_specific_date
For some_specific_date you can put e.g.
'20101201' or any other value you need.

UPDATE within a table from table name saved in the column [duplicate]

This question already has answers here:
How do I UPDATE from a SELECT in SQL Server?
(38 answers)
Closed 8 years ago.
I have a little problem, but I'm sure it s not really complicated.
It's just hard to find the key word to describe the problem and find a solution
I want to update a column in a table using parameters from this table for a query on an other table.
Example : I have Header + 2 lines
IDSOURCE, IDCIBLE, IDENTIFIANT, TABLE_CIBLE, NOM_ATTRIBUT, NOM_CHAMP_IDENTTIFIANT, NOM_CIBLE
--------------------------------------------------------------------------------------------
DMT_1000, DMT_1000, 1000, [dictionnaire].[dbo].[TABLE_CHAMPS_DATAMART], NOM_CHAMP_DMT, IDENTIFIANT_CHAMP_DATAMART, NULL
DMT_1001, DMT_1001, 1001, [dictionnaire].[dbo].[TABLE_CHAMPS_DATAMART], NOM_CHAMP_DMT, IDENTIFIANT_CHAMP_DATAMART, NULL
And I want to update the last column of each line with something like :
UPDATE
Table
SET
Table.NOM_CIBLE = SELECT table.NOM_ATTRIBUT FROM table.TABLE_CIBLE WHERE table.NOM_CHAMP_IDENTTIFIANT = table.IDCIBLE
FROM
Table
Don't know if it s clear.
Thanks for your help.
This sounds like situation where you could use a cursor. Check out this StackOverflow question How to update a column fetched by a cursor