How can I convert gregorian date to julian date in Hive - apache

I have a table with column name "date". The date is structured as YYYY-MM-DD and i need to convert it to YYYYDDD
I don't think hive has any simple quick way of doing this..
Using hive version 0.13.0

You can do this with the unix timestamp functions. First defining your date format and converting to a unix epoch timestamp, and then converting the unix timestamp into the Julian date format.
-- this would give the output of 2016096
select from_unixtime(unix_timestamp('2016-04-05','yyyy-MM-dd'), 'yyyyDDD') from yourTableName

Related

How to convert the date July 1, 2017 to dd-MM-yyyy using Hive SQL?

I have a Hive table with a Week column having values such as:
I have to convert this field to a date format such as: 2017-07-01 (yyyy-MM-dd) using hive SQL.
Any suggestions?
You can use a combination of from_unixtime and unix_timestamp.
select from_unixtime(unix_timestamp(weekCol,'MMM dd, yyyy'),'yyyy-MM-dd')
Use a combination of unix_timestamp and from_unixtime
select from_unixtime(unix_timestamp(week,'MMMM dd, yyyy'),'yyyy-MM-dd') from table_name;
unix_timestamp(string datetime, string pattern) converts datetime with given pattern to unix time stamp.
from_unixtime(bigint unixtime[, string format]) converts the number of seconds from unix epoch.

How to convert date in YYYYMMDD in Hive to unix timestamp

I am trying to convert date in format YYYYMMDD in hive to unix_timestamp but when I do below, I am getting incorrect timestamp.
select unix_timestamp(DATE,'YYYYMMDD') from table_name.
For '20180301' I am getting unix timestamp output as '1514631600' which is DECEMBER 30,2017 11:59 pm
The format string should be yyyyMMdd.
select unix_timestamp(DATE,'yyyyMMdd') from table_name

Hive- Extract timestamp and date in defined format

I have column value in my HIVE table in String format like 20160921091213 i.e. YYYYMMDDHHMMDD. In target I have two columns one timestamp and other date column. I want to extract the same in the format for timestamp "YYYY-MM-DD HH24:MI:SS" and for date in the format "YYYY-MM-DD".
What can be the possible SQL for that.
convert to unix timestamp format and then convert back to string.
from_unixtime(unix_timestamp('20160921091213', 'yyyyMMddHHmmss'),'yyyy-MM-dd HH:mm:ss')
Result: 2016-09-21 21:12:13

Convert from Unix time to YYYY-MM-DD in BigQuery

if there is a function to or a way to convert from UNIX time to YYYY-MM-DD in BigQuery syntax ?
If you have stored timestamp then you can get the UTC format by running below query
SELECT FORMAT_UTC_USEC((INTEGER(your_timestamp_field)+19800)*1000000)
The newer "standard SQL" syntax has a set of functions for timestamp operations. That includes timestamp_seconds, which converts unixtime (in seconds - there's another function for milliseconds) to a timestamp:
select timestamp_seconds(time) from <your-table>
If you want to format that in a particular way, you can convert that to a formatted string with format_timestamp. For instance, for your YYYY-MM-DD:
select format_timestamp("%Y-%m-%d", timestamp_seconds(time)) from <your-table>

SQLite Data Time Function

I currently have a timestamp in this format Tue Jun 03 17:17:05 +0000 2014 in one column in my table. I want to count the number of records happening in specific intervals (15 minutes). I have tried to follow the answer found in Group records by time. Although my timestamp is in a different format and I haven't seen any support function available in SQLite to convert this. Is this possible in SQL?
The SQLite date and time functions can be used to convert a timestring to a canonical format, or to a Julian Day Number. Unfortunately, the SQLite date and time functions only accept timestring in a limited number of formats:
YYYY-MM-DD
YYYY-MM-DD HH:MM
YYYY-MM-DD HH:MM:SS
YYYY-MM-DD HH:MM:SS.SSS
YYYY-MM-DDTHH:MM
YYYY-MM-DDTHH:MM:SS
YYYY-MM-DDTHH:MM:SS.SSS
If your timestring format has fixed field widths, you can use the substr function and the || string concatenation operator to convert it to a format SQLite understands. You'll have to use a case expression to convert the month names to numbers; here's an example.
You may use NEW_TIME in Oracle to convert the time to a specific timezone. Here is an example. This example is converting SYSDATE from PDT to GMT.
SELECT NEW_TIME (SYSDATE, 'PDT', 'GMT') FROM DUAL;
This thread is detailing how to add required minutes to your timestamp.