SQL CASE statement returns error - sql

Hi I have select CASE statement in DB2:
case when actualfinish is not null then dec (( timestampdiff(
4,
char(actualfinish - reportdate))/60.00),10,2)
else 'not'
end
It returns me error.
If I delete ELSE part there is no error and calculation is done.
case when actualfinish is not null then dec (( timestampdiff(
4,
char(actualfinish - reportdate))/60.00),10,2)
end
If I change THEN part to something like
case when actualfinish is not null then 'Yes'
else 'not'
end
also there is no error.
But for my complete SQL query - can't find an error.
Thanks

Issue here is that dec function returns a decimal datatype in the else part of the CASE statement you are returning a VARCHAR, hence the issue.
In your second version
case when actualfinish is not null then 'Yes'
else 'not'
end
Both the when and else are returning same datatype hence the query runs fine.
Change your else to send a decimal equivalent value and it should execute fine.
Update:
Use this version if you can return decimal as a string:
CASE
WHEN actualfinish IS NOT NULL THEN CHAR(DEC((TIMESTAMPDIFF(4, CHAR(actualfinish - reportdate))/60.00),10,2))
ELSE 'not'
END

Related

Problem with CASE statement syntax in teradata (BTEQ)

I've the following code with regards to inserting and checking values into the teradata database.My point is that data that is read from the flat file whose value after trimming leading 0 is 0 or NULL should not be loaded and if otherwise, the value should be loaded into the target table.......
VALUES
(
CASE STUD_ID WHEN TRIM(LEADING '0' FROM :STUD_ID) NOT IN ('0', NULL) THEN TRIM(LEADING '0' FROM :STUD_ID)
ELSE NEXT
END,
:B,
:C
)
I've unsure of if the next statement does exist for teradata in the conditioning statement...I've got this error
Illegal expression in WHEN clause of CASE expression.
Statement# 1, Info =0
I tried with the select statement in the VALUES area,
VALUES
(
SELECT (CASE STUD_ID WHEN TRIM(LEADING '0' FROM :STUD_ID) != '0' THEN TRIM(LEADING '0' FROM :STUD_ID)
ELSE 1000
END )
FROM :STUD_ID,
:B,
:C
)
I got this error statement...
Syntax error, expected something like ')' between '(' and
the 'SELECT' keyword.
The CASE ... WHEN syntax expects a single value for comparison (i.e. CASE some_expression WHEN 1 THEN 'Y'). Try the CASE WHEN ... form instead:
CASE
WHEN TRIM(LEADING '0' FROM :STUD_ID) NOT IN ('0', NULL)
THEN TRIM(LEADING '0' FROM :STUD_ID)
ELSE NEXT
END
You can also do this:
COALESCE(NULLIF(TRIM(LEADING '0' FROM :STUD_ID),0),NEXT) -- Return "NEXT" if 0 or NULL
There are several issues, ravioli fixed the syntax.
But your logic is flawed, too: TRIM(LEADING '0' FROM :STUD_ID) NOT IN ('0', NULL) will never be true because after trimming zeroes you never get '0' and additionally any cpmparison to NULL yields unknown. CASE WHEN TRIM(LEADING '0' FROM :STUD_ID) = '' OR :STUD_ID IS NULL THEN fixes this.
But based on your previous question you want to skip this row and this is not possible in BTEQ. Either switch to a load utility/TPT (preferred if it's a larger number of rows) or load the data as is in a staging table and apply the filter when INSERT/SELECTing into the target.

Use case statement in SQL

I want to change the value in a result using a case statement, but it seems to ignore this. Very simple, if the value in the table is 1 then must change to True, if the value is 0 then must change to false. Here is what I did:
CASE pbc.bShownUp
WHEN 0 THEN 'False'
ELSE pbc.bShownUp
END
AND
CASE ch.bShownUp
WHEN 1 THEN 'True'
ELSE ch.bShownUp
END
No matter what I do, the result is still 0 or 1.
Please help.
When you're using a CASE Expression, the return type of the data has to be the same for all WHEN parts or more simply put, CASE must resolve to a single type.
More reference here and here
From a Type point of view, at the moment your code says
CASE BIT
WHEN 1 then NVARCHAR
ELSE BIT
And that won't work.
So you have to do something like this
CASE ch.bShownUp
WHEN 1 then 'TRUE'
ELSE 'FALSE'
If bShownUp is numeric column, you are producing mixed result - sometimes 0/1, sometimes 'False'/'True' e.g. string. I suppose bShownUp is of type VARCHAR and you have to change WHEN 0 to WHEN '0'.
If your bShownUp is VARCHAR then your query should look like
CASE pbc.bShownUp
WHEN '0' THEN 'False'
ELSE pbc.bShownUp
END
AND
CASE ch.bShownUp
WHEN '1' THEN 'True'
ELSE ch.bShownUp
END
OR
Try something like below,
CASE pbc.bShownUp
WHEN 0 THEN 'False'
ELSE CAST(pbc.bShownUp AS VARCHAR(10))
END
AND
CASE ch.bShownUp
WHEN 1 THEN 'True'
ELSE CAST(ch.bShownUp AS VARCHAR(10))
END
I think this will do:
case ch.bShownUp
when 1 then 'True'
else 'False'
end

CASE logic when removing NULLs

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)

How to write case in sql server?

In sqlserver, I write a query in that i use "case" but it is giving error, this is my case.
(case when sm.SegCode =0 then '' else sm.SegCode = 7 end)
please help me.
Thanks for all for giving response, actually I have a parameter #id. Now I want to check when it is not zero I check that condition sm.segcode else if #id is zero then I don't want to check the condition that is sm.segcode = #id.
There are two issues with the statement you showed:
the syntactic error of else sm.SegCode = 7
the attempted mixing of types with the empty string and the int
Try this instead:
case when sm.SegCode = 0 then '' else '7' end
Of course that is partially a guess, because I'm not sure exactly what you are trying to achieve by setting the result to either an empty string or the integer 7.
this would be syntactically correct:
case sm.SegCode when '0' then '' else '7' end
or
case sm.SegCode when 0 then NULL else 7 end
if that columns allows NULL's that is
you can see more about case when in the documentation: http://msdn.microsoft.com/en-us/library/ms181765.aspx
Always try to return Same DATA TYPE value from Case statement, seems your first condistion returns VARCHAR whereas the else section returns INT, it's not possible, so best to convert each value to VARCHAR, so 7 will be '7' in else statement, thanks
(CASE
WHEN sm.SegCode = 0 THEN ''
ELSE '7' END)

TSQL CASE LTRIM (RTRIM NULL

SQL Syntax is still something I am learning. I am getting the error noted below the this snippet of code.
SELECT
CASE WHEN LTRIM(RTRIM(cLehmanNo)) =' ' THEN NULL
WHEN cLehmanNo IS NOT NULL THEN REPLACE ( cLehmanNo,SUBSTRING (cLehmanNo,PATINDEX( '%[^a-zA-Z0-9 '''''']%',cLehmanNo),1), ' ' )
END asLOAN_NUMBER
,CASE WHEN LTRIM(RTRIM(cMERS)) =' ' THEN NULL
WHEN cMERS IS NOT NULL THEN REPLACE ( cMERS,SUBSTRING (cMERS,PATINDEX( '%[^a-zA-Z0-9 '''''']%',cMERS),1), ' ' )
END asMERS_ID
and 100+ more of same.
Msg 8133, Level 16, State 1, Line 1
None of the result expressions in a CASE specification can be NULL.
What am I doing wrong? How do I keep the gist of the statement and not get this crazy error?
This happens when it can't infer the type.
e.g.
SELECT CASE WHEN 1 = 2 THEN NULL ELSE NULL END
But this works
SELECT CASE WHEN 1 = 2 THEN NULL ELSE replace(NULL,'','') END
so I doubt the error is from the code you have shown us (You are using string functions and the following quick test shows that it will assume that to be varchar(8000))
SELECT CASE WHEN 1 = 2 THEN NULL ELSE REPLACE(NULL,'','') END a
INTO t /*Creates column of datatype varchar(8000)*/
You need to convert NULL to a correct type matching the overall values, e.g. CONVERT(VARCHAR(10), NULL), otherwise the server can't deduce which type to make the resulting value.
The error message actually means that all results in one of your case expressions are null. You have an expression like:
case when something then null when something then null end
At least one of the results has to be something other than null. You could circumvent this, but most likely there is a mistake in the query, as a case exression that always returns the same result is pointless.
The error message has been changed to:
At least one of the result expressions
in a CASE specification must be an
expression other than the NULL
constant.
SELECT
CASE WHEN LTRIM(RTRIM(cLehmanNo)) =' ' THEN NULL
WHEN cLehmanNo IS NOT NULL THEN REPLACE ( cLehmanNo,SUBSTRING (cLehmanNo,PATINDEX( '%[^a-zA-Z0-9 '''''']%',cLehmanNo),1), ' ' )
ELSE ''
END asLOAN_NUMBER
,CASE WHEN LTRIM(RTRIM(cMERS)) =' ' THEN NULL
WHEN cMERS IS NOT NULL THEN REPLACE ( cMERS,SUBSTRING (cMERS,PATINDEX( '%[^a-zA-Z0-9 '''''']%',cMERS),1), ' ' )
ELSE ''
END asMERS_ID