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?
Related
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
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
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;
I know the following is legitimate SQL query, but why this cannot be interpreted in SQL Server Compact? (I'm using SQL Server Compact view)
Select
Case AStatus
When 1 then 'Success', 'AStatus', 'Expected:1'
When 0 then 'Faliure', 'AStatus', 'Recived: 0'
end
From Statuses
Where LocalPath= 'c:\Status
I get something like:
Query 1 : There was an error parsing the query [Token line number=3, Token line offset=22, Token in Error=,]
And when writing something like the following it works:
Select
Case AStatus
When 1 then 'Success'
When 0 then 'Faliure'
end
From Statuses
Where LocalPath= 'c:\Status
I think that's the only valid way to get three columns from case:
Select
Case AStatus
When 1 then 'Success'
When 0 then 'Faliure'
END,
Case AStatus
When 1 then 'AStatus'
When 0 then 'AStatus'
END,
Case AStatus
When 1 then 'Expected:1'
When 0 then 'Recived: 0'
END
From Statuses
Where LocalPath= 'c:\Status'
EDIT:
Another way. Not much shorter, but seems more flexible:
Select
Astatus,
x.*
From Statuses s
CROSS APPLY (
select 'Success' as c1,'AStatus' as c2,'Expected:1' as c3 where AStatus=1 union all
select 'Failure' ,'AStatus','Recived:0' where AStatus=0
) x
Where LocalPath= 'c:\Status'
You get +1 for making me come up with that, I already know where I'll use it :).
Your statement is missing the END Cluase in the CASE statement I dont know if this is the only error try this 1st
Select
Case AStatus
When 1 then 'Success', 'AStatus', 'Expected:1'
When 0 then 'Faliure', 'AStatus', 'Recived: 0'
END
From Statuses
Where LocalPath= 'c:\Status
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