When we use the query like this in oracle to get the the total number of hours in round figure,
hours= select round(out_time-in_time)*24 from table_name;
what is the datatype of hours here?
out_time and in_time are column names
You can take NUMBER as the datatype in your case.
EDIT:- This will return you an integer
select 24 * round((to_date('2013-07-07 22:00', 'YYYY-MM-DD hh24:mi')
- to_date('2013-07-07 19:30', 'YYYY-MM-DD hh24:mi'))) diff_hours
from table_name;
Maybe a datatype called "INTERVAL" will suit you. It's called:
INTERVAL DAY [(day_precision)] TO SECOND
Stores a period of time in days, hours, minutes, and seconds
Interval is something that can we added (or substrated) to a DATE or TIMESTAMP.
see: http://docs.oracle.com/cd/E11882_01/server.112/e26088/sql_elements001.htm
Related
I'm querying the database (RedShift) and I have a piece of information stored in epoch MS format. Envision a table along the lines of:
Purchase, date
1, 1620140227019
2, 1620140227045
3, 1620140226573
I need to convert the timestamp to a readable date but I can't make it work with to_timestamp() or extract(). The problem is first with the size of the value (13 digits are not supported).
The closest solution I have is
select to_timestamp(1620140226573/1000, 'SS')
But the result is 0051-05-04 14:57:06. In other words month, date and seconds are correct but the year is wrong.
You can run this query
select to_timestamp(round(1620140227254/1000))
The solution was in the documentation: https://docs.aws.amazon.com/redshift/latest/dg/r_Dateparts_for_datetime_functions.html
SELECT timestamp with time zone 'epoch' + 1620140227019/1000 * interval '1 second' AS converted_timestamp
or
select '1970-01-01'::date + 1620140227019/1000 * interval '1 second'
I have a postgres database carrying date/time information in a text format. There is no way of changing it, but I have to retrieve those values as milisecons since epoch.
I managed to make a query, converting those date-time records to timestamps so that I get a correct "max" function behaviour like so:
SELECT max(TO_TIMESTAMP(column_name, 'YYYY/MM/DD HH24:MI:SS'))
FROM table_name;
But converting other results into miliseconds does not seem to work. And all the examples int the documentation and forums showcase only the usage for some literal value, not a value selected from a database. So lines like these don't work:
SELECT EXTRACT(EPOCH FROM TIMESTAMP
(select max(TO_TIMESTAMP(column_name, 'YYYY/MM/DD HH24:MI:SS'))
FROM table_name));
SELECT EXTRACT(EPOCH FROM TIMESTAMP
(select TO_TIMESTAMP(column_name,'YYYY/MM/DD HH24:MI:SS')
FROM table_name));
SELECT EXTRACT(EPOCH FROM TIMESTAMP WITH TIME ZONE(
SELECT TO_TIMESTAMP(column_name, 'YYYY/MM/DD HH24:MI:SS')
FROM table_name));
SELECT EXTRACT(EPOCH FROM TIMESTAMP WITH TIME ZONE TO_TIMESTAMP
(column_name, 'YYYY/MM/DD HH24:MI:SS'))
FROM table_name;
Is there an actual way to accomplish what I want by using a query, or I have to do something more complicated?
P.S.
Of course I can just retrieve all the infomation as text and use Qt (QDateTime) to convert it to miliseconds, but It would be more expensive and I was wondering if there is a way to ask the database to do it for me.
The timestamp keyword is only needed for literals (constants), not if you have a proper timestamp value available:
SELECT extract(epoch from max(TO_TIMESTAMP(column_name, 'YYYY/MM/DD HH24:MI:SS')))
FROM table_name;
Note that epoch represents seconds, not milliseconds.
I have Oracle 12c DB table and one of it's column utc_timestamp is of type
UTC_TIMESTAMP TIMESTAMP(6) WITH TIME ZONE
It stores timestamp in UTC while current_timestamp and systimestamp both gives timestamp in different timezones.
How can I get time difference in MAX(utc_timestamp) and current_timestamp in minutes ignoring time difference due to different time zones.
For example:
select current_timestamp from dual;
Gives=> 23-AUG-17 04.43.16.253931000 PM AMERICA/CHICAGO
select systimestamp from dual;
Gives=> 23-AUG-17 05.43.16.253925000 PM -04:00
select max(UTC_TIMESTAMP) from table_name;
Gives=> 23-AUG-17 09.40.02.000000000 PM +00:00
For above condition when I run SQL to check time difference between in MAX(utc_timestamp) and current_timestamp I should get number 3.
I think I need something like:
select (extract(minute from current_timestamp) - extract(minute from max(UTC_TIMESTAMP)) * 1440) AS minutesBetween from table_name;
But different timezones are messing it up and I get negative number like -4317. This might be correct as current_timestamp will be higher than max(utc_timestamp) being in CST. So I tried:
select (extract(minute from CAST(current_timestamp as TIMESTAMP(6) WITH TIME ZONE)) - extract(minute from max(UTC_TIMESTAMP)) * 1440) AS minutesBetween from table_name;
This SQL runs without error but producing a big negative number like -83461. Please help me find what am I doing wrong.
You really have two problems here.
One is to convert CURRENT_TIMESTAMP to UTC. That is trivial:
select CURRENT_TIMESTAMP AT TIME ZONE 'UTC' from dual [.....]
(use the AT TIME ZONE clause https://docs.oracle.com/cd/B19306_01/server.102/b14225/ch4datetime.htm#i1007699)
The other is that the difference between two timestamps is an interval, not a number.
select current_timestamp at time zone 'UTC'
- to_timestamp_tz('24-AUG-17 04.00.00.000 AM UTC', 'dd-MON-yy hh.mi.ss.ff AM TZR')
from dual;
produces something like
+00 00:02:39.366000
which means + (positive difference) 00 days, 00 hours, 02 minutes, 39.366 seconds.
If you just want the minutes (always rounded down), you may wrap this whole expression within extract( minute from < ...... > ). Be aware though that the answer will still be 2 (minutes) even if the difference is five hours and two minutes. It is probably best to leave the result in interval data type, unless you are 100% sure (or more) that the result is always less than 1 hour.
I have time, select cast(SYSDATETIME() AS time)
14:59:09.2834595
What is the way to truncate seconds?
14:59
Description
You can use the T-SQL function convert.
Sample
PRINT convert(varchar(5), SYSDATETIME(), 108)
will give you hh:mm
More Information
MSDN - CAST and CONVERT
If you want to truncate seconds and still have a T-SQL Date datatype, first convert the date into minutes from the date '0' and then add the minutes back to '0'. This answer doesn't require any additional parsing/converting. This method works to truncate other parts just change MINUTE.
Example:
SELECT DATEADD(MINUTE, DATEDIFF(MINUTE, 0, '2016-01-01 23:22:56.997'), 0)
If you need to drop seconds off entirely, you can use the DATEPART() function (SQL Server) to strip out the hour and minute, then append it back together. (I like dknaack's solution more, if that works.)
SELECT CAST(DATEPART(hour, SYSDATETIME()) + ':' + DATEPART(minute, SYSDATETIME()) AS DATETIME)
select cast(left(cast(SYSDATETIME() AS time), 5) as time)
I have a table called items. It contains a column tz with timezone identifiers (like America/New_York, Europe/London, etc).
I want to select all the items where the current time is 8AM +/- 5 minutes.
So if I run the query at 8AM EST it will return rows where tz = 'America/New_York'.
If I run the query at 9AM EST it will return rows where tz = 'America/Chicago'.
SELECT *
FROM items
WHERE CAST(current_timestamp at time zone tz AS time) BETWEEN '07:55' AND '08:05'
You should be storing the offset for each timezone. Then the query is as simple as
select * from items where GETDATE() between
DATEADD(minute,5,DATEADD(HOUR,offset,GETUTCDATE())) and
DATEADD(minute,-5,DATEADD(HOUR,offset,GETUTCDATE()))
I'm using SqlServer GETUTCDATE and DATEADD functions, but you can easily make them work in postgresql if you lookup the correct function names.
EDIT
If you can't add a offset to the table, create a timezone table with the string timezone and numeric offset. Join and run the above query.
Your tz field is valid if it respects this: http://www.postgresql.org/docs/8.0/static/datetime-keywords.html#DATETIME-TIMEZONE-SET-TABLE
If so, I think this should work (no tested):
SELECT *
FROM items
WHERE LOCALTIMESTAMP BETWEEN
(LOCALTIMESTAMP AT TIME ZONE tz) - interval '5 minutes' AND
(LOCALTIMESTAMP AT TIME ZONE tz) + interval '5 minutes'