An expression of non-boolean type - sql

With this:
select puser_id, puser_name, plast_login_time, plicense_level =
case
when '1' then 'Потребитель'
else 'Автор'
end
from dbo.PPOM_USER
order by plast_login_time
I have error like this:
An expression of non-boolean type specified in a context where a
condition is expected, near 'then'.
Please help me to find the problem in code.

Change your WHEN to:
when plicense_level = '1'
You need to have a valid comparison in WHEN to avoid the error.
From the CASE documentation:
WHEN when_expression
Is a simple expression to which input_expression is compared when the
simple CASE format is used. when_expression is any valid expression.

Your syntax for the CASE statement expression isn't quite right, try:
case plicense_level
when '1' then 'Потребитель'
else 'Автор'
end

If what you're trying is to show the user a text describing the licennse instead of the numeric value of the column you should try something like this:
select
puser_id,
puser_name,
plast_login_time,
case plicense_level
when '1' then 'Потребитель'
else 'Автор'
end as plicense_level
from dbo.PPOM_USER
order by plast_login_time
You can find some samples of CASE in this page at MSDN

Thank all of you!
The dicision was in changing data type of field plicense_level in report
Figure 1
Figure 2

select puser_id, puser_name, plast_login_time, plicense_level =
case
when plicense_level = '1' then 'Потребитель'
else 'Автор'
end
from dbo.PPOM_USER
order by plast_login_time

Related

select case when number then char in postgresql

I'm trying to make a query that in case detect a number and in then shows me a char but not works. My sentence is:
SELECT xxxx
CASE
WHEN status=11 THEN 'OK'
WHEN status=33 THEN 'BAD'
ELSE status
END,
...............
It gives me an error:
ERROR: invalid input syntax for integer: "OK"
What's wrong? Any help, please?
Thanks and sorry for my English!
You need to convert to a string. Something like this:
(case when status = 11 then 'OK'
when status = 33 then 'BAD'
else status::text
end),
A case expression returns a single value, with a specified type. By the rules of SQL, when you mix strings and numbers, numbers are preferred.
Postgres makes it easy to convert the value you want to a string in the else clause.

SQL How to check if field value is >0 (case when)?

I try to do an aggregation with two fields but only if the value of one field is not 0.
SELECT
CASE WHEN (CC90_StatisticQueueData.NBR_OF_CONVERSATIONS)>0
THEN (CC90_StatisticQueueData.SUM_WAITING_DURATION / CC90_StatisticQueueData.SUM_WAITING_DURATION)
FROM
CC90_StatisticQueueData
I always receive **ERROR**:
Incorrect syntax near the keyword 'FROM'.`
and don't know how to fix it.
Thanks in advance.
Use the END Keyword in CASE Statement :
SELECT CASE WHEN (CC90_StatisticQueueData.NBR_OF_CONVERSATIONS)>0
THEN (CC90_StatisticQueueData.SUM_WAITING_DURATION /
CC90_StatisticQueueData.SUM_WAITING_DURATION) END
FROM CC90_StatisticQueueData

How to check the date format of the cell (oracle)

Please, help)
I need to verify that cells are populated with correct values.
I have a few types of column with a few format of value, i.e. '2015-05-20Z' and 'XXX/MOSCOW/XXXMSX/2015-05-20'.
I do not know how to check the date, if it is only part of the value.
This query work:
case
when A like 'XXX/MOSCOW/XXXMSX/%'
then 'true'
else 'false'
end
But it doesn't enough...
This query doesn't work:
case
when A like 'XXX/MOSCOW/XXXMSX/[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]'
then 'true'
else 'false'
end
You appear to be trying to use like to match regular expressions. If that is your intent, look into regexp_like instead.
use this:
case
when regexp_like(A, 'XXX/MOSCOW/XXXMSX/[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]')
then 'true'
else 'false'
end

Invalid character found in string argument 'DECFLOAT'

I seem to be having a problem using with a filed when used in a case statement. For example when I try:
select a.*,
case when value > 0 then non_zero
else value
from mytable a
where I get:
Invalid character found in string argument of the function 'DECFLOAT'
usually I come across this type of error by forgetting to cast the field as decimal, but this makes no difference. However if I try:
select * from mytable where value > 0
then this runs fine.
So why will my criteria not work within the case statement?
I think you forgot your "end case". Try this:
select a.*,
case
when value > 0 then non_zero
else value
end case
from a

SQL CASE in Query - Odd behavior

Using a Case Statement in a query seems to work fine as long as the only case you want to match to is a string. But if you want to put ">" greater than (date or number), etc, SSMS automatically adds apostrophes around your statement (below) as if it were a string.
How can I get it to accept "greater than date" or "greater than number", etc??
There are two types of CASE expression:
The simple CASE expression compares an expression to a set of simple expressions to determine the result.
The searched CASE expression evaluates a set of Boolean expressions to determine the result.
You want a searched CASE expression. This means that the WHEN should come immediately after the CASE keyword.
CASE WHEN SoldDate <= SubjectDate THEN ...
There are two types of CASE expressions (from msdn):
A Simple CASE compares the specified expression for EQUIVALENCY ONLY
SELECT FieldName CASE WHEN 'Blah' THEN 'Foo' ELSE 'Bah' END
A Searched CASE can do inequalities too but has more verbose syntax:
SELECT CASE WHEN FieldName >= 'Blah' THEN 'Foo' ELSE 'Bah' END
You are trying to use the Simple syntax and need to use the Searched syntax by explicitly writing out the fieldname and comparison for each evaluation.
Your CASE statement syntax is incorrect. WHEN should come directly after CASE
This:
CASE SoldDate WHEN ...
Should be this:
CASE WHEN SoldDate <= SubjectDate THEN SoldFor ELSE EstSalePrice END
Additional Information
Take a look at the MSDN docs for additional examples of CASE syntax.