Click here to Skip to main content
15,902,275 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
syntax error near str_to_date(date_format(sysdate(),'%d-%b-%Y'),'%d-%b-%Y')-max(ren_date)>=7


what is correct syntax?

What I have tried:

CREATE OR REPLACE VIEW HWP_LASTRENEWED (REGNO, WPNO, PERMITSTATUS, PROCESSSTATUS, LASTRENDATE, DAYS, HTYPE, SRNO) AS
(
select regno, wpno, permitstatus, processstatus, max(ren_date) as lastrendate,
str_to_date(date_format(sysdate(),'%d-%b-%Y'),'%d-%b-%Y')-max(ren_date) days , 'renw' as htype, '0' as srno
from hwp_renewal
having permitstatus='RENEWED'
and str_to_date(date_format(sysdate(),'%d-%b-%Y'),'%d-%b-%Y')-max(ren_date)>=7
group by wpno, permitstatus, regno, processstatus
union
select reg_no as regno, wp_no as wpno, permitstatus, processstatus, permitdate as lastrendate,
(str_to_date(date_format(sysdate(),'%d-%b-%Y'),'%d-%b-%Y')-permitdate) as days, 'req' as htype, srno
from hwp_hotwork where wp_no is null and reg_no is null and
( (permitstatus='REQUESTED' and processstatus='REQ') or (permitstatus='APPROVED' and processstatus='APPR') )
and str_to_date(date_format(sysdate(),'%d-%b-%Y'),'%d-%b-%Y')-permitdate >=7
);
Posted
Updated 8-Jul-18 19:34pm
Comments
Bryian Tan 9-Jul-18 1:36am    
look fine beside the group by ... having .. clause in wrong order.
Darsh12345 9-Jul-18 2:06am    
still showing error

1 solution

There are many issues with your query.
I will explain some and this may improve your query

SQL
    -- What do you expect from this statement? I am not sure. Are you subtracting two date with max date?
  str_to_date(date_format(sysdate(),'%d-%b-%Y'),'%d-%b-%Y') - max(ren_date) days 
-- few issue I will address from this statement
-- You are generating todays date by 
  SELECT str_to_date(date_format(sysdate(),'%d-%b-%Y'),'%d-%b-%Y'); 
--  IMPROVED version
  SELECT DATE(NOW());
-- Second issue subtraction:
-- you are running something like this
  SELECT "2018-07-09"-"2018-07-02"; 
-- Improved version 
  SELECT DATEDIFF("2018-07-09", "2018-07-02"); 
-- next issue max command, looks okay but I will still suggest to split up the query
-- examle
   SELECT
      a
    , b
    , '0'  as zero
    , 'd'  as d
   FROM (
     SELECT 
         aa as a
       , MAX(bb) as b
     FROM
      table_tbl
     GROUP BY a
     HAVING b > 7

   )



If you have any further question, please let me know.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900