Rename category in the column in SQL Server - sql

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.

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

How to not display even one column is null in table

I have table like below:
I want result like even one column is null or empty string those records should not be display.
I want results like below:
select * from WHERE col1 IS NOT NULL AND col2 IS NOT NULL AND col3 IS NOT NULL AND col4 IS NOT NULL AND col5 IS NOT NULL....AND col9 IS NOT NULL
If you wanted an efficient way to do this, then one method would be a persistent computed column:
alter table t
add numNulls as ( (case when col1 is null then 1 else 0 end) +
(case when col2 is null then 1 else 0 end) +
(case when col3 is null then 1 else 0 end) +
. . .
) persisted;
You can index this column:
create index t_numNulls on t(numNulls);
And then you can use this in the select:
select t.*
from t
where num_nulls = 0;
That said, I suspect that your real problem is the data model. I am guessing that those 90 columns are really an "array" -- that is, all the same entity. These should be implemented as separate rows in a junction table.

insert select alias value into column

Not sure if this is the best title, but i want to select string values into an int column of a new table (the reason is to use keys with int data types rather than strings, so there are more columns not shown in this example)
table1.key1 table2.key2
a 1
b 2
c 3
a 1
one way i can do this is as follows but the syntax is very very long in some scenarios
insert into table2 (key2)
select 1
from table1
where key1 = 'a'
insert into table2 (key2)
select 2
from table1
where key1 ='b'
etc...
can someone show me how i could use a syntax that is shorter? also i have to keep identity insert set to off so an update statement will not work from what i understand.
SQL Fiddle Demo
Use a CASE expresion
insert into table2 (key2)
select CASE WHEN key1 = 'a' THEN 1
WHEN key1 = 'b' THEN 2
WHEN key1 = 'c' THEN 3
.....
ELSE -1
END as key2
from table1

Test data for unique and not null

How can I quickly check to see if the data from the test table 'test_table', and selected columns are unique and not null.
Summary, at the entrance gets a table name and a list of columns in the output are expected , eg . flag 1 or 0.
Table is big so unfortunately i must posibly fast execute;
select 1 from dual
where exsist (select col1,col2,col3,... from table
where col1 is not null and col2 is not null and col3....
group by col1,col2,col3.. having count(*) > 1 )
this will return 1 when one of the is true.
SELECT 1
FROM dual
WHERE EXISTS
(SELECT a, b FROM tab WHERE id=1
AND name='John'
AND (a IS NULL OR b IS NULL))
I changed your code and I have one question about it , now I have is that id must be 1 and the name 'John ' and any of verifying null must be correct, and wants to have that must be id = 1 and the name 'John ' and if there is either the value of the column is ' 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);