What does Oracle SYSDATE - 1/8 mean? - sql

I am looking at a query:
select JOB_ID from db where last_updated_date >= sysdate - 1/8
I went through the ORACLE SYSDATE documentation but couldn't understand what sysdate - 1/8 means.
Please clarify.

In Oracle, it means you're subtracting 1/8 of the whole day. As one day has 24 hours, its 1/8th part is 24/8 = 3 hours. So:
SQL> alter session set nls_date_format = 'dd.mm.yyyy hh24:mi:ss';
Session altered.
SQL> select sysdate col1,
2 sysdate - 1/8 col2
3 from dual;
COL1 COL2
------------------- -------------------
29.10.2021 22:18:36 29.10.2021 19:18:36
SQL>
date value remained the same (it is still today, 29.10.2021)
time has changed; right now, it is 22:18:36. When we subtract 3 hours from it, we get 19:18:36 (3 hours earlier)
It means that your query fetches rows whose last_updated_date column value is within the last 3 hours.

It will subtract 3 hours (24 hours/8) from sysdate, try running the following:
select to_char((sysdate - 1/8), 'mm/dd/yyyy hh24:mi:ss') from dual
This will give you 3 hours previous to the current date/time of the database.

Related

How in Oracle to calculate difference between current date and column date?

I have column INACTIVE_TIME where I need to put integer number (how many days pass from some date), to represent difference between current date and column date ("LOAD_DATE" column).
In column LOAD_DATE I have data in format 03-AUG-22 03.55.57.587481000 PM.
I understand I need to get current date and than minus date from LOAD_DATE column.
I try something like this:
SELECT COUNT(*)
FROM TABLE_NAME
WHERE ((TO_DATE(SYSDATE,'DD/MM/YYYY')-(TO_DATE(LOAD_DATE,'DD/MM/YYYY'));
It is about load_date column's datatype, not the way you see that value (because it can be changed). I presume (and hope) it is timestamp; you aren't storing it as a string, are you?
If so, then you don't apply to_date to sysdate - it is a function that already returns date datatype.
Setting timestamp and date format (just to know what is what; your tool displays different format, with month name and two-digits year) (you don't have to do that).
SQL> alter session set nls_timestamp_format = 'dd.mm.yyyy hh24:mi:ss.ff9';
Session altered.
SQL> alter session set nls_date_format = 'dd.mm.yyyy hh24:mi:ss';
Session altered.
Sample table; note datatype:
SQL> create table table_name (load_date timestamp);
Table created.
SQL> insert into table_name values (systimestamp);
1 row created.
Query you're looking for (at least, I think so):
SQL> select load_date, sysdate,
2 --
3 sysdate - load_date as diff
4 from table_name;
LOAD_DATE SYSDATE DIFF
------------------------------ ------------------- ------------------------------
04.08.2022 10:22:58.101062000 04.08.2022 10:23:08 +000000000 00:00:09.898938
SQL>
To extract days, hours, minutes ... whatever, you can use that function - extract. For example:
SQL> select load_date,
2 sysdate,
3 sysdate - load_date as diff,
4 --
5 extract (day from sysdate - load_date) as diff_days,
6 extract (hour from sysdate - load_date) as diff_hours,
7 extract (minute from sysdate - load_date) as diff_minutes
8 from table_name;
LOAD_DATE SYSDATE DIFF DIFF_DAYS DIFF_HOURS DIFF_MINUTES
------------------------- ------------------- -------------------------- ---------- ---------- ------------
04.08.22 10:22:58,101062 04.08.2022 11:51:32 +000000000 01:28:33.898938 0 1 28
SQL>
Your Where clause isn't saying anything. What are you wanting it to filter?
Try
Where (sysdate - table_name.load_date) > 0
This might not be what you want, but you need to tell the query something else

Trying to convert datetime in date in oracle

Hi i'm trying to convert date 01-03-2020 10:48:27 which obtained from query
SELECT
LAST_DAY( ADD_MONTHS(SYSDATE,-3 ) )+1
FROM
dual;
into '01-Mar-2020' but not able to do trying many concept
eg.
trunc(SELECT LAST_DAY( ADD_MONTHS(SYSDATE , - 3 ) )+1 FROM dual),'YEAR')
and
SELECT TRUNC(TO_DATE('SELECT LAST_DAY( ADD_MONTHS(SYSDATE , - 3 ) )+1 FROM dual','DD-MON-YY'), 'YEAR') "New Year" FROM DUAL;
but getting error
Any idea would be appreciated
You're making things way too complicated. Oracle TRUNC takes an additional parameter to specify whatever time interval to truncate to:
SELECT TRUNC(some_date_here, 'MON') FROM dual
If you put some_date_here as sysdate, then currently it will return 01-May-2020 until next month when it starts returning 01-Jun-2020
You can truncate to any interval; TRUNC 01/01/2000 12:34:56 with 'MI' will return 01/01/2000 12:34:00. Truncating to DD is the default (cut the time off). Truncating to DAY sets the date back to the day that started the week in the country oracle thinks it is in (probably a Sunday or Monday)
More info: https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions230.htm#i1002084
As I understood your problem you want to go from the current date, to the first of the month that was between 2 and < 3 months ago (so if it's May now, you want to go back to first of March until it's June, when you want to go back to first of April)
If you hence, in the current date of 5th May, want to go back to a date of 1 March, take 2 months off the current date and then TRUNC to the start of the month:
SELECT TRUNC(ADD_MONTHS(sysdate, -2), 'MON') FROM dual
Don't forget you can TRUNC to the nearest quarter of a year, so if you're doing a report that is "the current quarter", then looking at a variation of TRUNC(sysdate, 'Q') would be the way to go
Lastly, I'd urge you NOT to use oracle to convert your dates to strings (in most cases) - if you keep it as a date all the way 'tIl it hits the user's computer it can be formatted for their regional preferences. If you make a decision as to the format as its coming out the dB it makes it much harder to deliver a good international experience for your app
"Convert" in your case means TO_CHAR; alter session is here to set default format for this session.
SQL> alter session set nls_date_format = 'dd.mm.yyyy hh24:mi:ss';
Session altered.
SQL> select
2 last_Day(add_months(sysdate, -3)) + 1 orig,
3 to_char(last_day(add_months(sysdate, -3)) + 1, 'dd-Mon-yyyy', 'nls_Date_language = english') result
4 from dual;
ORIG RESULT
------------------- --------------------
01.03.2020 07:25:44 01-Mar-2020
SQL>
Or, if you altered the session, you'd get it as
SQL> alter session set nls_date_language = 'english';
Session altered.
SQL> alter session set nls_date_format = 'dd-Mon-yyyy';
Session altered.
SQL> select
2 last_Day(add_months(sysdate, -3)) + 1 orig
3 from dual;
ORIG
-----------
01-Mar-2020
SQL>
But, yes - usually we TO_CHAR it.

How to subtract 30 days from sysdate and exclude hours

i am trying to subtract 30 days from sysdate
The query that i am using is (sysdate - 30). However, this includes the current sysdate hours and minute.
i want it to include all data where sysdate is -30 without specificing the hour and minute.
For example:
SQL> alter session set nls_Date_format = 'dd.mm.yyyy hh24:Mi:ss';
Session altered.
SQL> select sysdate,
2 sysdate - 30 thirty_days_ago,
3 trunc(sysdate - 30) truncated
4 from dual;
SYSDATE THIRTY_DAYS_AGO TRUNCATED
------------------- ------------------- -------------------
26.09.2019 11:10:18 27.08.2019 11:10:18 27.08.2019 00:00:00
SQL>
If you want to display it without time component, either alter session again (but using different data format, e.g. dd.mm.yyyy), or apply TO_CHAR function:
SQL> select to_char(trunc(sysdate - 30), 'dd.mm.yyyy') without_time
2 from dual;
WITHOUT_TI
----------
27.08.2019
SQL>

Oracle SQL What does start_date-1 mean?

Oracle SQL What does start_date-1 mean?
Is it just one day less the start_date?
Assuming your datatype is a date or timestamp, yes, it will remove a full day from your date:
SELECT SYSDATE AS "start_date",
SYSDATE - 1 AS "start_date -1"
FROM DUAL;
Returns
26/08/2015 11:09:21 | 25/08/2015 11:09:21
YES. It will subrtract 1 day from the start_date.
Given that the data type of start_date is DATE or TIMESTAMP, subtracting N from it would subtract N number of days.
For example,
SQL> alter session set nls_date_format='DD-MM-YYYY HH24:MI:SS';
Session altered.
SQL> SELECT sysdate, sysdate -1 FROM DUAL;
SYSDATE SYSDATE-1
------------------- -------------------
26-08-2015 14:45:35 25-08-2015 14:45:35
SQL>
Remember, date has both date and time elements, so you will go back to previous day with exactly that time portion.
Yes it is 1 day earlier to start_date.

Oracle SQL - Column with unix timestamp, need dd-mm-yyyy timestamp

is there any way in Oracle, to get only the dd-mm-yyyy part from an unix timestamp in oracle?
Like:
select to_char(my_timestamp, 'ddmmyyyy') as my_new_timestamp from table
Given this data ...
SQL> alter session set nls_date_format='dd-mon-yyyy hh24:mi:ss'
2 /
Session altered.
SQL> select * from t23
2 /
MY_TIMESTAMP
--------------------
08-mar-2010 13:06:02
08-mar-2010 13:06:08
13-mar-1985 13:06:26
SQL>
.. it is simply a matter of converting the time elapsed since 01-JAN-1970 into seconds:
SQL> select my_timestamp
2 , (my_timestamp - date '1970-01-01') * 86400 as unix_ts
3 from t23
4 /
MY_TIMESTAMP UNIX_TS
-------------------- ----------
08-mar-2010 13:06:02 1268053562
08-mar-2010 13:06:08 1268053568
13-mar-1985 13:06:26 479567186
SQL>
As I understand it, A Unix timestamp is defined as a number of seconds since 1970-01-01, in which case:
select DATE '1970-01-01' + my_timestamp/86400 from table;
(There are 86400 seconds in a day.)
To ignore the hours, minutes and seconds:
select TRUNC(DATE '1970-01-01' + my_timestamp/86400) from table;
However, if what you want is a "truncated" Unix timestamp then try this:
select floor(my_timestamp/84600)*84600 from dual;
I believe it's:
select to_char(my_timestamp, 'dd-mm-yyyy') as my_new_timestamp from table
See also this reference on Oracle's date format specifiers.
Unix timestamp is seconds since Jan 01 1970. (UTC).
To get Unix timestamp of now, try the follwing sql:
select (CAST(SYS_EXTRACT_UTC(current_timestamp) AS date) - to_date('1970-01-01', 'YYYY-MM-DD')) * 86400 FROM dual;
You can replace current_timestamp with any timestamp-value.
BTW, some answers seem to return seconds since Jan 01, 1970 with local time zone. That's wrong.