Error while using SQL query with DATEADD condition in R - r

I am trying to extract data based on a date condition connecting to SQL from R.
My database connection is from Impala.
Below is my sample code.
dbGetQuery(src,"SELECT * FROM sample WHERE eventdate BETWEEN '2017-01-31' AND DATEADD(m,1,'2017-01-31')")
I get below error while trying to query.
Error in .verify.JDBC.result(r, "Unable to retrieve JDBC result set for ", :
Unable to retrieve JDBC result set for select * from sample where
eventdate between '2017-01-31' and dateadd(m,1,'2017-01-31') ([Cloudera]
[ImpalaJDBCDriver](500051) ERROR processing query/statement. Error Code: 0,
SQL state: TStatus(statusCode:ERROR_STATUS, sqlState:HY000,
errorMessage:AnalysisException: Could not resolve column/field reference: 'm'
), Query: select * from sample where eventdate between '2017-01-31' and dateadd(m,1,'2017-01-31').)
Instead of using DATEADD if I hard code between two dates I get the result e.g.
dbGetQuery(src,"SELECT * FROM sample WHERE eventdate BETWEEN '2017-01-31' AND '2017-02-28' LIMIT 5")
I get the result for above code but I want to use DATEADD in my code because I have multiple date conditions which I am doing using a loop function.
Any help regarding this please.

The problem is the format of your literal date string ('2017-01-31'). SQL Server, when passed a literal string for date for DATEADD will implicitly convert the value to a datetime. datetime will read the string in the format yyyy-dd-MM, translating the value to 20173101; you can see the problem there (there aren't 31 months in the year).
If you're using a literal string to pass a date(time) then use either the format yyyyMMdd or yyyy-MM-ddThh:mm:ss.sssssss as both are unambiguous regardless language and datatype.
So, for your value that would be:
WHERE eventdate BETWEEN '20170131' AND DATEADD(m,1,'20170131')
On a different note, are you really looking for rows between 20170131 and 20170228, inclusive of those dates (assuming eventdate is a date)?

remove time from your DATEADD function by using Convert function.
dbGetQuery(src,"SELECT * FROM sample WHERE eventdate BETWEEN '2017-01-31' AND convert(varchar,DATEADD(month, 1, '2017/08/25'),23)")

Related

insert statment make error

I am using oracle 12c with the username system. My problem is when I execute this insert statement that I took from oracle live sql site:
insert into emp
values(7788, 'SCOTT', 'ANALYST', 7566,to_date('13-JUL-87','dd-mm-rr') - 85,3000, null, 20);
it shows :
sql error ora-01858. 00000 - "a non-numeric character was found where a numeric was expected"
*Cause: The input data to be converted using a date format model was
incorrect. The input data did not contain a number where a number was
required by the format model.
*Action: Fix the input data or the date format model to make sure the
elements match in number and type. Then retry the operation.
what is this -85 after the to_date(..)
To handle dates, you would better use the ANSI format (date 'yyyy-mm-dd'):
insert into emp values(7788, 'SCOTT', 'ANALYST', 7566, date '1987-07-13'- 85,3000, null, 20);
If you need to use a to_date for some reason, you have to be sure that the format of your string exactly matches the format mask you use: if your month is written as 'JUL' you need 'MON' in the format mask and not 'mm'. 'mm' would match a month written as '07'.
Please notice that even with the right format mask, this way to write dates is dangerous, because it's based on the language of your DB.
The -85 means "subtract 85 days".

read table from SQL database based on a date

I'm trying to read a table from an SQL database using the sqlQuery function. Specifically, I want to ceate a function with a date as input and then select the data from the SQL database which match the given date. The commands are like:
example=function(dateA)
{sqlQuery(channel,paste("select * from TABLE","where date=dateA"))}
example('2017-10-26')
Note that the format of the date on the SQL table is YYYY-MM-DD and the above command works fine when the specific date is written on the sqlQuery function. i.e. when using the command:
sqlQuery(channel,paste("select * from TABLE","where date='2017-10-26'"))
nevertheless when calling the function example with date as input this does not work.
Does anybody know if there is a way to overcome this??
That is because dateA is treated as part of the string instead of replacing the value of dateA, try instead:
example=function(dateA){
sqlQuery(channel,paste0("select * from TABLE","where date=", dateA))
}
example('2017-10-26')
Here, dateA is replaced with '2017-10-26' when you call example, and paste0 pastes "select * from TABLE","where date=" and '2017-10-26' (the value of dateA) together to form "select * from TABLE","where date='2017-10-26'"

How can i get my table filled automatically when i gave id from another table in ax 2009

I have a two tables in first table i filled the values with name id,
on second table if i gave the id the table needs to fill the name automatically, how can i do this please help.
You commented that the error is in the following line:
axsl.TransDate = DateTimeUtil::utcNow();
This is logic because axsl.TransDate is Date and DateTimeUtil::utcNow() return a UtcDateTime when you compile get this error Operand types are not compatible with the operator.
There are many ways to fix this error.
Try this:
axsl.TransDate = DateTimeUtil::date(DateTimeUtil::utcNow())
DateTimeUtil::date() convert UtcDateTime in Date.
or you can use today() method to return the actual date.

Different dates in SSRS report with same calculation

So I've got what could be a very silly question, but for some reason my 'problem' isn't working.
It's quite simple really. One of the fields in a SSRS tablix is a due date calculated by using the SQL function DateAdd:
=DateAdd(DateInterval.Day, (Int(Fields!TMinus.Value) * 7), Parameters!StartDate.Value)
Where TMinus is a negative integer simulating weeks and StartDate being the date the activity started.
I'm calculating the same thing in VB.NET using this formula to set up the DueDate of an activity in a row cell:
Dim intTMinus As Integer = CInt(dataItem.GetDataKeyValue("TMinus").ToString)
CType(dataItem.FindControl("RlblDue"), RadLabel).Text = CDate(DateAdd(DateInterval.Day, (intTMinus * 7), dtStartDate)).ToString
The problem is that the SSRS report shows a DIFFERENT date than the Grid, even though I've used hardcoded values to attempt to find the culprit in the report.
This calculation in the report:
=DateAdd(DateInterval.Day, (Int(-40) * 7), '12/09/2016')
Shows the date: 07/12/2015 in the Grid, but 3/4/2016 on the Report
Note DateAdd arguments data types have to be DateInterval, Double, and DateTime respectively. You are passing a string '12/09/2016' for the third argument but it requires a DateTime. By the way, strings in SSRS must be surrounded with double quotes.
After fix the expression, it should be like this:
=DateAdd(DateInterval.Day, (Int(-40) * 7), CDATE("2016-09-12"))
Which returns: 07/12/2015 as your Grid in VB.
Note CDATE("2016-09-12") converts the date string in a DateTime value.
Check your parameter is set to Date/Time type.
REFERENCE
Let me know if this helps.

In Cognos 10.2.1 - how can I use substring portions of an input parameter to create an expression to use for date comparison?

In Cognos 10.2.1 (FP7), I have a situation with two tables (see below), where a given transaction may be in the same "PERIOD", but what I want to do is to only give me results for transactions that have a date after the 15th of the month (i.e - the transaction may be in PERIOD 201510, and I only want transactons on/after 10/15/2015). The PERIOD is an input parameter that the user selects - I want to build the date from a portion of the PERIOD. (Before anyone complains about syntax, etc. - I've tried to simplify this as much as possible).
Given Table A:
ID varchar
PERIOD varchar
Given Table B:
ID varchar
TYPE varchar
TRANDATE timestamp2
Query1
[A.PERIOD]=?PARM1? <- this is the user selected input parameter
Query2
[B.TYPE] in ('A','B')
Then create a join from the results of Query1 and Query2 on the A.ID=B.ID to give Results1
I've tried the following (I'm keeping this as simple as I can - so I'm ONLY working with the YEAR portion of the period):
[Results1].[TRANDATE] >= concat(substring([Results1].[PERIOD],1,4),'-10-15T00:00:00.000000000')
[Results1].[TRANDATE] >= cast(concat(substring([Results1].[PERIOD],1,4),'-10-15T00:00:00.000000000'), timestamp2)
In both cases - Cognos won't validate the expression, or, if it does validate, I get a runtime error when running the report. In both cases, I get messages basically "literal does not match format string", even with the cast.
So - how can I get pieces/portions of the parameter, and slice/dice them to use as a date comparison as I mentioned above?
Given: Column [PERIOD] of type integer in the form YYYYMM, [TRANDATE] of type date/time
[TRANDATE] >= cast(cast(cast([PERIOD]/100,integer),varchar(4)) || '-' || cast(mod([PERIOD],100),varchar(2)) || '-15',date)
Given: Column [PERIOD] of type string in the form 'YYYYMM', [TRANDATE] of type date/time
[TRANDATE] >= cast(substring([PERIOD],1,4) || '-' || substring([PERIOD],5,2) || '-15',date)
The trick is that you don't have to build a full date/time string. You only have to build a string of format 'YYYY-MM-DD' in order to cast to a date type.

Resources