Delete all row in a table where all content are equals to NULL - sql-server-2008

Based in a table with follow structure:
column1 column2 column3 ... columnN
1 null null null null
How i can delete all the row in the table that contain all null values? (except the 1st column that is an ID column)
Thank you guys

Please try with below code snippet.
DELETE FROM TableName
WHERE COALESCE (column2,column3,column4) IS NULL;

Please use the below code:
Delete from tbl1 where ISNULL(column1,'')=''
and ISNULL(column2,'')='' and ISNULL(column3,'')=''

Related

How do I update the data in a mysql table called link in the type column for user_id NULL?

I understand almost nothing in sql, help me update the data so that in the link table for the type column, change the value to direct, provided that the user_id column is NULL
Image
I only know how to update the entire type column, but I don't know how to do with the condition, only for user_id is NULL
Update links set type='direct'
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
See docs: https://dev.mysql.com/doc/refman/8.0/en/update.html
Update links set type='direct' WHERE user_id is null

How to add column from other table after converting the value in sql?

i have one table having a column darm_review. i want to add this column to another table with convert value using sql query.
0 to No
1 to Yes
NULL to Undefined
eg:-
table1
darm_review
0
1
NULL
now i want to add this column in table2 like this
table2
darm_review
No
Yes
Undefined
Please help me how to do this.
Use case when expression
select darm_review,
case when darm_review='No' then 0
when darm_review=1 then 'Yes'
when darm_review='Undefined' then null end as conditionalValue
from table2
This is will insert all the records of table1 into table 2 and during insertion it will also replace data of darm_review as per requirement.
INSERT INTO TABLE 2
SELECT c1,c2,
CASE
WHEN darm_review = 0 THEN "No"
WHEN darm_review = 1 THEN "YES"
WHEN darm_review IS NULL THEN "Undefined"
END AS T
FROM TABLE1
Hope this will help you.

Merge two columns in MySQL and ignore the NULL

MySQL data table are as follows:
Column1 Column2
A NULL
B NULL
NULL C
NULL D
NULL NULL
Need output as follows:
Column3
A
B
C
D
NULL
How can i get this merging?
MySQL query would be preferable.
If no options available in the end then i can look for python code.
select COALESCE(Column1, Column2) as Column3 from tablename
This is working.
You need to join tables on common columns and use case in mysql.
You can do something as below..
CREATE TABLE new_tbl [AS] SELECT CASE
WHEN orig_tbl1.col1 IS NOT NULL THEN orig_tbl1.col1
WHEN orig_tbl1.col2 IS NOT NULL THEN orig_tbl1.col2
ELSE NULL
END , orig_tbl1.col2,orig_tbl1.col3,
FROM orig_tbl1,orig_tbl2 where orig_tbl1.id = orig_tbl2.id ;
You can try this link. https://stackoverflow.com/a/8600850/1069633

How to check that table has empty rows and then delete them

In my SQL tables there are rows where columnX has empty value (""). Now i want them i queried to select them and then delete them.
Query like:
tables has empty rows
Delete empty rows
How can i do this. Any idea
Depending on what exactly you mean by "empty" rows:
delete from yourTable where column1 is null
will delete where column1 has a null value. If you mean where multiple columns have nulls, it's just a matter of adding more conditions to the where clause:
delete from yourTable where column1 is null and column2 is null and column3 is null
If by empty you mean "has spaces in a text field or the field is empty" you can use some of the builtin functions to find them for example:
delete from yourTable where trim(column1)=''
which would find a row in the table where column1 only has white space in it and so on.
You might want to have a read of this article that I wrote on SQL, join and the like - it has got a fair bit in it about selecting the right rows from the table - and in your case, replace the select.... from where... with a delete from where...
Having said all that, I would really wonder why you are inserting data into your table that you don't want in it?
You can check each field for null or the empty string like this:
DELETE FROM table WHERE (column1 IS NULL OR column1 = '') AND (column2 IS NULL OR column2 = '')
Just add the rest of your columns to the WHERE clause.
Simple : delete from Test_table where c1 is null,....and cN is null
Ok Try this, i hope u'll find your solution
What you need is, first get empty rows
Select * From table_name Where column_name = "";
Then Delete the empty rows
Delete From table_name Where column_name = "";
or don't write the select query only write the delete query
I hope this solve your problem

inserting default value along with other columns

Hi help in this case i need to insert two columns in a table one column has to be default value as 'Other'(Please don't suggest to put Other as default value) and other column should get inserted from different table.
This is my sample code please suggest necessary change:
INSERT INTO table1 (`ID`,`specialty`)
SELECT `ID`,`here i need to put default value as other` from table2 a WHERE
Id IS NOT NULL
Do you mean this?
INSERT INTO table1 (ID,specialty)
SELECT ID, 'Other'
FROM table2
WHERE ID IS NOT NULL