Case in Select Query has syntax error - sql

I want to return a boolean according to a condition on one of the column of my table. I tested it in SQL Server 2014 and it works, but I have no experience in Access.
This is the query I have at the moment, using Access 2007.
SELECT (CASE WHEN Type = 'C' THEN 1 ELSE 0 END) AS EstContrat
FROM Historique_EnTete
Type has the Text type.
I have translated the error message to :
"Syntax error (missing operator) in the expression << (CASE WHEN Type = 'C' THEN 1 ELSE 0 END) >>"
What am I missing?

In access you have IIF
SELECT IIF(Type = 'C', 1, 0 ) AS EstContrat
FROM Historique_EnTete
As Lamak say you also have SWITCH

Related

Oracle PLSQL using a dynamic variable in a where clause

For testing in Toad, I have the following code
select ...
from ...
where term_code = :termcode AND
(
case
when :theSubject is not null then SUBJ_CODE = :theSubject
else 1 = 1
end
)
AND ptrm_code <> 8
In short: If theSubject is not entered (is null) I want to display all the courses, otherwise I want to display only those where subject_code is the same as the one entered in the variable window in Toad.
But I get an error:
[Error] Execution (77: 68): ORA-00905: missing keyword
in here:
when :theCourse is not null then sect.SSBSECT_SUBJ_CODE = theCourse
You can use boolean logic:
where
term_code = :termcode
and (:theSubject is null or subj_code = :theSubject)

Denodo - Unable to case DATE>= addday(cast(now() as date),-365)

I've encountered an issue when trying to obtain the following output:
"If x_date >= now-365 then 1 else 0"
My select statement reads:
SELECT
id,
x_date,
CASE x_date
WHEN x_date >= addday(cast(now() as date),-365) then 1
else 0
end as output
I'm receiving an error message that reads:
"SQL Error [30100] [HY000]: CASE argument case((xdate,ge,[addday(trunc(cast('date', now(), 'DATE')) '-365')], utc_il8n), 'true', 'false') is not compatible with the rest of the values.
Has anyone else performed a similar operation with dates in a CASE statement? The Addday works fine and returns 2017-01-05.
Issue with the CASE syntax. I think reading this and other sources may have cause the confusion: https://www.techonthenet.com/sql_server/functions/case.php
Should read:
SELECT
id,
x_date,
CASE WHEN x_date >= addday(cast(now() as date),-365) then 1
else 0
end as output

RODBC "invalid character\n" error when using CASE WHEN statement

I'm attempting to execute a simple "if/then" statement on an Oracle SQL database using RODBC in R. The SQL statement works fine in SQL Developer v4.0.2.15 but throws an error when performing the same statement in R
sqlQuery(channel, "
select
Variable1,
Variable2,
CASE WHEN Variable1 = 0 then 0 else 1 end as Var3
from schema.TABLE
where ROWNUM<100;
"
)
The error message (updated):
[1] "[1] "HY000 936 [Oracle][ODBC][Ora]ORA-00936: missing expression\n"
[2] "[2] ...
The statement works fine when removing the CASE WHEN line, so the error must be in the CASE WHEN syntax.
I've found a semicolon sometimes causes problems in cases like this.
> sqlQuery(con, "select case when dummy = 'X' then 1 else 0 end from dual")
CASEWHENDUMMY='X'THEN1ELSE0END
1 1
> sqlQuery(con, "select case when dummy = 'X' then 1 else 0 end from dual;")
[1] "HY000 911 [Oracle][ODBC][Ora]ORA-00911: invalid character\n"
[2] "[RODBC] ERROR: Could not SQLExecDirect 'select case when dummy = 'X' then 1 else 0 end from dual;'"
> close(con)
If you look at the SQL statement (which seems to appear in it's entirety in the error message), we see:
'\nselect\n UW_NET_ULTIMATE_LOSS_USD,\n UW_NET_WRITTEN_PREMIUM_USD,\n
IF UW_NET_WRITTEN_PREMIUM_USD = 0 THEN testvar=0 ELSE testvar=1 end\n
from bqiread.POLICY_METRICS\nwhere ROWNUM<100;\n
There's no CASE statement here....it apprears to be an IF/THEN/ELSE, which is not valid Oracle syntax.
Perhaps your tool is generating SQL for a different RDBMS than Oracle, and attempting to execute it in Oracle?

Microsoft ODBC Excel Driver Syntax Error (missing operator) in query expression

I am using XL Report Builder version 2.1.4 and trying to execute the following script but, keep getting the error "Microsoft ODBC Excel Driver Syntax Error (missing operator) in query expression".
select Bidders.*
From Bidders
where
If :Param1 = 1 Then
Due > 0 and
Left(Event,2) <> 12
Else
Due = 0 and
Left(Event,2) <> 12
End If
order by
Name
Param1 is a user-entry parameter that can be either "0" or "1".
Does anyone have any advice as to what operator I am missing?
The if in the query looks suspicious. Try writing the query as:
select Bidders.*
From Bidders
where (:Param1 = 1 and Due > 0 and Left(Event,2) <> 12) or
(:Param1 <> 1 and Due = 0 and Left(Event,2) <> 12)
order by Name;
You can simplify this as:
select Bidders.*
From Bidders
where Event not like '12%' and
((:Param1 = 1 and Due > 0) or
(:Param1 <> 1 and Due = 0)
)
order by Name;

How to scrape redirected URL with R

I have afunction
testFun <- function(myTeam){
print(myTeam)
teamResults <- sqlQuery(channel,paste(
"
SELECT soccer.tblResultsallMore.TEAMNAME,
sum(case when soccer.tblResultsallMore.RES='W' then 1 else 0 end) as W,
sum(case when soccer.tblResultsallMore.RES='L' then 1 else 0 end) as L,
sum(case when soccer.tblResultsallMore.RES='D' then 1 else 0 end) as D
FROM soccer.tblResultsallMore
WHERE soccer.tblResultsallMore.TEAMNAME=myTeam
GROUP BY soccer.tblResultsallMore.TEAMNAME
"))
return(teamResults) # no error if this is not returned
}
testFun("Everton")
I f I hardcode 'Everton' in the code, I get the required output
[1] "Everton"
TEAMNAME W L D
1 Everton 1734 1463 1057
but with the parameter there is an error
[1] "Everton"
[1] "42S22 207 [Microsoft][ODBC SQL Server Driver][SQL Server]Invalid column name 'myTeam'."
[2] "[RODBC] ERROR: Could not SQLExecDirect
Help much appreciated
In the code you provided the name myTeam is not replaced, it is seen as character string, and the sql statement looks for the team called myTeam.
variabel = "spam"
paste("from table select variabel")
Does not put "spam" in the sql statement inside paste. The correct syntax is:
paste("from table select ", variabel)
In your situation I would use sprintf. An example:
variabel = "spam"
sprintf("from table select %s", variable)
For more detail see the documentation of sprintf.
In regard to your remark, if there is no explicit return statement, the last evaluated expression is returned. For a discussion see:
Explicitly calling return in a function or not