Alternative option for case condtion in sql - sql

How to check the column value zero or not
I want to insert 50 column values, each column i want to check whethere the value is 0 or not. If the value is 0 then it should be null
Query
insert into table1 values (Case when column1 = '0' then null else column1 end, .....
Case when column50 = '0' then null else column50 end)
The above query is working, but query length is too long because i am using the above query of 50 column's
there is any alternative option is there for check the column value is 0 or not.
like this if(column1, 0) then null
Need Query Help

Try using NULLIF:
insert into table1 values (NULLIF(column1, '0'), .....
NULLIF(column50, '0'))

Related

Create a Query to check if any Column in a table is Null

I have zero experience with SQL but am trying to learn how to validate tables. I am trying to see within a table if any of the columns are null.
Currently I have been going with a script that is just counting the number of nulls. I am doing this for each column. Is there a better script that I can use to check all the columns in a table?
select count(id) from schema.table where id is not null
If there are 100 records I would expect all columns to come back with 100 but if one column is null it will show a 0.
You can count each column in a single query by using sum and case:
select
sum(case when Column1 is null then 1 else 0 end) Column1NullCount
, sum(case when Column2 is null then 1 else 0 end) Column2NullCount
-- ...
, sum(case when ColumnN is null then 1 else 0 end) ColumnNNullCount
from MyScheme.MyTable

way to have query with case statement

I have table called Numbers in that column I have values from 0 - 10 but I like to keep value of 1-10 only change record of 0 too null
Case numbers
when 0
then ''
but I found this has changed all values and not values that have 0 is there way I can say else leave value as is?
Do you want this?
update t
set number = null
where number = 0;
Or as a select:
select t.*,
(case when number <> 0 then number end)
from t;
SELECT CASE WHEN [column] = 0 THEN NULL ELSE [column] END AS [SomeName]
FROM Numbers

Comparing two column values in postgres

I have table having following columns and records. I need to compare the two column values(ColumnA and ColumnB), if ColumnB>ColumnA then and update the third column from 'N' TO 'Y'
CREATE TABLE Test(ColumnA int,ColumnB int,Result Varchar(2))
INSERT INTO Test values(1,3,'N')
INSERT INTO Test values(2,1,'N')
INSERT INTO Test values(1,5,'N')
INSERT INTO Test values(8,7,'N')
I need to update Result Column='Y' for first and third row because columnB>ColumnA
Result
ColumnA ColumnB Result
1 3 Y
2 1 N
1 5 Y
8 7 N
This can be done with a simple CASE expression:
update test
set result = case
when columna > columnb then 'Y'
else 'N'
end
;
Online example: https://rextester.com/ZHIUZD82060
I would recommend to use a boolean column instead of a varchar to store "yes/no" flags. Then the update becomes as simple as set result = column_a > columnb
update test
set result =
case
when columnb > columna then 'Y'
else 'N'
end;
Hope this will help.

How to check if all rows validate a predicate

I've a table in my database for which I need to check if all rows have one field not null.
If there are no row or if there is at least 1 row with the field null => true
If there are rows and they are all with the field not null => False
Is there a way to do this in on simple query? Or I need to check if my table is empty first then if it's not check if I've a row with the field value empty ?
This will count how many NULL values you have in a field;
SELECT
SUM(CASE WHEN FieldName IS NULL THEN 1 ELSE 0 END) NullValues
FROM TableName
Will return 0 if there are no NULL values, and will return the number of NULLS if there are any present.
If you actually want to return a value as 'True' or 'False' then do this;
SELECT CASE
WHEN a.NullValues > 0
THEN 'True'
ELSE 'False'
END CheckField
FROM (
SELECT
SUM(CASE WHEN FieldName IS NULL
THEN 1
ELSE 0
END) NullValues
FROM TableName
) a
Use count(*) and count(field) and compare the two:
select
case when count(*) > 0 and count(*) = count(field) then 1 -- not empty and no nulls
else 0 end as isgood
from mytable;
Oracle SQL has no boolean data type , so I use 1 for true and 0 for false. You can replace this with whatever you like (e.g. 'true' instead of 1 and 'false' instead of 0).
As to turning this into a predicate (correlated to a main query), you'd use something along the lines of:
select ...
from main
where exists
(
select 1
from mytable
where mytable.colx = main.coly
having count(*) > 0 and count(*) = count(field)
);
You can do this with aggregation. However, it is difficult to understand what you are asking for. If you want to check that a field has no NULL values, you can do:
select (case when count(*) > 0 then 1 else 0 end) as HasNullValues
from t
where field is null;
Alternate way I found using max with putting null first:
select case when
max(field) keep (dense_rank first order by datfin desc nulls first) is null then 1
else 0 end as flag
from MYTABLE;

SQL statement - use avg and count together but in different conditions

Assume we have the table as follows,
id Col-1 Col-2
A 1 some text
B 0 some other text
C 3
...
Take the table above as example, I want to build one SQL statement which would output the result: 2, 2.
The first value is the avg of all col-1 values except for 0, that is (1+3)/2 = 2. (If 0 is counted, then the result would be (1+0+3)/3 = 1, which is not what I want.)
The second value is the total number of all col-2 that is not empty. So the value is 2.
P.S, I know how to create them separately. What I prefer is to create only 1 statement to get both results.
For the first you can use NULLIF as null values are ignored in aggregations such as AVG.
For the second I assume you want to only count values not NULL or empty string.
SELECT AVG(NULLIF(Col1, 0)),
COUNT(CASE WHEN Col2 <> '' THEN 1 END)
FROM T
You want conditional aggregation:
select avg(case when col1 <> 0 then col1 end) as avg_not_zero,
count(col2) as num_not_empty
from table t;
As a note: 0 does not mean that the value is empty. Often NULL is used for this purpose in SQL, although strictly speaking, NULL means an unknown value.
Note: If "empty" could mean the empty string instead of NULL:
select avg(case when col1 <> 0 then col1 end) as avg_not_zero,
count(nullif(col2, '')) as num_not_empty
from table t;