Hive SQL- Adding (not appending) seconds to an existing time stamp - sql

I am trying to add seconds to an existing time stamp to find the end time.
I have start time in one field and total duration in one field. How could i add. i tried to use date_add function, but that dont work.
Select
testdate,
DATE_ADD ( testdate,CAST (testduration as INT) ) as Test_end_date
from <DBName> limit 10;
TestDate is this format= 9/14/2017 16:33:25.000000
Testduration is in seconds e.g: 144 seconds or so
It adds to the date rather than to the seconds component.
Any help is appreciated.
Tried Date_Add function, wont work.

Convert to seconds using unix_timestamp, add seconds
select from_unixtime(UNIX_TIMESTAMP('9/14/2017 16:33:25.000000','M/dd/yyyy HH:mm:ss.SSSSS')+144, 'yyyy-MM-dd HH:mm:ss.SSSSS')
This returns timestamp in standard Hive timestamp format, change output format if you need to keep original one: 'M/dd/yyyy HH:mm:ss.SSSSS' as a second parameter of from_unixtime() function.

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

Select rows which date (epoch) field equals a specific year [duplicate]

I store date from Calendar.getTimeInMilliseconds() in SQLite DB.
I need to mark first rows by every month in SELECT statement, so I need convert time in milliseconds into any date format using SQLite function only. How can I avoid this?
One of SQLite's supported date/time formats is Unix timestamps, i.e., seconds since 1970.
To convert milliseconds to that, just divide by 1000.
Then use some date/time function to get the year and the month:
SELECT strftime('%Y-%m', MillisField / 1000, 'unixepoch') FROM MyTable
Datetime expects epochtime, which is in number of seconds while you are passing in milliseconds. Convert to seconds & apply.
SELECT datetime(1346142933585/1000, 'unixepoch');
Can verify this from this fiddle
http://sqlfiddle.com/#!5/d41d8/223
Do you need to avoid milliseconds to date conversion or function to convert milliseconds to date?
Since sqlite date functions work with seconds, then you can try to
convert milliseconds in your query, like this
select date(milliscolumn/1000,'unixepoch','localtime') from table1
convert millis to seconds before saving it to db, and then use date function in sql query

Impala: set a constant date for timestamp

is there an alternative to the to_char() function in impala? I want to set a timestamp field where the date and minutes are fixed and only showing the hours, but can't seem to find an alternative.
This is my existing code in postgres which I need to convert to impala.
select to_char(starttime, '1900-01-01 HH24:00:00')::timestamp
Any help is appreciated!
In Impala, you can use
SELECT hours_add('1900-01-01', hour(now()));
to get the same result
function hour extracts the hour part from a timestamp
function hours_add adds hour to a timestamp

No proper output for this SQL query

I m using this query to get a result of the difference between the start time and end time of an activity.
SELECT end_time,
NVL(end_time, (TO_Date(sysdate, 'dd/mm/yyyy hh24:mi:ss'))),
(end_time - start_time) * 24 * 60 difference_in_minutes
FROM NCR;
NCR is the name of the table. Start time and end time are in date datatype. Where the end time is NULL, I wanted to put the value as current system date and time. I am getting the results for every row except for the ones where the end time is NULL. Please help and guide
You appear to know the NVL function but you're not using it in your calculation. If you want the NVL to be a part of your calculation, it would need to be part of the calculation
(nvl(end_time,sysdate) - start_time) * 24 * 60 difference_in_minutes
sysdate is already a date so it makes no sense to pass it in to to_date. to_date doesn't take a date parameter, it only takes a string. If you pass a date to to_date, Oracle has to first convert the date to a string using your session's NLS_DATE_FORMAT. Then, to_date runs with whatever format mask you specified. At best, you're just converting it back into the same date you started with. If your session's NLS_DATE_FORMAT doesn't match the format mask you're passing in to to_date, however, you may get a different date back or you may get an error.

EXTRACT the date and time - (Teradata)

I am trying to extract the date and time from a field in Teradata.
The field in question is:
VwNIMEventFct.EVENT_GMT_TIMESTAMP
Here is what the data look like:
01/02/2012 12:18:59.306000
I'd like the date and time only.
I have tried using EXTRACT(Date, EXTRACT(DAY_HOUR and a few others with no success.
DATE_FORMAT() does not appear to work since I'm on Teradata.
How would I select the date and time from VwNIMEventFct.EVENT_GMT_TIMESTAMP?
If the datatype of EVENT_GMT_TIMESTAMP is a TIMESTAMP, it's simple Standard SQL:
CAST(EVENT_GMT_TIMESTAMP AS DATE)
CAST(EVENT_GMT_TIMESTAMP AS TIME)
If it's a CHAR you need to apply a FORMAT, too:
CAST(CAST(EVENT_GMT_TIMESTAMP AS TIMESTAMP FORMAT 'dd/mm/yyyyBhh:mi:SS.s(6)') AS DATE)
CAST(CAST(EVENT_GMT_TIMESTAMP AS TIMESTAMP FORMAT 'dd/mm/yyyyBhh:mi:SS.s(6)') AS TIME)
Edit:
For simply changing the display format you need to add a FORMAT and a CAST to a string:
CAST(CAST(EVENT_GMT_TIMESTAMP AS FORMAT 'YYYYMMDDHHMI') AS CHAR(12))
or
CAST(CAST(EVENT_GMT_TIMESTAMP AS FORMAT 'YYYYMMDDHHMISS') AS CHAR(14))
If you don't care about display, just want to truncate the seconds:
EVENT_GMT_TIMESTAMP - (EXTRACT(SECOND FROM EVENT_GMT_TIMESTAMP) * INTERVAL '1.000000' SECOND)
Working with timestamps is a bit tricky :-)
I know this is an old topic, but I've struggled with this too. Try:
CAST(EVENT_GMT_TIMESTAMP AS TIMESTAMP(0))
The result will be
01/02/2012 12:18:59
The datatype will still be timestamp, but it will just be the date and time with no microseconds (looks just like a datetime object in Microsoft SQL).