insert data where all rows equals to null - sql

Is there a way to insert data where all rows are equals to null? I know it looks like
select login from Users
where login is null
When it return me
1. null
2. null
3. null
and so on...
How can I fill data to all this null rows?

To just update the null values in one column then do this;
UPDATE TableName
SET FieldName = ISNULL(FieldName,'NewValue')
If you want to update all columns when they ALL are NULL then you can do something like this
UPDATE TableName
SET
Field1 = 'Value1'
,Field2 = 'Value2'
,Field3 = 'Value3'
WHERE
Field1 IS NULL
AND Field2 IS NULL
AND Field3 IS NULL
If you want to replace all NULL values in the table regardless of whether the whole row is NULL then you can do this;
UPDATE TableName
SET
Field1 = ISNULL(Field1,'Value1')
,Field2 = ISNULL(Field2,'Value2')
,Field3 = ISNULL(Field3,'Value3')
If you want to update the values to different values for each row you will need a way of linking a user to their login. A basic version would be something like this (I'm referring to this as LookupTable)
ID Login
1 User1
2 User2
3 User3
Your query will be something like this;
UPDATE a
SET a.login = ISNULL(a.login,b.login)
FROM TableName a
JOIN LookupTable b
ON a.id = b.id
This will only update values with a NULL but you'd probably want to just set a.login = b.login to ensure that all of your data is correct.

UPDATE Users
SET login = 'value'
WHERE login is NULL
This would update the login column with the value 'value', if the current value is NULL.

Assuming that all columns in a table can take NULL values and do not have defaults, you can insert a row into a table with all NULL values by doing:
insert into t(col)
select NULL;
insert should have at least a single column; any column will do.

Related

Moving a cell value to another row based on ID numbers

I am looking to move B to the above row. It can either be placed where the Null value is in Column B or another column can be created. The value of B is linked to value A through an ID. The ID for value B is always X + 2 (the values in the ID column are integers).
I can’t just move the value up because the table I am working with has thousands of rows. It must be linked to the ID’s.
Please let me know if you have any questions. Any assistance is much appreciated. Thank you.
ID
Column A
Column B
X
A
NULL
X+2
NULL
B
Keep in mind I am very new to SQL. Below is what I tried. It created a new column that only contains NULL values.
Select
Column_B
From
Table_Name
Where
Table_Name.ID = Table_Name.ID +2 ) AS Col_B_Value
You can use a conditional subselect for that
UPDATE Table_Name T1
SET Column_B = (Select
Column_B
From
Table_Name
Where
Table_Name.ID = T1.ID +2 )
WHERE Column_B IS NULL
Some databases could have a problem so you can make
UPDATE Table_Name T1
SET Column_B = (Select
T2.Column_B
From
(SELECT ID,Column_B FROM Table_Name) T2
Where
T2.ID = T1.ID +2 )
WHERE Column_B IS NULL
You could just do it with 2 updates statements
UPDATE Table
SET Column B = 'B'
WHERE ID = 'X'
UPDATE Table
SET Column B = NULL
WHERE ID = 'X+2'
If you need to do it in a select statement you could do it with a case statement too
SELECT ID,
Column A,
CASE WHEN ID = X AND Column B = NULL THEN 'B'
ELSE Column B END
FROM Table

Rename category in the column in SQL Server

Here is the query
select col1
from table
col1 contains these category values:
A
B
C
NULL
How can I rename null category to D?
If you want to make the change permanent
UPDATE table
SET col1 = 'D'
WHERE col1 IS NULL
From then on you can simply query with ...
SELECT col1
FROM table
... to get the desired result.
If there is more than one row having a NULL in col1, you need to filter by a unique key, preferably by the primary key (which every table should have by the way). Let's say you have a table like
id (PK) col1
--- ----
1 'A'
2 'B'
3 'C'
4 NULL
5 NULL
then you can fix it with
UPDATE table SET col1 = 'D' WHERE id = 4;
UPDATE table SET col1 = 'E' WHERE id = 5;
unless you can calculate the new value from another column, e.g.:
UPDATE table
SET col1 = UPPER(LEFT(name, 1))
Try this : ISNULL( ) function is used to replace NULL value with another value
select isnull(col1,'D') as col1
from table
SQL Server uses ISNULL().
SELECT ISNULL(value_to_check, use_this_instead_if_valuetocheck_is_null)
For your code:
select ISNULL(col1, 'D') AS col_name
from table
However, this will happen across the board for this column. You can't use this to make a sequence, like D then E then F. Any NULL value you come across in this column will change to D.

Hiding a row in a table with a query when 3 columns have null values

I am looking for the best way to build a query which would hide the record (row) in the event that three field values (in three different columns) would be null. The code below is giving me a syntax run time error message of 3075. Also, I am not sure if it is causing a problem but the code below is executed from a main form and impacting the subform frmStaticDataSkills02.
sql_get = "SELECT [tblCompetency02].[HighLevelObjective], [tblCompetency04].[Self], [tblCompetency04].[SelfSpecialLanguage], [tblCompetency04].[SelfChecklist], [tblCompetency04].[Team], [tblCompetency04].[TeamSpecialLanguage], [tblCompetency04].[TeamChecklist], [tblCompetency04].[Organisation], [tblCompetency04].[OrganisationSpecialLanguage], [tblCompetency04].[OrganisationChecklist], [tblCompetency02].[Competency] FROM [tblCompetency04] INNER JOIN [tblCompetency02] ON [tblCompetency04].[HighLevelObjective] = [tblCompetency02].[ID] WHERE ([tblcompetency04].[self]<>"" or [tblcompetency04].[team]<>"" or [tblcompetency04].[organisation]<>"")"
Form_frmStaticDataSkills02.Form.RecordSource = sql_get
In a general sense, in a table of N columns, you can explicitly count the number of NULL columns in a table and then add them up and compare the count of nulls to 3 in a where predicate:
SELECT *
FROM MyTable x
WHERE
((IIF(x.COL1 IS NULL, 1 , 0) +
IIF(x.COL2 IS NULL, 1 , 0) +
IIF(x.COL3 IS NULL, 1 , 0) +
IIF(x.COL4 IS NULL, 1 , 0))) <> 3;
(Obviously, keep adding IIF statements for all N columns of the table
This will return the data if not all three columns are NULL:
where not (col1 is null and col2 is null and col3 is null)
This is the same after applying algebra of logic: return the row if any of the three rows is NOT NULL
where col1 is not null or col2 is not null or col3 is not null)
End your query with:
WHERE [column_1] IS NOT NULL AND [column_2] IS NOT NULL AND [column_3] IS NOT NULL
You can do that in your where clause by specifying that all 3 fields should not be NULL, the result set that is returned will not have records where all 3 fields are NULL.
SELECT *
FROM tablename
WHERE
field1 IS NOT NULL AND
field2 IS NOT NULL AND
field3 IS NOT NULL
If you do not want to have rows returned where any one of the columns has null values you would use OR. For example:
SELECT *
FROM tablename
WHERE
field1 IS NOT NULL OR
field2 IS NOT NULL OR
field3 IS NOT NULL

Select where column in not null array

I am trying to do :
SELECT *
FROM table
WHERE column IN (SELECT col FROM table2 WHERE col2 = value )
but I want to check if the second request doesn't return a null array.
How is that possible?
Thanks in advance
Simply add a NOT NULL check in the subquery to omit the null values returned.
SELECT * FROM table WHERE column IN
(SELECT col FROM table2 WHERE col2 = value AND col IS NOT NULL);

2 SQL Updates in 1 statement

Im trying to do something like the following:
UPDATE table SET fieldA='valueA' WHERE id='id1', SET fieldB='valueB' WHERE id='id2';
But cant seem to get it to work. Is there a way of doing this, or will I need to use two separate SQL commands?
If you really need to do it in one statement, you can use this:
UPDATE table
SET fieldA = case when id = 'id1' then 'valueA' else fieldA end,
fieldB = case when id = 'id2' then 'valueB' else fieldB end
WHERE id in ('id1', 'id2');
But for clarity (and thus maintainability), it would be much better to use two statements.
This is best done as 2 different UPDATE statements, because you have 2 different WHERE clauses:
UPDATE table SET fieldA='valueA' WHERE id='id1'
UPDATE table SET fieldB='valueB' WHERE id='id2'
MERGE INTO YourTable
USING ( VALUES ( 'id1', 'valueA', NULL ),
( 'id2', NULL, 'valueB' ) )
AS source ( id, fieldA , fieldB )
ON YourTable.id = source.id
WHEN MATCHED THEN
UPDATE
SET fieldA = COALESCE(source.fieldA, YourTable.fieldA),
fieldB = COALESCE(source.fieldB, YourTable.fieldB);
You'll need to use 2 separate SQL-statements because the set operator works on the whole resultset that's filtered by the where-clause.
UPDATE table SET fieldA='valueA' WHERE id='id1'; UPDATE table SET fieldB='valueB' WHERE id='id2';
UPDATE table SET fieldA='valueA',fieldB='valueB' WHERE id in ('id1','id2')