Condition to get the NULL values and Blank values - sql

I need help in creating the condition in stored procedure.
I have a two columns in my sp named col1,col2 and actually i have a data in my view named[SampleData] and in this view i have one column [col_new] which has both NULL values and Blank Values.
The Condition is:
If the [col_new] has an NULL, then display the [col_new] NULL in the col1 field.
If the [col_new] has a Blank, then display the [col_new] Blank in the col2 field.
So how can i add the condition here using if and else in CASE Condition?

Do you just want case expressions?
select (case when col_new is null then '[col_new] is null' end) as col1,
(case when col_new = '' then '[col_new] is blank' end) as col2
from SampleData;

Related

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.

First column with NULL value in a table

I would like to get to know how I can get the first column with NULL value from the left for each row in my table, I've tried with SELECT CASE but it doesn't work the way I would like.
Guys, I'd like to be crystal-clear about what I want to accomplish. I have a table with 22 columns and there are rows in which last 10 columns have NULL values but I need to get to know only a name of the first column from the left with NULL value.
You get the value from the first non-NULL column using coalesce():
select coalesce(col1, col2, col3, . . .)
You can get the name using case logic:
select (case when col1 is not null then 'col1'
when col2 is not null then 'col2'
. . .
end)
Just specify NULL as your first field selection.
SELECT NULL, FieldA, FieldB, FieldC etc
FROM yourtable
The only general approach here is case statement:
Case
when col1 is null then 'col1'
when col2 is null then 'col2'
when col3 is null then 'col3'
end as frst_null
This way frst_null would contain the name of the first column containing Null value. You can order columns whichever order you like.

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

Retrieve single result with 2 columns in oracle

Do we have any function in oracle do this scenario.
There are 2 columns in table col1, col2.
col1 either has 'Y' or 'N' or NULL
col2 eitehr has 'Y' or 'N' or NULL
But always only one of the column has value 'Y'.
So , i want to check first col1 whether it has 'Y' ,if it has i want a string "COL1 found" ,if col2 has 'Y' i want a string "COL2 found". Is it possible with NVL2 and Decode function. With one column ,i can able to do that. But here i have to check 2 columns .Please note that i want a result in single row.
Regards,
Chaitu
select case when col1 = 'Y' then 'Col1 found' when col2 = 'Y' then 'Col2 found' end f
from yourtable
UPD.
select decode (col1, 'Y', 'col1 found', decode(col2, 'Y', 'col2 found')) from yourtable

Alternative option for case condtion in 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'))