I have a time field which is storing numbers in '49235062'. Can these be converted to an actual readable time?
This does not look anything like a time stamp.
Thanks
Just a guess, but is this milliseconds from midnight?
Example
Select dateadd(MILLISECOND,49235062,0)
Returns
1900-01-01 13:40:35.063 -- 1:40 PM
If so, then it is a small matter to convert to time or format as time
Assuming that this is a UNIX timestamp (number of seconds since 1/1/1970), try the following:
DECLARE #timeStamp varchar(10) = '49235062'
SELECT #timeStamp, CONVERT(TIME, dateadd(S, CAST(#timeStamp AS int), '1970-01-01'))
Produces the following
49235062 20:24:22.0000000
So your time is 20:24:22 which is the answer that #pac0 suggested.
Related
I have a weird dataset that I need to access in hive. Using traditional date/time functions (such as dateadd, etc) have proven difficult/ineffective.
There is a column in my dataset that is a string with the date in YYYY-MM-DD format.
I am wondering if it's possible to get the current date in YYYY-MM-DD format, and somehow cast it as a string?
I've done considerable amounts of research and have tried just about everything in the documentation (https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF#LanguageManualUDF-DateFunctions)
Any help here will be greatly appreciated, as I've been grinding away at this problem for a considerable amount of time :)
Thanks!
I didn't get how the date will be displayed for format YYYY-MM-DD, but you can try below.
hive> select cast(from_unixtime(unix_timestamp(cast(current_date() as string), 'yyyy-MM-dd'),'YYYY-MM-DD') as string);
OK
2018-09-271
Time taken: 0.114 seconds, Fetched: 1 row(s)
hive>
from_unixtime accepts string format and you can get all the options here - https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
hope this helps.
VIJAY
Using PostgreSQL database for my attendance application.
I have a table with IN and out times (hh:mm:ss.us format).
When I subtract the times (OUT -IN) to calculate the working hours, results are not as expected due to precision.
If IN Time is "22:12:56.09"
& OUT TIme is "22:14:06.06" the difference considering only HH:mm should be 00:02 but it actually shows "00:01:09.97" which becomes "00:01" in excel using only HH:mm.
I am trying to do the time conversion from hh:mm:ss.us to hh:mm (time format) so that I can subtract the time and get the desired output.
I have done similar things in SQL Server but I did not find any function in PostgreSQL. Please advise.
First you need to truncate the seconds. Then subtract to get desired result
select
to_char(
(
to_char('22:14:06.06' :: time, 'HH24:MI'):: time -
to_char('22:12:56.09' :: time, 'HH24:MI'):: time
),
'HH24:MI'
)
Result: 00:02
General Solution:
select
to_char(
(
to_char(out, 'HH24:MI'):: time - to_char(in, 'HH24:MI'):: time
),
'HH24:MI'
)
Here the purpose of to_char() is to format result to hours:minutes and not to include seconds.
Postgres includes seconds in interval by default.
You can use the date_trunc function with timestamp.
It would work something like this:
select date_trunc('minute', out) - date_trunc('minute', in)
This would set a minute level precision on the timestamp and convert HH:mm:ss to HH:mm:00
I have a column in my dataset that has a datatype of bigint:
Col1 Col2
1 1519778444938790
2 1520563808877450
3 1519880608427160
4 1520319586578960
5 1519999133096120
How do I convert Col2 to the following format:
year-month-day hr:mm:ss
I am not sure what format my current column is in but I know that it is supposed to be a timestamp.
Any help will be great, thanks!
Have you tried to use functions like from_unixtime? You could use it to convert unix time to timestamp, then you could use date_format to display it in way you want. Notice that in your example your unix time is with microseconds, so you might want to convert it first to milliseconds.
I have not tested that but I am assuming that your code should look like:
date_format(from_unixtime(col2/1000), '%Y-%m-%d %h:%i:%s')
Notice that from_unixtime accepts also a time zone.
Please visit this page to see the more details about date related functions: https://docs.starburstdata.com/latest/functions/datetime.html
I believe the denominator should be 1000000 not 1000. Probably a typo. Anyways juts adding the test results here for others reference.
-- Microseconds
select date_format(from_unixtime(cast('1519778444938790' as bigint)/1000000), '%Y-%m-%d %h:%i:%s');
2018-02-28 12:40:44
If you need to filter the data where the column is in BIGINT Unix format, then you can use the following snippet to compare : from_unixtime(d.started_on /1000) >= CAST('2022-05-10 22:00:00' AS TIMESTAMP )
Accepted answer is a bit misleading. You should divide by 1000.0 otherwise you'll lose ms precision and be limited to second precision:
date_format(from_unixtime(col2/1000.0), '%Y-%m-%d %h:%i:%s')
I have dates in a postgres database. The problem is they are stored in a string field and have values similar to: "1187222400000" (which would correspond to 07.08.2007).
I would like to convert them into readable dates usind some SQL to_date() expression or something similar but can't come up with the correct syntax to make it work.
There really isn't enough information here for a conclusion, so I propose this 'scientific-wild-ass-guess' to resolve your puzzle. :)
It appears this number is UNIX 'epoch time' in milliseconds. I'll show this example as if your string field had the arbitrary name, 'epoch_milli'. In postgresql you can convert it to a time stamp using this statement:
SELECT TIMESTAMP WITH TIME ZONE 'epoch' + epoch_milli * INTERVAL '1 millisecond';
or using this built-in postgresql function:
SELECT to_timestamp(epoch_milli / 1000)
either of which, for the example '1187222400000', produces the result
"2007-08-15 17:00:00-07"
You can do some of your own sleuthing with quite a few values selected similarly to this:
SELECT to_timestamp(epoch_milli/1000)::DATE
FROM (VALUES (1187222400000),(1194122400000)) AS val(epoch_milli);
"Well, bollocks, man. I just want the date." Point taken.
Simply cast the timestamp to a date to discard the excess bits:
SELECT to_timestamp(epoch_milli / 1000)::DATE
Of course its possible that this value is a conversion or is relative to some other value, hence the request for a second example data point.
I am converting an Unix script with a SQL transact command to a PostgreSQL command.
I have a table with records that have a field last_update_time(xtime) and I want to select every record in the table that has been updated within a selected period.
Say, the current time it 05/01/2012 10:00:00 and the selected time is 04/01/2012 23:55:00. How do I select all the records from a table that have been updated between these dates. I have converted the 2 times to seconds in the Unix script prior to issuing the psql command, and have calculated the interval in seconds between the 2 periods.
I thought something like
SELECT A,B,C FROM table
WHERE xtime BETWEEN now() - interval '$selectedtimeParm(in secs)' AND now();
I am having trouble evaluating the Parm for the selectedtimeParm - it doesn't resolve properly.
Editor's note: I did not change the inaccurate use of the terms period, time frame, time and date for the datetime type timestamp because I discuss that in my answer.
What's wrong with:
SELECT a,b,c
FROM table
WHERE xtime BETWEEN '2012-04-01 23:55:00'::timestamp
AND now()::timestamp;
If you want to operate with a count of seconds as interval:
...
WHERE xtime BETWEEN now()::timestamp - (interval '1s') * $selectedtimeParm
AND now()::timestamp;
Note the standard ISO 8601 date format YYYY-MM-DD h24:mi:ss which is unambiguous with any locale or DateStyle setting.
The first value for the BETWEEN construct must be the smaller one. If you don't know which value is smaller use BETWEEN SYMMETRIC instead.
In your question you refer to the datetime type timestamp as "date", "time" and "period". In the title you used the term "time frames", which I changed to "timestamps". All of these terms are wrong. Freely interchanging them makes the question even harder to understand.
That, and the fact that you only tagged the question psql (the problem hardly concerns the command line terminal) might help to explain why nobody answered for days. Normally, it's a matter of minutes around here. I had a hard time understanding your question.
Understand the data types date, interval, time and timestamp - with or without time zone. Start by reading the chapter "Date/Time Types" in the manual.
Error message would have gone a long way, too.
For anyone who is looking for the fix to this. You need to remove timestamp from the where clause and use BETWEEN!
TABLENAME.COL-NAME-FOR-TIMESTAMP BETWEEN '2020-01-29 04:18:00-06' AND CURRENT_TIMESTAMP