2

I have an existing large enterprise application that we are converting the database from MySQL to Oracle for various reasons. We've got hundreds of current MySQL commands working inside Oracle without much hassle, except for one. The following SQL executes on MySQL without any issues:

SELECT `user_id`, `shift_type`, `date`, COUNT(*) as c
FROM `staff`
WHERE `date` = '2020-02-02' AND `shift_type` != 'oncall'
GROUP BY user_id, shift_type, date
HAVING c > 1

This executes and gives us the results of any staff who exist more than once for a given date with the criteria above. But running that same SQL query in Oracle, with the apostrophes removed (as required by Oracle), we get a Error Message : ORA-00936: missing expression. This is the exact Oracle SQL I tried:

SELECT user_id, shift_type, date, COUNT(*) as c
FROM staff
WHERE date = '2020-02-02' AND shift_type != 'oncall'
GROUP BY user_id, shift_type, date
HAVING c > 1

I've checked lots of other Stack Overflow answers about this error code. Most refer to removing an extra comma. I've tried that on various parts above, but nothing seems to work. Really hoping someone here is able to help point me in the right direction.

2
  • 3
    "with the apostrophes removed (as required by Oracle)" - as required by the SQL standard, this is not Oracle specific. The dreaded backticks are simply invalid standard SQL. Commented Aug 28, 2021 at 9:07
  • thanks @a_horse_with_no_name - the backticks come from the ORM we are using, but good to know. The ORM does not seem to generate the backticks in the Oracle query, so thats an improvement :) Commented Aug 28, 2021 at 9:11

1 Answer 1

4
+500

Proper syntax - "date" instead of date and HAVING COUNT(*)>1:

SELECT user_id, shift_type, "date", COUNT(*) as c
FROM staff
WHERE "date" = '2020-02-02' 
  AND shift_type != 'oncall'
GROUP BY user_id, shift_type, "date"
HAVING COUNT(*) > 1;

db<>fiddle.com demo


ORA-00936: missing expression

Oracle expects date literal here, thus error:

SELECT date '2020-01-01'
FROM dual

Also using keywords as identifiers is not the best practice.

Sign up to request clarification or add additional context in comments.

3 Comments

It probably needs to be "DATE" as Oracle stores identifiers as uppercase by default.
Thanks @Lukasz Szozda - that works. I agree "date" is not best practice, its a legacy application, so I dont think I can change it now. But we'll keep it in mind. I'll flick you the bounty when SO allows me. cheers.
DATE is a keyword in (all) SQL dialects and namely in ISO SQL. This can easily stop working in future MySQL release too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.