ORA-00936: Missing Expression Error Code - sql

New here and was wondering if anyone can shed some light into this ORA-00936 error with my SQL Query.
select Count(Auftragsnr) as Open_Orders
from wa_kopf
where convert(varchar(4), Aender_Datum, 104) = convert(varchar(4), getdate(), 104)
and zustand < (60);
Get the following error:
14:47:37 [SELECT - 0 row(s), 0.000 secs] [Error Code: 936, SQL
State: 42000] ORA-00936: Missing Expression
Any help would be greatly appreciated.
Regards
JRidge

You're using SQL Server syntax, which won't work with an Oracle database. You seem to be trying to format a date as dd.mm.yyyy (but in a 4-character string, somehow?). There is no getdate function in Oracle either, but it does have sysdate instead.
So the Oracle equivalent for what I think you're trying to do would be:
where to_char(Aender_Datum, 'dd.mm.yyyy') = to_char(sysdate, 'dd.mm.yyyy')
But that would just mean you're comparing dates as string, which isn't generally a good idea. If you're looking for any value from today you could use the trunc() function, which strips the time portion from a date by default; if your column is only representing a date and the time is always midnight then you can do:
where Aender_Datum = trunc(sysdate)
If your column has varying times then you could truncate that as well:
where trunc(Aender_Datum) = trunc(sysdate)
... but that can have a performance impact if the column is indexed, and an alternative is to use a range comparison:
where Aender_Datum >= trunc(sysdate)
and Aender_Datum < trunc(sysdate) + 1

Related

Case statement with date and string

I have the following error, ORA-01841
(full) year must be between -4713 and +9999 and not be 0
The error is coming from the below case statement.
Any help on what's going on and how to fix?
SQL
SELECT
CASE
WHEN NVL(uap.us_pend_dt, act_d_dt) >= TO_CHAR(TO_DATE('".PENDING_DATE_CUTOFF."','YYYY-MM-DD'),'mm/dd/yyyy')
THEN NVL(uap.us_pend_dt, act_d_dt)
ELSE CASE WHEN NVL(act_d_dt, SYSDATE) <TO_CHAR(TO_DATE('".HIRE_DATE_CUTOFF."','YYYY-MM-DD'),'mm/dd/yyyy')
THEN act_d_dt
ELSE ua_dt
END
END AS h_DT
It looks like you are trying to convert a string ".PENDING_DATE_CUTOFF." to a date. I just did the following and got an identical error:
SELECT TO_DATE('".PENDING_DATE_CUTOFF."', 'YYYY-MM-DD') FROM dual;
Is .PENDING_DATE_CUTOFF. (with periods) the name of a column in your table? If so then omit the single quote characters, e.g.:
SELECT TO_DATE(".PENDING_DATE_CUTOFF.", 'YYYY-MM-DD') FROM dual;
Of course this will give an altogether different error [ORA-00904: ".PENDING_DATE_CUTOFF.": invalid identifier] if you run as is! So I think you might want something like the following (I'm assuming the other date columns are actual dates, and the cutoff columns are VARCHAR2 columns that are storing dates in the format YYYY-MM-DD:
SELECT CASE WHEN COALESCE(uap.us_pend_dt, act_d_dt) >= TO_DATE(".PENDING_DATE_CUTOFF.", 'YYYY-MM-DD') THEN COALESCE(uap.us_pend_dt, act_d_dt)
ELSE WHEN COALESCE(act_d_dt, SYSDATE) < (TO_DATE(".HIRE_DATE_CUTOFF.", 'YYYY-MM-DD') THEN act_d_dt
ELSE act_d_dt
END AS h_dT
FROM mytable;
Note that I also got rid of the extraneous CASE statement and converted the Oracle-specific NVL() function to the ANSI-standard COALESCE() function.
EDIT: In the event that your *_dt columns are strings and not dates, best to convert them to dates using TO_DATE() before comparing - that way you're comparing dates to dates.
Hope this helps.
Presumably, your date columns are stored as dates. So, do the comparison as dates not strings:
SELECT (CASE WHEN NVL(uap.us_pend_dt, act_d_dt) >= TO_DATE('".PENDING_DATE_CUTOFF."', 'YYYY-MM-DD')
THEN NVL(uap.us_pend_dt, act_d_dt)
WHEN NVL(act_d_dt, SYSDATE) < TO_DATE('".HIRE_DATE_CUTOFF."', 'YYYY-MM-DD')
THEN act_d_dt
ELSE ua_dt
END) AS h_DT
You also don't need the extra case expressions.

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.

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.

Oracle to SQL server Date conversion

I would like to convert an Oracle SQL query into SQL server query.
But I encountered a problem with the following line :
AND to_date(to_char(M_DATE,'DD-MM-YYYY')) = '27/01/12'
M_DATE : DATE NOT NULL
I use
to_char(DATE,'DD-MM-YYYY')
in order to get their data like that : DD-MM-YYYY 00:00:00.000 (data are stocked like : 25/02/12 15:32:06.578)
So I searched on the Internet, but I didn't find any available solution. But I'm not an experienced SQL user, so if anybody know the solution..
Thanks
In general when removing any time values from a date I would use Date functions rather than converting to string
DATEADD(DAY, 0, DATEDIFF(DAY, 0, GETDATE()))
instead of
CONVERT(VARCHAR, GETDATE(), 103)
Although the end result is the same you are maintaining date format and while I have no specific results sets to prove it conclusively I have found this to be much quicker when dealing with large quantities of data.
In Oracle, I would remove the time element of a datetime using trunc - like so:
AND trunc(M_DATE) = ...
In SQLServer, I would convert to a date - like so:
AND convert(date,M_DATE) = ...
SELECT CONVERT(VARCHAR(25), GETDATE(), 131)
You could just do:
AND convert(varchar(8), M_DATE, 3) = '27/01/12'
Of course, that won't work if you have dates from other centuries.
I'm not sure what you mean by "data are stocked like"; be aware that the Microsoft SQL Server DATE type only has a precision of one day. If you want to have the time as well as the day, you should use the DATETIME2 type

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