Error making a selection in Teradata with R - sql

When I try to import teradata data with a where in the sql statement I get the following error
clients<-dbGetQuery(con, "SELECT * from clients where year(cl_dataentrada) = 2018")
Error in new_result(connection#ptr, statement) :
nanodbc/nanodbc.cpp:1344: 42000: [Teradata][ODBC Teradata Driver]Teradata DatabaseSyntax error: expected something between the 'where' keyword and the 'year' keyword.
clients<-data.frame(clients)
I've also tried:
clients<- dbSendQuery(con, "SELECT * from clients where year(cl_dataentrada) = 2018")
Error in new_result(connection#ptr, statement) :
nanodbc/nanodbc.cpp:1344: 42000: [Teradata][ODBC Teradata Driver]Teradata DatabaseSyntax error: expected something between the 'where' keyword and the 'year' keyword.
Also in a sql chunk :
SELECT * from clients where year(cl_dataentrada) = 2018
Error in new_result(connection#ptr, statement) :
nanodbc/nanodbc.cpp:1344: 42000: [Teradata][ODBC Teradata Driver]Teradata DatabaseSyntax error: expected something between the 'where' keyword and the 'year' keyword.
Failed to execute SQL chunk

As Shiva Prakash suggested this finally works:
clients<-dbGetQuery(con, "SELECT * from clients where extract(year from cl_dataentrada)=2018")
It was an error because the function year is an odbc function not Teradata.
Thank you.

Related

Nested select statements in impala sql

I have the following SQL query in impala
SELECT currentdate,close
FROM ( SELECT * FROM spyprice)
Where currentdate between '2015-01-16' and '2016-06-17';
And it is giving me the error:
Starting Impala Shell without Kerberos authentication
ERROR: AnalysisException: Syntax error in line 15:
WHERE currentdate BETWEEN '2015-01-16' and '2016-06-17'
^
Encountered: WHERE
Expected: AS, DEFAULT, IDENTIFIER
CAUSED BY: Exception: Syntax error
Anyone knows what's going on?
Thanks in advance!
#James Xiang The right syntax of the query statement is :
SELECT a.currentdate, a.close FROM
(
SELECT * FROM spyprice
) a
Where a.currentdate between '2015-01-16' and '2016-06-17';

Can't make view from multiple table

I'm trying to learn about view in SQL. In my case, i can't execute statement to create view. Here is the statement:
CREATE VIEW VTOTALMINUTESEMPLOYEE (EMPL_KODE, EMPL_NAME,DATE_IN, TIME_IN, TIME_OUT, HASIL)
SELECT
EMPLOYEE.EMPL_KODE,
EMPLOYEE.EMPL_NAME,
ATTANDENCE.DATE_IN,
ATTANDENCE.TIME_IN,
ATTANDENCE.TIME_OUT,((TIME_OUT-TIME_IN)/60) AS "RESULT"
FROM EMPLOYEE
JOIN ATTANDENCE
ON EMPLOYEE.EMPL_KODE=ATTANDENCE.EMPL_KODE
This is the errror code :
SQL Message : -104 Invalid token
Engine Code : 335544569 Engine Message : Dynamic SQL Error SQL
error code = -104 Token unknown - line 2, column 9 SELECT
What's wrong with my code ? is there a thing that i missed ? i run the code with flamerobin. please help.
Edit: I get following error:
-104 Invalid token Engine Code : 335544569 Engine Message : Dynamic SQL Error SQL error code = -104 Token unknown - line 2, column 9
SELECT
create view emp_attd_dtls
as
select
emp.emp_id,
t.empname,
attend_dateout,
attend_datein,
attend_timein,
attend_timeout
from employee emp
inner join emp_attend t on emp.emp_id =t.emp_id

Execute SQL-functions in R, Microsoft SQL Server

I have a number of functions written on our Microsoft SQL servers.
I can easily access and query all data normally, but I cannot execute functions on the server using RODBC.
How can I execute sql-functions using R? Are there other packages that can do this?
Or do I need to switch strategies completely?
Example:
require(RODBC)
db <- odbcConnect("db")
df <- sqlQuery(channel = db, query = "USE [Prognosis]
GO
SELECT * FROM [dbo].[Functionname] ("information_variable")
GO" )
Error message:
"42000 102 [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near 'GO'."
[2] "[RODBC] ERROR: Could not SQLExecDirect 'USE... "
This turned out to work:
df <- sqlQuery(channel = db,
query = "SELECT * FROM [dbo].[Functionname] ("information_variable")" )
So I dropped USE [The_SQL_TABLE] and GO

Syntax Error In Query with selectRaw and time diff

please advice how to correct this query
$no_of_hours = DB::Table('shifts')
->where('time_sheet_id','=', $timesheet_id->id)
->selectRaw("SELECT time(sum(TIMEDIFF( 'shift_end_time', 'shift_start_time' )))")
->get();
return $no_of_hours;
im getting following error
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT time(sum(TIMEDIFF( 'shift_end_time', 'shift_start_time' ))) from `shifts`' at line 1 (SQL: select SELECT time(sum(TIMEDIFF( 'shift_end_time', 'shift_start_time' ))) from `shifts` where `time_sheet_id` = 35)
You have a sintax error probably because you don't have to write the SELECT keyword in the selectRaw function ( the keyword is added implicity by the query builder in this case ):
->selectRaw("time(sum(TIMEDIFF( 'shift_end_time', 'shift_start_time' )))")

Advantage Database 8.1 SQL IN clause

Using Advantage Database Server 8.1 I am having trouble executing a successful query. I am trying to do the following
SELECT * FROM Persons
WHERE LastName IN ('Hansen','Pettersen')
To check for multiple values in a column. But I get an error when I try to execute this query in Advantage.
Edit - Error
poQuery: Error 7200: AQE Error: State = 42000; NativeError = 2115; [iAnywhere Solutions][Advantage SQL Engine]Expected lexical element not found: ( There was a problem parsing the
WHERE clause in your SELECT statement. -- Location of error in the SQL statement is: 46
And here is the SQL i'm executing
select * from "Pat Visit" where
DIAG1 IN = ('43644', '43645', '43770', '43771', '43772', '43773', '43774',
'43842', '43843', '43845', '43846', '43847', '43848', '97804', '98961',
'98962', '99078')
Done
Does anyone have any Idea how I could do something similar in advantage that would be efficient as well?
Thanks
You have an extraneous = in the statement after the IN. It should be:
select * from "Pat Visit" where
DIAG1 IN ('43644', '43645', <snip> )