Error in list of function arguments: '=' not recognised. Unable to parse query in SSRS - sql

Hi i am trying to create a report in SSRS , and i am using below query for taking date difference and comparison.
IIF ((CAST(issues.created_at as smalldatetime)) = (CAST(sprints.created_at as smalldatetime)) OR (DATEDIFF(Minute,CAST(sprints.created_at as smalldatetime), CAST(issues.created_at as smalldatetime)) < 2),1,0) as new_task
but i am getting error "Error in list of function arguments: '=' not recognised. Unable to parse query"
is there anything i need to change in the code.

It seems that your version of SQL Server does not support the IIF (added in 2012).
Change it to a CASE statement which has the same functionality.
SELECT...,
CASE WHEN CAST(issues.created_at AS SMALLDATETIME) = CAST(sprints.created_at AS SMALLDATETIME)
OR DATEDIFF(MINUTE, CAST(sprints.created_at AS SMALLDATETIME), CAST(issues.created_at AS SMALLDATETIME)) < 2
THEN 1 ELSE 0 END AS new_task

Related

Unable to derive parameter information when parameter marker is a function argument

Inside Excel, under DATA > Properties
Then under definition > Command text
I have a SQL Command text to generate data table, but I've been getting this error when I use the following query string:
SELECT convert(datetime, (?) , 121)
The (?) is the parameter that I've set my cell to be =TEXT(BE3,"mm/dd/yyyy") which takes in datetime value as my datatype for that column in SQL Server is datetime
My full sql command is as of following:
SELECT * FROM Student
WHERE GraduationDate
BETWEEN
(CASE
WHEN (GraduationDate Is Not Null)
THEN SELECT convert(datetime, (?) , 121)
ELSE''
END)
AND
(CASE
WHEN (GraduationDate Is Not Null)
THEN SELECT convert(datetime, (?) , 121)
ELSE''
END)
Any idea what is causing this to happen?
Your syntax is not correct. If nothing else, subqueries need their own parentheses.
You seem to want to ignore null graduation dates. Well, this logic does that:
SELECT s.*
FROM Student
WHERE GraduationDate BETWEEN convert(datetime, (?), 121) AND convert(datetime, (?), 121);
Notes:
I would discourage you from using BETWEEN with date/time values. Aaron Bertrand has a good explanation of why.
If you are passing parameters in, they should already be date/time values, so no conversion is necessary.
If you want to include NULL values, then add OR GraduationDate IS NULL.

Conversion failed when converting date and/or time from character string in a view

I have a datetime column TAR_DATE in a view with date in format yyyy-mm-dd hh:mi:ss.mmm which I read is a standard format for ODBC canonical.
I need to compare this TAR_DATE column with another date:
select count(1)
from db_view
where (case
when isdate(TAR_DATE) = 1
then convert(datetime, TAR_DATE, 121)
end) <= convert(datetime, '2016-08-25', 121)
The above query is throwing an error:
Conversion failed when converting date and/or time from character string.
Whereas performing the conversions individually such as below are working fine:
select
(case
when isdate(TAR_DATE) = 1
then convert(datetime, TAR_DATE, 121)
end)
from
db_view
select convert(datetime, '2016-08-25', 121)
What is causing this?
Using try_convert() also isn't working. Does it have something to do with the data being in a view rather than a table?
The optimizer can evaluate a case statement in an unexpected order. It could decide to evaluate the then branch first and use its result only when the when condition is met. So surprisingly you can get an error for rows where you do not expect the then condition to be executed.
Using try_convert prevents this issue, and it's easier to read as well:
wehre try_convert(datetime, TAR_DATE, 121) <= '2016-08-25'
If the conversion fails, try_convert returns null instead of raising an error.
You should be using try_convert(). However, if the value is in ISO standard YYYY-MM-DD . . . format, then you don't even need to convert:
where tar_date < '2016-08-26'
The string comparison is sufficient.

CASE statement in DateDiff (Amazon Redshift)

I am trying to implement a CASE statement in a datediff function but it throws an ERROR: syntax error at or near "case"
Even when using a simplified query like the following, the error arises:
select datediff(CASE WHEN 1=1 THEN 'month' ELSE 'month' end,'2009-01-01','2009-12-31') as nummonths;
Is it not possible to include CASE statement in the function?
The first argument to datediff() in Redshift is a keyword. So, you cannot do what you want that simply.
Instead:
select (case when 1=1
then datediff(month, '2009-01-01', '2009-12-31')
else datediff(month, '2009-01-01', '2009-12-31')
end) as nummonths;
I assume this is oversimplified code, because obviously the case is not needed in any version of this query.

sql query to get last 6 months of data

I am trying to get the data for the last 6 months.
This is what I have used:
WHERE d_date > DATEADD(m, -6, current_timestamp)
and I am getting this error.
ERROR: CLI prepare error: SQL0206N "M" is not valid in the context where it is used
also tried
WHERE d_date > current date -180
and got this error:
ERROR: CLI prepare error: SQL0171N The data type, length or value of the argument for the parameter in
position "2" of routine "-" is incorrect. Parameter name: "". SQLSTATE=42815
Please advice.
Based on Andriy's eagle-eyes, here is (I think) the DB2 syntax:
WHERE d_date > current_date - 6 MONTHS
And here is a link to a pretty good function to mirror DATEADD in DB2.
Also, since you mentioned SAS, here is the SAS syntax to do the same thing:
WHERE d_date > intnx('MONTH', today(), -6, 'SAME');
Although you say you are running this with SAS Enterprise Guide, the syntax you show is not SAS. The error message you are getting suggests you are submitting "pass-thru" code directly to a database.
In DB2, it should be something like
WHERE TIMESTAMPDIFF(64, CAST(CURRENT_TIMESTAMP- d_date AS CHAR(22))) <= 6
Remove the SQL-Server tag, that's for MS SQLServer questions.
You can also try this
SELECT *
From PIS.dbo.PIS_tblEmployeeGenInfo
WHERE dtJoiningDate > DATEADD(m, -6, current_timestamp)
I'm using in code the following:
INSERT INTO Employee(I_EMP, T_CRT, C_SYS, D_HIRED)
VALUES (21,CURRENT TIMESTAMP, 10,CURRENT DATE - 6 MONTH);
This is in DB2, and it is working with no issues.
Hope that will help.

Please tell me what is error in my date comparison sql query

Please help me to find out error in my SQL query. I have created this query to compare dates
select * from Joinplans jp
where cast(convert(varchar,GETDATE(),103) AS datetime) BETWEEN
CASE(convert(varchar,jp.planstartDate,103) AS datetime) AND
CASE(convert(varchar,DATEADD(DAY,jp.planDays,jp.planstartDate),103) AS DATETIME)
It's giving me the error:
incorrect near 'AS'
I am using SQL Server 2005.
You wrote case instead of cast in two instances.
If planStartDate is actually a date, then there is no need to cast it to a character column:
Select ...
From Joinplans jp
where GetDate() Between planStartDate And DateAdd(day, jp.planDays, jp.planStartDate)
Now, if planStartDate is storing both date and time data, then you might want to use something like:
Select ...
From Joinplans jp
Where planStartDate <= GetDate()
And GetDate() < DateAdd(day, jp.planDays + 1, jp.planStartDate)
This ensures that all times on the last date calculated via the DateAdd function are included