Operator 'is true' for type 'long' not found for oracle sql query - sql

I am trying to run this logic, where i get output as follows,
My code to get the below output field is shown below
select max(dtm) over (partition by name ,id )-current_date from mm
output
-4168
-4168
-4168
-4127
what i want is to run this logic along with 'case when' statement so i tried:
case when max(dtm) over (partition by name ,id )-current_date then 'yes'
else 'No' end as output
from mm
but i get an error as follows, not sure what went wrong in this logic.
Operator 'is true' for type 'long' not found

There are two forms of CASE expression. One is referred to as simple case expression and the other is referred to as searched case expression. The SQL in your question uses the latter, i.e. searched case expression. I believe you probably need simple case expression, i.e.
select case max(dtm) over (partition by name ,id ) - current_date
when -4168 then 'Yes'
else 'No'
end as answer
from mm

select
case when current_date-max(dtm) over (partition by name ,id ) < 30 then 'yes'
else 'No' end as 'output'
from mm

Related

How to write case condition to check particular value exist among multiple values in sql?

I have a column in table which is having single/multiple value. I have to convert it yes/no based on condition.
example:
rendition_type_sys
distribution
uploaded, distribution
uploaded
single
I need to change the value based on condition. If column having distribution then value should convert as 'Yes' otherwise 'No'
Final Output:
rendition_type_sys
Yes
Yes
No
No
I tried one case statement but that is working for single value not multiple value-
case when ren.rendition_type__sys='distribution' then 'Yes' else 'No' end as rendition_type__sys
First, you should fix your data model so you are not storing multiple values in a string.
In the meantime, like should do what you want. For your example:
(case when ren.rendition_type__sys like '%distribution%'
then 'Yes' else 'No'
end) as rendition_type__sys
Note: In case "distribution" is part of an element name and you don't want that, you can check for delimiters. In Standard SQL, this would be:
(case when ', ' || ren.rendition_type__sys || ', ' like '%, distribution, %'
then 'Yes' else 'No'
end) as rendition_type__sys
The string concatenation operator may vary depending on the database you are using.
This below code would work assuming distribution is not part of any other discrete value; here is the code in a sample form:
WITH rt AS (
SELECT 'distribution' AS rendition_type_sys
UNION SELECT 'uploaded, distribution'
UNION SELECT 'uploaded'
UNION SELECT 'single'
)
SELECT CASE WHEN rt.rendition_type_sys LIKE 'distribution' THEN 'Yes' ELSE 'No' END AS Col
FROM rt
You might consider adding a table with for these "tags" so that they are discrete.

Select with IF statement on postgresql

I have a code like that:
select
tbl.person
,COUNT(distinct tbl.project)
,if (tbl.stage like '%SIGNED%') then sum(tbl.value) else '0' end if as test
from
my_table tbl
group by
1
And it returns me that error message:
SQL Error [42601]: ERROR: syntax error at or near "then"
I didn't got it. As I saw on documentation, the if statement syntax appears to be used correctly
IF is to be used in procedures, not in queries. Use a case expression instead:
select
tbl.person
,COUNT(distinct tbl.project)
,sum(case when tbl.stage like '%SIGNED%' then tbl.value else 0 end) as test
from
my_table tbl
group by
1
Notes:
tbl.stage is not part of the group by, so it should most probably be enclosed within the aggregate expression, not outside of it
all values returned by a case expression need to have the same datatype. Since sum(tbl.value) is numeric, the else part of the case should return 0 (number), not '0' (string).
In Postgres, I would recommend using filter:
select tbl.person, COUNT(distinct tbl.project)
sum(tbl.value) filter (where tbl.stage like '%SIGNED%') as test
from my_table tbl
group by 1;
if is control flow logic. When working with queries, you want to learn how to think more as sets. So the idea is to filter the rows and add up the values after filtering.
replace
if (tbl.stage like '%SIGNED%') then sum(tbl.value) else '0' end if as test
with
sum(case when tbl.stage like '%SIGNED%' then tbl.value end) as test

CASE expression for NULL condition is not working

I have an SQL query where the case expression is not working because I am getting the NULL value.
Any idea how to fix this?
select
td.reportEndDate,
CASE td.originalLinearAirDate
WHEN NULL THEN '12345678'
END As originalLinearAirDate
from
FROM DBA.Telecast td
where id = 2
order by
td.reportEndDate,
originalLinearAirDate;
You can use isnull
select
td.reportEndDate,
CASE WHEN td.originalLinearAirDate IS NULL THEN '19000101'
ELSE td.originalLinearAirDate
END As originalLinearAirDate
from
FROM DBA.Telecast td
where id = 2
order by
td.reportEndDate,
originalLinearAirDate;
You can use COALESCE() :
SELECT td.reportEndDate,
COALESCE(td.originalLinearAirDate, '12345678') AS originalLinearAirDate -- Use default date instead of '12345678'
FROM DBA.Telecast td
WHERE id = 2
ORDER BY td.reportEndDate, originalLinearAirDate;
In your case expression you didn't specified ELSE part hence you got NULL.
However, case expression will only return one type. So, you should check code or do necessary conversation.
The problem is the NULL comparison. The comparison is never true, even when used from comparison in a CASE expression.
If you wanted to do this using CASE, then you need to use IS NULL:
(CASE WHEN td.originalLinearAirDate IS NULL
THEN '12345678'
END) As originalLinearAirDate
If you want to return the original value in this case, you need an ELSE:
(CASE WHEN td.originalLinearAirDate IS NULL
THEN '12345678'
ELSE td.originalLinearAirDate
END) As originalLinearAirDate
Note that this will return an error if the column is really a DATE, because '12345678' cannot be converted to a date.
This version is better expressed using COALESCE():
COALESCE(td.originalLinearAirDate, '12345678')

SQL query returning error only when CASE WHEN is used

I have the sql code below and the case when statement is always returning the 'Error converting data type varchar to numeric'. But if I comment it out then the statement runs successfully even though the same code is used in the first column.
SELECT CAST(DATA1 AS DECIMAL(10,5)), AMOUNT, CASE WHEN NAME ='A' THEN CAST(DATA1 AS DECIMAL(10,5)) ELSE '0' END FROM TEST
If I run as is above, it fails. If I comment out the Case when piece then it runs successfully. Thanks!
Your CASE statement is trying to return two different types, DECIMAL(10,5) and varchar.
It needs to return one type, so instead of having '0' just use 0.0.
You are telling it to display two different data types in the same column. Try;
SELECT CAST(DATA1 AS DECIMAL(10,5)), AMOUNT,
CASE
WHEN NAME LIKE 'A' THEN CAST(DATA1 AS DECIMAL(10,5))
ELSE CAST(0.0 AS DECIMAL(10,5)) END FROM TEST

Using regular expressions in conditional expressions in Postgresql v.9.6

I have a table with a column I denote with "col" containing character values. I would like to extract a column such that at the position where the column "col" starts with either '01', '02' or '03' the column will have the value "yes" and otherwise "no". I tried with the following code and can't get it to work.
SELECT
CASE WHEN col ~ '^(01|02|03)' THEN 'yes'
ELSE 'no' END
FROM table;
I know that one solution is to use the LIKE operator in the following way, but I am looking for a more compact way to solve this.
SELECT
CASE WHEN col LIKE '01%' THEN 'yes'
WHEN col LIKE '02%' THEN 'yes'
WHEN col LIKE '03%' THEN 'yes'
ELSE 'no' END
FROM table;
Any solution, even without regular expression, would be very helpful.
Thanks // Henri