converting native SAS datetime to oracle - sql

Been trying to change a native SAS datetime value to an oracle one-
ie. 1788130680 converts to 29AUG2016:22:58:00
I had attempted to do this:
to_date('01/01/1960','DD/MM/YYYY')+ 1788130680
but no dice. Suggestions appreciated.
Thank you.

It looks like you have a number of seconds since January 1st, 1960. You can turn it to a date as follows:
date '1960-01-01' + 1788130680 / 60 / 60 / 24
The idea is to conver the number to days (by dividing it by the number of seconds there is in a day), and then add it to the starting date.
It might be easier to understand by using an interval:
date '1960-01-01' + 1788130680 * interval '1' second

Related

Mysql Date to minutes

Im new using sql, i tried to convert interval to minutes. Is there a developed function that did this.
Thank you
You can use epoch and divide by 60.
select extract(epoch from <date>) / 60
extract(epoch) gives the number of seconds since 1970-01-01. So, this gives the number of minutes since 1970-01-01, which seems like a reasonable interpretation of your question.
I suppose from tag that you use PostgreSql.
Postgresql has a very good documentation. In this link you can find all the date-time functions.
In your case you can use this function to truncate date to minute part:
date_trunc(text, timestamp)
date_trunc('minute', date_column)
If you need the timestamp, you can cast to timestamp ::timestamp and then convert to minutes

time difference in sql Oracle

I need to know a difference between start time and end time. Both are DATETIME fields, I tried to use "-" and DATADIFF.
I already tried using DATADIFF and simple subtraction converting the field to just time.
(to_date(Fim_Hora,'HH24:MI') - to_date(Inicio_Hora,'HH24:MI')) AS Diferenca
DATADIFF(MIN,Fim_Hora,Inicio_Hora)
I need to know the time in minutes for use as parameters.
Oracle does not have a time data type. Usually, subtraction works well enough:
select (end_time - start_time) as diff
You may need to convert to a string if you want it formatted in a particular way.
In Oracle, you can directly substract dates, it returns the difference between the dates in days. To get the difference in minutes, you can multiply the result by 24 (hours per days) and 60 (minutes per hour):
(Fim_Hora - Inicio_Hora) * 24 * 60 diff_minutes
This assumes that both Fim_Hora and Inicio_Hora are of datatype DATE.

presto - getting days interval (not date)

How do I get the days interval for prestodb? I can convert to milliseconds and convert these to number of days but I am looking if there is any shorter way to do this.
Example: I want to see how many days has it been since the first row inserted in a table.
SELECT
to_milliseconds(date(current_date) - min(created)) / (1000*60*60*24) as days_since_first_row
FROM
some_table
What I am hoping to see: (Either 1 of below)
SELECT
to_days(date(current_date) - min(created)) / (1000*60*60*24) as days_since_first_row
,cast(date(current_date) - min(created)) as days) as days_since_first_row2
FROM
some_table
Unfortunately, daylight savings breaks the solution from the accepted answer. DAY(DATE '2020-09-6' - DATE '2020-03-09') and DAY(DATE '2020-09-6' - DATE '2020-03-08') are both equal to 181 due to daylight savings time and DAY acting as a floor function on timestamps.
Instead, use DATE_DIFF:
DATE_DIFF('day', DATE '2020-09-6', DATE '2020-03-09')
Use subtraction to obtain an interval and then use day on the interval to get number of days elapsed.
presto:default> select day(current_date - date '2018-07-01');
_col0
-------
86
The documentation for this is at https://trino.io/docs/current/functions/datetime.html

Oracle SQL - Trying to Add One Second to a Date I'm pulling from a text field

SQL newbie here.
In Oracle BI Publisher, I'm using Oracle SQL to do the following.
I need to pull only the time from a string and add one second to the time. The data in the 'Flight' column looks like this:
Dayton 01:23:59
I pull only the time using the following syntax:
substr(Flight,length(Flight)-8,8)
Which gives me this:
01:23:59
In the messy code below, I'm trying to add one second to the time which works but the second is '60' which is obviously is an invalid time.
substr(lpad(to_number(replace(substr(Flight,length(Flight)-15,9),':')+2,'999999'),6,'0'),1,2)||':'||substr(lpad(to_number(replace(substr(Flight,length(Flight)-15,9),':')+2,'999999'),6,'0'),3,2)||':'||substr(lpad(to_number(replace(substr(Flight,length(Flight)-15,9),':')+2,'999999'),6,'0'),5,2)
Any ideas for a better way to do this?
Dan
Can you do something like:
to_date(substr(Flight,length(Flight)-8,8), HH:MI:SS') + interval '1' second
You can also use
TO_DATE(SUBSTR(FLIGHT, LENGTH(FLIGHT-8)), 'HH24:MI:SS') + 1 / (24 * 60 * 60)
or
TO_DATE(SUBSTR(FLIGHT, LENGTH(FLIGHT-8)), 'HH24:MI:SS') + 1 / 86400
because
24 = hours in a day
* 60 = minutes in an hour
* 60 = seconds in a minute
-----
86400 = seconds in a day
After a while you find you've memorized numbers like that. :-)

SQL : How sysdate minus a value works?

I found below code in my existing project.
select * from mytable where SomeColumn_date >= trunc(sysdate)-.25/24;
Sample value for SomeColumn_date is 22-JUN-17 05:46:55
How does SomeColumn_date >= trunc(sysdate)-.25/24 work on Date data type?
Different database engines allow different operations to be applied to date data types. For the most part, an operation of <Date Value> +/- 1 will add or subtract one day to that date value. This is syntactically equivalent to the dateadd function when using days.
In your example here, the -.25/24 resolves to the number equivalent of -15 minutes, which is then subtracted from your date value.
It is essentially a much less readable version of datedd(min,-15,<Date Value>).
From the documentation of TRUNC (I'm guessing you are using Oracle):
The TRUNC (date) function returns date with the time portion of the day truncated to the unit specified by the format model fmt. [...] If you omit fmt, then date is truncated to the nearest day.
The result of trunc(sysdate) would be the present date without the time component. Now .25/24 (actually meaning 0.25/24) is substracted from that. If you substract a date using - the operand is always in days. 0.25/24 would be a form to express a quarter of an hour.
So trunc(sysdate)-.25/24 would result in yesterday 23:45.
Ok so 2 things are happening here:
trunk(date,fmt)
The TRUNC (date) function returns date with the time portion of the day truncated to the unit specified by the format model fmt. If you omit fmt, then date is truncated to the nearest day.
So if you have suppose 22-JUN-17 05:46:55 you get 22-JUN-17. Since you don't have the fmt
DATETIME - .25/24 implies .25 hours before your current Date time.
But since you have only DATE all it does is .25 hours before todays 12:00 AM i.e yesterdays 11:45PM
SomeColumn_date >= trunc(sysdate)-.25/24
So suppose if its 22-JUN-2017 right now the date is compared to 21-JUN-2017 11:45 PM
NOTE: - is for before current time, + is for after the current time