I have a column transaction_value , here is the requirement if the value is empty then i need to replace it with null
i have tried the below case statement
CASE WHEN nc.transaction_value WHEN IS NULL
THEN nc.transaction_value
ELSE nc.transaction_value
END nc.transaction_value
Related
I have a table with following columns :
SalaryStructure:
Id StructureName IsApplicable IsActive
IsApplicable - bit, not null
"IsApplicable" is a newly added column which has all the values as "false" by default.
Now, I want to update "IsApplicable" based on pattern matching logic and then set the value of "IsApplicable" based on that.
Query:
SELECT
CASE
WHEN (StructureName LIKE '%Associate1%' OR StructureName Like '%Tier1%')
THEN 'No' ELSE 'Yes' END AS IsApplicable,
FROM SalaryStructure
WHERE IsActive = 0
I want to run above script on same table SalaryStructure and update the values "Yes" and "No" in the column "IsApplicable".
But I am not getting how to include this part in "Update" statement. I want update all the records in the SalaryStructure table.
Can someone please help me?
Neither 'Yes' or 'No' are valid bit values, a non-NULLable bit column can store 1 or 0 and that is it. I therefore assume 'No' should be 0 and 'Yes' should be 1.
As for the UPDATE it would, in truth, look like another other UPDATE on a single table. UPDATE...SET...WHERE:
UPDATE dbo.SalaryStructure
SET IsApplicable = CASE WHEN (StructureName LIKE '%Associate1%' OR StructureName LIKE '%Tier1%') THEN 0
ELSE 1
END
WHERE IsActive = 0;
I have condition in datastage which verifies 30 columns as not null and assigning value as '0' if anyone of the field is null like below,
isnull(columnA) or isNull(coloumnB) or isnull(columnC) or isNull(coloumnD) then 0 else 1.
we have option to use case statement in hive to set values, however we have to give use CASE statement per column like below,
select
case when (columnA is NULL) then 0
case when (columnB is NULL) then 0
case when (columnC is NULL) then 0
.
.
.
else 1
end as iValidColumn
From tablea
What i am looking?
Trying to look for option where we can validate all the 30 columns in condition for null validation.
This is my first post, and I attempted to do a thorough search for this issue, so please accept my apologies if it has been posted elsewhere many times, but I'm wondering if anyone has encountered the following issue when attempting to remove NULLs from their result set:
case Occurrence
when NULL then '0'
else occurrence
end as Occurrence,
case Aggregate
when NULL then '0'
else Aggregate
end as Aggregate,
This didn't do anything to my NULLs; however, this did the trick:
case
when occurrence is NULL then '0'
else occurrence
end as Occurrence,
case
when aggregate is NULL then '0'
else Aggregate
end as Aggregate
Does anyone have any idea why this behaves this way? I'm using SQLServer2012.
I'm also not very versed in programming and only have less than a year SQL experience.
Thanks!
You should be using the ISNULL() or COALESCE() system function for handling nulls
something like
SELECT ISNULL(Occurrence , 0) AS Occurrence
,ISNULL(Aggregate , 0) AS Aggregate
FROM Table
OR
SELECT COALESCE(Occurrence , 0) AS Occurrence
,COALESCE(Aggregate , 0) AS Aggregate
FROM Table
The reason it didn't work in the case statement with
case Occurrence
when NULL then '0'
else occurrence
end as Occurrence,
is because it is interpreting it as
CASE
WHEN Occurrence = NULL THEN 0
ELSE Occurrence
END
Null is checked in sql server using IS NULL or IS NOT NULL if you use any other operator with null like = , <> or <, < it yields NULL hence the unexpected results.
Only for SQL Server 2012 and Later
In sql server 2012 and later versions you also have the IIF function
SELECT IIF(Occurrence IS NULL, 0, Occurrence) AS Occurrence
,IFF(Aggregate IS NULL , 0, Aggregate) AS Aggregate
FROM Table
You use simple case:
The simple CASE expression operates by comparing the first expression to the expression in each WHEN clause for equivalency. If these expressions are equivalent, the expression in the THEN clause will be returned.
Allows only an equality check.
case Occurrence
when NULL then '0'
else occurrence
end as Occurrence,
Which is executed as :
case
when occurence = NULL then '0'
else occurrence
end as Occurrence
Then expression occurence = NULL return NULL and is treated like False
Second your case use searched CASE with full condition and works fine:
case
when occurrence IS NULL then '0'
else occurrence
end as Occurrence,
So your question is about difference column IS NULL vs column = NULL
try
select 1 where null =null
select 1 where null is null
your statement looks like null equals null
select case when null is null then 1 else 0 end
select case null when null then 1 else 0 end
In your case use ISNULL this will give you the results your after
SELECT ISNULL(null,1)
I have this:
**value**
S:581930640 | P:581930640
And I would like to get the value as in Oracle:
**valuaA ValueB**
581930640 581930640
select
case
when field like 's:%'
then substr(field,3,13)
else null
end as A,
case
when field like 's:%'
then substr(field,17)
else null
end as B
from table;
both the case condition are same, but the substring is different, this should do what you are trying to do.
I have a column with some nulls. If that column is null, I want to condition output for it based on values in another column.
So if case when null (if c=80 then 'planb'; else if c=90 then 'planc')
How would you code that in an inline T-SQL statement?
thanks.
COALESCE(YourColumn, CASE c WHEN 80 then 'planb' WHEN 90 THEN 'planc' END)
You can also use the nested case statement. Assuming that the first column is called DataColumn.
CASE
WHEN DataColumn IS NULL THEN
CASE c
WHEN 80 THEN 'planb'
WHEN 90 THEN 'planc'
ELSE 'no plan'
END
ELSE DataColumn
END