Right parenthesis error in oracle SQL Developer - sql

SELECT titol, data_prestec
FROM if_llibres, if_llibre_prestec
WHERE
if_llibre_prestec.ubicacio = if_llibres.ubicacio
AND data_devolucio IS NULL
AND data_prestec <= date_sub(current_date(),interval 30 day);

There is no DATE_SUB() function in Oracle. You could phrase this as:
select
titol,
data_prestec
from if_llibres il
inner join if_llibre_prestec ilp
on ilp.ubicacio = il.ubicacio
where
data_devolucio is null
and data_prestec <= current_date - interval '30' day;
Note that I rewrote your query to use a standard, explicit join (with the on keyword) rather than an implicit join (with a comma in the from clause): this old syntax from decades ago should not be used in new code.
I would also recommend prefixing each column in the SELECT and WHERE clause with the (alias of the) table it belongs to: this makes the query unambiguous and easier to understand.

Subtracting 30 days smells like "previous month". If that was your intention, well, not all months have 30 days so you'll get wrong result. Note that Oracle offers the ADD_MONTHS function which allows you to subtract any number of months. In your case - 1 month.
Furthermore, current_date returns both date and time, so of you subtract 1 month (or 30 days, doesn't matter), you'll move back to that very second, not date itself.
Here's what I mean:
SQL> alter session set nls_Date_format = 'dd.mm.yyyy hh24:mi:ss';
Session altered.
SQL> select current_date as curdat,
2 current_date - interval '30' day as cur_30,
3 add_months(current_date, -1) as adm_1,
4 --
5 trunc(current_date) - interval '30' day as cur_trun_30,
6 add_months(trunc(current_date), -1) as adm_trun_1
7 from dual;
CURDAT CUR_30 ADM_1 CUR_TRUN_30 ADM_TRUN_1
------------------- ------------------- ------------------- ------------------- -------------------
14.06.2020 21:49:43 15.05.2020 21:49:43 14.05.2020 21:49:43 15.05.2020 00:00:00 14.05.2020 00:00:00
SQL>
CURDAT is current date (along with time)
CUR_30 subtracted 30 days; as you can see, you aren't on 14.05. but 15.05. at 21:49:43 so your query will return rows whose date value depends on that time as well
ADM_1 subtracted 1 month from current date, and it brought you back to 14.05. - again with the time component
CUR_TRUN_30 truncated current date (and "removed" the time component, setting time to midnight at beginning of that day), but you're again at 15.05. - not 14.05.
ADM_TRUN_1 truncated current date and set you to 14.05.2020 00:00 which is - I believe - what you wanted
Therefore, my suggestion would be
SELECT titol,
data_prestec
FROM if_llibres join if_llibre_prestec ON if_llibre_prestec.ubicacio = if_llibres.ubicacio
WHERE data_devolucio IS NULL
AND data_prestec <= ADD_MONTHS(TRUNC(current_date), -1);
Not related to your problems, but I'd suggest you to always use table aliases. The way you wrote the query, it is impossible to know which column belongs to which table and it makes confusion.

Related

How can I truncate a timestamp to make readjustments to a date in Oracle?

I have the following query in my SELECT from Oracle:
CASE
WHEN to_char(ola.promise_date, 'D') = 2 THEN (ola.promise_date - 4)
WHEN to_char(ola.promise_date, 'D') = 3 THEN (ola.promise_date - 4)
WHEN to_char(ola.promise_date, 'D') > 3 AND to_char(ola.promise_date, 'D') < 7 THEN (ola.promise_date - 2)
WHEN to_char(ola.promise_date, 'D') = 7 THEN (ola.promise_date - 2)
WHEN to_char(ola.promise_date, 'D') = 1 THEN (ola.promise_date - 2)
END mod_Promise_date
"ola.promise_date" is a column of type TIMESTAMP, and I use the CASE statement in order to adjust the date, considering the day of the week of the original promise date (because of internal considerations of shipment, among other things)
So, for example, something that has a promise date of 2021-11-07 00:00:00.0000000 will have a modified promise date of 2021-11-05 00:00:00.0000000 (considering Sunday as the seventh day)
Now, I run this query with a program made with C#, there I have a date picker in order to select specific promise dates, so the user can choose Sunday, November 7, 2021 and it will run the query as said before, this works good with the exception of promise dates that are something like 2021-11-07 23:59:00.0000000 because the query will consider the date as 8th of November instead of 7th, and this is not expected behavior, because the hour, minutes, seconds and fractional seconds are not really needed in this specific instance of the project.
Is there a way to ignore the hour, minutes, seconds and fractional seconds in the CASE-WHEN statement? Or floor down the date to consider everything in the same day as the day specified, without consideration of the time.
I'm pretty new using Oracle, so sorry if I'm not clear enough or if the query doesn't look viable.
You can use TRUNC(datetime_value) to truncate a DATE or a TIMESTAMP data type and set the time component to midnight:
CASE TRUNC(ola.promise_date) - TRUNC(ola.promise_date, 'IW')
WHEN 0 /* Monday */ THEN TRUNC(ola.promise_date) - 2
WHEN 1 /* Tuesday */ THEN TRUNC(ola.promise_date) - 4
WHEN 2 /* Wednesday */ THEN TRUNC(ola.promise_date) - 4
WHEN 6 /* Sunday */ THEN TRUNC(ola.promise_date) - 2
ELSE TRUNC(ola.promise_date) - 2
END mod_Promise_date
Note: TO_CHAR(datevalue, 'D') will give different values depending on where you are in the world as different territories consider the week to start on different days-of-the-week. If you want a territory-agnostic method of determining the day-of-the-week then you can find the difference from the start of the ISO week using TRUNC(datetime_value) - TRUNC(datetime_value, 'IW').
If I understood you correctly, you'll just have to truncate that value and "remove" time component (i.e. reset it to midnight).
For example:
(just setting format; you don't have to do that)
SQL> alter session set nls_timestamp_format = 'dd.mm.yyyy hh24:mi:ss.ff6';
Session altered.
Query that shows the result:
SQL> select systimestamp,
2 trunc(systimestamp) result
3 from dual;
SYSTIMESTAMP RESULT
----------------------------------- -------------------
05.11.21 19:29:41,207000 +01:00 05.11.2021 00:00:00
SQL>
You'd have
... THEN (trunc(ola.promise_date) - 4)

Sorting SQL Query By Date Column

I'm working on a project with a db that contains a date column for patient visits in the format of %m-%d-yyyy and need to sort so that it only pulls the rows where that date is within the last two weeks. I've tried a few different functions of convert, to_date, and can't seem to get anything to work.
I'm still very new to SQL and I don't know if this is a special case because I'm working with an oracle db
Not the full code, because it has dozens of queries and multiple joins (would that affect the date syntax?) but this is the format I'm trying for...
create table "Visits"
insert into "Visits" (
'John Doe',
'5/24/2021',
'Story about the visit',
'More room for story if needed')
select
"User_Name",
"Visit_Date",
"Visit_Narrative",
"Visit_Narrative_Overflow"
from "Visits"
where "Visits"."Visit_Date" >= TRUNC(SYSDATE) - 14
I'm working on a project with a db that contains a date column for patient visits in the format of %m-%d-yyyy
No, you don't have it in the format mm.dd.yyyy. A DATE data type value is stored within the database as a binary value in 7-bytes (representing century, year-of-century, month, day, hour, minute and second) and this has no format.
need to sort so that it only pulls the rows where that date is within the last two weeks.
You want a WHERE filter:
If you want to have the values that happened in the last 14 days then TRUNCate the current date back to midnight and subtract 14 days:
SELECT visit_date
FROM patient
WHERE visit_date >= TRUNC(SYSDATE) - INTERVAL '14' DAY
or
SELECT visit_date
FROM patient
WHERE visit_date >= TRUNC(SYSDATE) - 14
(or subtract 13 if you want today and 13 days before today.)
If you want it after Monday of last week then TRUNCate to the start of the ISO Week (which is always a Monday) and subtract 7 days:
SELECT visit_date
FROM patient
WHERE visit_date >= TRUNC(SYSDATE, 'IW') - INTERVAL '7' DAY
I ended up figuring it out based on an answer from another forum (linked below);
it appears that my original to_date() was incomplete without a second and operator in my where clause. This code is working perfectly
select
"User_Name",
"Visit_Date",
"Visit_Narrative",
"Visit_Narrative_Overflow"
from "SQLUser"."Visits"
where "SQLUser"."Visits"."Visit_Date" >= to_date('5/10/2021', 'MM/DD/YYYY')
and "SQLUser"."Visits"."Visit_Date" < to_date('5/24/2021', 'MM/DD/YYYY')

What is the difference between these 2 sql queries?

select * from accounts where sysdate - 100 < last_active_dt;
select * from accounts where sysdate - 100 <= last_active_dt;
Both of the queries returned the same result.
I was thinking that for the first query, it would not select
records on the day of last_active_dt. For the 2nd query, it
will select records on the day of last_active_dt.
But this does not seem to be the case as both returned the same results.
Is there any difference to it? And how can I see the difference?
last_active_dt column follows a date format DD-MON-YYYY
last_active_dt column follows a date format DD-MON-YYYY
No, it doesn't (but it may look like it does in the user interface you are using).
A DATE data type always has year, month, day, hour, minute and second components and is stored (without any formatting) in 7 bytes. It is the user interface that applies a (default) format to the date and this may hide the time component from you.
You can use:
ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS';
To change the default date format used in SQL*Plus (and SQL Developer) to see the date with all the components.
Equally, SYSDATE is of a DATE data type and has year, month, day, hour, minute and second components so what you are probably intending is:
select * from accounts where TRUNC( sysdate ) - INTERVAL '100' DAY < last_active_dt;
This will get all the columns where the last_active_dt is after midnight of the day 100 days ago (even if it 1 second after). This will work if all your last_active_dt values have the times TRUNCated to exactly midnight 00:00:00 but not if the column as non-zero time components as you will still get values with times from 00:00:01 to 23:59:59 of the day 100 days ago.
If your last_active_dt has a non-zero time component then you want:
select * from accounts where TRUNC( sysdate ) - INTERVAL '99' DAY <= last_active_dt;

Presto SQL get yyyymm minus 2 months

I am using Presto. I have an integer column (let's call the column 'mnth_nbr') showing year and month as: yyyymm. For instance, 201901. I want to have records showing all dates AFTER 201901 as well as 2 months before the given date. In this example, it would return 201811, 201812, 201901, 201902, 201903, etc. Keep in mind that my data type here is integer.
This is what I have so far (I do a self join):
select ...
from table 1 as first_table
left join table 1 as second_table
on first_table.mnth_nbr = second_table.mnth_nbr
where first_table.mnth_nbr <= second_table.mnth_nbr
I know this gives me all dates AFTER 201901, including 201901. But, I don't know how to add the 2 previous months (201811 and 201812)as explained above.
As far as the documentation, Presto DB date_parse function expects a MySQL-like date format specifier.
So the proper condition for your use case should be :
SELECT ...
FROM mytable t
WHERE
date_parse(cast(t.mnth_nbr as varchar), '%Y%m') >= date '2019-01-01' - interval '2' month
Edit
As commented by Piotr, a more optimized expression (index-friendly) would be :
WHERE
mnth_nbr >= date_format(date '2019-01-01' - interval '2', '%Y%m')
Something like this would help. first parse your int to date
date_parse(cast(first_table.mnth_nbr as varchar), 'yyyymm') > date '2019-01-01' - interval '2' month
please keep in mind that you may encounter with indexing issues with this approach.

Choose active employes per month with dates formatted dd/mm/yyyy

I'm having a hard time explaining this through writing, so please be patient.
I'm making this project in which I have to choose a month and a year to know all the active employees during that month of the year.. but in my database I'm storing the dates when they started and when they finished in dd/mm/yyyy format.
So if I have an employee who worked for 4 months eg. from 01/01/2013 to 01/05/2013 I'll have him in four months. I'd need to make him appear 4 tables(one for every active month) with the other employees that are active during those months. In this case those will be: January, February, March and April of 2013.
The problem is I have no idea how to make a query here or php processing to achieve this.
All I can think is something like (I'd run this query for every month, passing the year and month as argument)
pg_query= "SELECT employee_name FROM employees
WHERE month_and_year between start_date AND finish_date"
But that can't be done, mainly because month_and_year must be a column not a variable.
Ideas anyone?
UPDATE
Yes, I'm very sorry that I forgot to say I was using DATE as data type.
The easiest solution I found was to use EXTRACT
select * from employees where extract (year FROM start_date)>='2013'
AND extract (month FROM start_date)='06' AND extract (month FROM finish_date)<='07'
This gives me all records from june of 2013 you sure can substite the literal variables for any variable of your preference
There is no need to create a range to make an overlap:
select to_char(d, 'YYYY-MM') as "Month", e.name
from
(
select generate_series(
'2013-01-01'::date, '2013-05-01', '1 month'
)::date
) s(d)
inner join
employee e on
date_trunc('month', e.start_date)::date <= s.d
and coalesce(e.finish_date, 'infinity') > s.d
order by 1, 2
SQL Fiddle
If you want the months with no active employees to show then change the inner for a left join
Erwin, about your comment:
the second expression would have to be coalesce(e.finish_date, 'infinity') >= s.d
Notice the requirement:
So if I have an employee who worked for 4 months eg. from 01/01/2013 to 01/05/2013 I'll have him in four months
From that I understand that the last active day is indeed the previous day from finish.
If I use your "fix" I will include employee f in month 05 from my example. He finished in 2013-05-01:
('f', '2013-04-17', '2013-05-01'),
SQL Fiddle with your fix
Assuming that you really are not storing dates as character strings, but are only outputting them that way, then you can do:
SELECT employee_name
FROM employees
WHERE start_date <= <last date of month> and
(finish_date >= <first date of month> or finish_date is null)
If you are storing them in this format, then you can do some fiddling with years and months.
This version turns the "dates" into strings of the form "YYYYMM". Just express the month you want like this and you can do the comparison:
select employee_name
from employees e
where right(start_date, 4)||substr(start_date, 4, 2) <= 'YYYYMM' and
(right(finish_date, 4)||substr(finish_date, 4, 2) >= 'YYYYMM' or finish_date is null)
NOTE: the expression 'YYYYMM' is meant to be the month/year you are looking for.
First, you can generate multiple date intervals easily with generate_series(). To get lower and upper bound add an interval of 1 month to the start:
SELECT g::date AS d_lower
, (g + interval '1 month')::date AS d_upper
FROM generate_series('2013-01-01'::date, '2013-04-01', '1 month') g;
Produces:
d_lower | d_upper
------------+------------
2013-01-01 | 2013-02-01
2013-02-01 | 2013-03-01
2013-03-01 | 2013-04-01
2013-04-01 | 2013-05-01
The upper border of the time range is the first of the next month. This is on purpose, since we are going to use the standard SQL OVERLAPS operator further down. Quoting the manual at said location:
Each time period is considered to represent the half-open interval
start <= time < end [...]
Next, you use a LEFT [OUTER] JOIN to connect employees to these date ranges:
SELECT to_char(m.d_lower, 'YYYY-MM') AS month_and_year, e.*
FROM (
SELECT g::date AS d_lower
, (g + interval '1 month')::date AS d_upper
FROM generate_series('2013-01-01'::date, '2013-04-01', '1 month') g
) m
LEFT JOIN employees e ON (m.d_lower, m.d_upper)
OVERLAPS (e.start_date, COALESCE(e.finish_date, 'infinity'))
ORDER BY 1;
The LEFT JOIN includes date ranges even if no matching employees are found.
Use COALESCE(e.finish_date, 'infinity')) for employees without a finish_date. They are considered to be still employed. Or maybe use current_date in place of infinity.
Use to_char() to get a nicely formatted month_and_year value.
You can easily select any columns you need from employees. In my example I take all columns with e.*.
The 1 in ORDER BY 1 is a positional parameter to simplify the code. Orders by the first column month_and_year.
To make this fast, create an multi-column index on these expressions. Like
CREATE INDEX employees_start_finish_idx
ON employees (start_date, COALESCE(finish_date, 'infinity') DESC);
Note the descending order on the second index-column.
If you should have committed the folly of storing temporal data as string types (text or varchar) with the pattern 'DD/MM/YYYY' instead of date or timestamp or timestamptz, convert the string to date with to_date(). Example:
SELECT to_date('01/03/2013'::text, 'DD/MM/YYYY')
Change the last line of the query to:
...
OVERLAPS (to_date(e.start_date, 'DD/MM/YYYY')
,COALESCE(to_date(e.finish_date, 'DD/MM/YYYY'), 'infinity'))
You can even have a functional index like that. But really, you should use a date or timestamp column.