Question

I am trying to find out average of electricity volume of certain day in a week with the following query:

SELECT avg(volume)
FROM v_nem_rm16
WHERE to_char(day, 'day') = 'monday';

where the v_nem_rm16 is a table and volume, day are its columns and my query is returning null whatever I change the day value 'monday', 'tuesday',....

is this query wrong?

Was it helpful?

Solution

Actually 'DAY' is returned with padding spaces on the right side. If you use 'RTRIM' then you can avoid the null values.

SELECT avg(volume)
FROM v_nem_rm16
WHERE RTRIM(to_char(day, 'day')) = 'monday';

OTHER TIPS

I would rather use different date format to_char DAY is nls-dependent that is bad (for instance your software will fail in Spain). D returns number so in your case the query should look like

SELECT avg(volume)
FROM v_nem_rm16
WHERE RTRIM(to_char(day, 'd')) = 1;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top