I was working on Oracle APEX writing a query which gives a list of the upcoming birthdays of students in 2016 and orders them chronologically. This worked and I used the following code:
SELECT first_name, last_name, to_date(concat(to_char(date_of_birth, 'dd-mm'), '-2016'), 'dd-mm-yyyy') AS birthday
FROM students
WHERE date_of_birth IS NOT NULL
ORDER BY birthday
This gave the right output and started showing names with the chronologically ordered birthdays in 2016.
However, I now wanted to get another column which shows on which day students celebrate their birthday in class. So for students with a birthday during a week day that would be the same day, but for students with a birthday during the weekend that would be Monday, using a case statement to change 'saturday' or 'sunday' to 'monday'.
I already made a query that shows on which day the birthday of a student is celebrated, which gives proper output like 'friday' or 'wednesday', the code is:
SELECT first_name, last_name, to_date(concat(to_char(date_of_birth, 'dd-mm'), '-2016'), 'dd-mm-yyyy') AS birthday, to_char(to_date(concat(to_char(date_of_birth, 'dd-mm'), '-2016'), 'dd-mm-yyyy'), 'day') AS celebrationday
FROM students
WHERE date_of_birth IS NOT NULL
ORDER BY birthday
I had already done some simple tests with case statement, like adding something when the first name is 'John', like this:
SELECT student_number, first_name,
(CASE first_name
WHEN 'John' THEN 'Check'
END) addition
FROM Students
Which resulted in correct output; for all names other than 'John' the column addition would be null and those with the name would have 'Check' in the column addition.
Now, the main problem starts than when I try to replicate this in order to check whether celebrationday is in the weekend I either get the following error: 'ORA-00904 'celebrationday': invalid identifier', for this piece of code:
SELECT first_name, last_name, to_date(concat(to_char(date_of_birth, 'dd-mm'), '-2016'), 'dd-mm-yyyy') AS birthday, to_char(to_date(concat(to_char(date_of_birth, 'dd-mm'), '-2016'), 'dd-mm-yyyy'), 'day') AS celebrationday,
(CASE celebrationday
WHEN 'saturday' THEN 'monday'
WHEN 'sunday' THEN 'monday'
END) addition
FROM students
WHERE date_of_birth IS NOT NULL
ORDER BY birthday
(Little commentary, I know the case should also contain the other days of the week, but at first I'm interested in getting a proper output from the weekend days)
Or for the following piece of code I just don't get any results in the column addition, just null values.
SELECT first_name, last_name, to_date(concat(to_char(date_of_birth, 'dd-mm'), '-2016'), 'dd-mm-yyyy') AS birthday,
CASE to_char(to_date(concat(to_char(date_of_birth, 'dd-mm'), '-2016'), 'dd-mm-yyyy'), 'day)
WHEN 'saturday' THEN 'monday'
WHEN 'sunday' THEN 'monday'
END) addition
FROM Students
WHERE date_of_birth IS NOT NULL
ORDER BY date_of_birth
- What do I need to do in order to get the case statement working so that I can change celebrationday ? I hope the code examples provided are clear enough to work with and give you an idea of what is and isn't working.