HIVE - date_format( your_date_column, '%Y-%m-%d %H' ) - hive

I'm trying to achieve the MySQL equivalent of date_format( your_date_column, '%Y-%m-%d %H' ) as my_date in Hive. I've tried a few options from Hive date formatting but can't get the format right. I haven't found anything that has helped me yet.
Could I please request someone who may have already bumped into this situation or knows how to do it please?

Recent version of Hive have a date_format() function, it just uses java formatting codes instead of C. Try date_format(your_date_column, 'yyyy-MM-dd HH')

To convert date to given string format you have to use from_unixtime() function of hive
from_unixtime(bigint unixtime[, string format]) converts the number of seconds from unix epoch (1970-01-01 00:00:00 UTC) to a string representing the timestamp of that moment in the current system time zone.
The sample input and output can be seen from below image:
The final query is
select from_unixtime(unix_timestamp(),'yyyy-MM-dd HH') as my_date from table1;
where table1 is the table name present in my hive database.
I hope this help you to achieve date_format( your_date_column, '%Y-%m-%d %H' ) !!!

Let's say you have a column 'birth_day_H' in your table which is in string format,
you should use the following query to filter using birth_day_H
date_Format(birth_day_H, 'yyyy-MM-dd HH')
You can use it in a query in the following way
select * from yourtable
where
date_Format(birth_day_H, 'yyyy-MM-dd HH') = '2019-04-16 10';

I worked around this by using concat(substr(your_date_column,1,13), ':00')
In case the date column has a reserved keyword such as timestamp as in my case, this works - concat(substr(`timestamp`,1,13), ':00')

Related

How to convert BIGINT to DATE in Redshift?

I am trying to figure out how to convert and format a BIGINT field (i.e. 20200301) to a DATE type field using Redshift SQL. I was successful in getting the snippet below to work but I believe that returns a string and I need a valid date returned in 'YYYY-MM-DD' format. I've tried several other version unsuccessfully. Thank you in advance.
'''to_char(to_date(date_column::text, 'yyyymmdd'), 'yyyy-mm-dd')'''
You just want the to_date() part:
select to_date(date_column::text, 'YYYYMMDD')
When it is a timestamp we need the below code to convert into correct value.
select trunc(TIMESTAMP 'epoch' + date_column / 1000 * INTERVAL '1 second')

Timestamp to date convertion in oracle sql [duplicate]

How can we convert timestamp to date?
The table has a field, start_ts which is of the timestamp format:
'05/13/2016 4:58:11.123456 PM'
I need to query the table and find the maximum and min timestamp in the table but I'm not able to.
Select max(start_ts)
from db
where cast(start_ts as date) = '13-may-2016'
But the query is not returning any values.
Please help me in finding the max timestamp for a date.
CAST(timestamp_expression AS DATE)
For example, The query is : SELECT CAST(SYSTIMESTAMP AS DATE) FROM dual;
Try using TRUNC and TO_DATE instead
WHERE
TRUNC(start_ts) = TO_DATE('2016-05-13', 'YYYY-MM-DD')
Alternatively, you can use >= and < instead to avoid use of function in the start_ts column:
WHERE
start_ts >= TO_DATE('2016-05-13', 'YYYY-MM-DD')
AND start_ts < TO_DATE('2016-05-14', 'YYYY-MM-DD')
Format like this while selecting:
to_char(systimestamp, 'DD-MON-YYYY')
Eg:
select to_char(systimestamp, 'DD-MON-YYYY') from dual;
If the datatype is timestamp then the visible format is irrelevant.
You should avoid converting the data to date or use of to_char. Instead compare the timestamp data to timestamp values using TO_TIMESTAMP()
WHERE start_ts >= TO_TIMESTAMP('2016-05-13', 'YYYY-MM-DD')
AND start_ts < TO_TIMESTAMP('2016-05-14', 'YYYY-MM-DD')
You can try the simple one
select to_date('2020-07-08T15:30:42Z','yyyy-mm-dd"T"hh24:mi:ss"Z"') from dual;
You can use:
select to_date(to_char(date_field,'dd/mm/yyyy')) from table
I'd go with the following:
Select max(start_ts)
from db
where trunc(start_ts) = date'13-may-2016'
If you have milliseconds in the date string, you can use the following.
select TO_TIMESTAMP(SUBSTR('2020-09-10T09:37:28.378-07:00',1,23), 'YYYY-MM-DD"T"HH24:MI:SS:FF3')From Dual;
And then convert it to Date with:
select trunc(TO_TIMESTAMP(SUBSTR('2020-09-10T09:37:28.378-07:00',1,23), 'YYYY-MM-DD"T"HH24:MI:SS:FF3')) From Dual;
It worked for me, hope it will help you as well.
This may not be the correct way to do it. But I have solved the problem using substring function.
Select max(start_ts), min(start_ts)from db where SUBSTR(start_ts, 0,9) ='13-may-2016'
using this I was able to retrieve the max and min timestamp.

Oracle SQL how to convert time zone string to date

I have following 2015-06-17T00:00:00.000+05:00 string.
I want to convert this string to Date using oracle sql.
I tried lot of format mask but none works for me :
SELECT TO_DATE('2015-06-17T00:00:00.000+05:00','yyyy-mm-dd HH24:MI:SS TZR') FROM DUAL;
Any idea which format mask should i apply for above conversion.
Also please note that i only need date information i.e (mm-dd-yyyy). So its also ok if the conversion results in date information only (i.e skipping time information)
This should work:
SELECT TO_DATE(SUBSTR('2015-06-17T00:00:00.000+05:00',1,10),'yyyy-mm-dd') from dual
If you need to keep track of the time zone you should probably look at something like this:
SELECT CAST(TO_TIMESTAMP_TZ('2015-06-17T00:00:00.000+05:00','yyyy-mm-dd"T"HH24:MI:SS.FFTZH:TZM') AT TIME ZONE 'UTC' AS DATE) FROM DUAL;

ORA-01722 INVALID NUMBER in oracle

I am getting invalid number error message while executing the below select statement.Can any one have an idea about the issue..Please let me know.
select TO_DATE(TO_CHAR('2015/01/22 00:00:00','YYYY/MM/DD'),'YYYY/MM/DD')
actually i want oracle standard date format without time stamp for this date '2015/01/22 00:00:00'
select to_date('2015/01/22 00:00:00','YYYY/MM/DD HH24:MI:SS') as dt
from dual
Fiddle - http://sqlfiddle.com/#!4/6a3a6/1/0
As an FYI, the Oracle DATE data type does include the time component (just not down to fractional seconds, as is the case with the TIMESTAMP data type).
If you are converting values and want to bring all the time values to zero you can use the trunc function like this (which changes 12:07:00 to 00:00:00):
select trunc(to_date('2015/01/22 12:07:00','YYYY/MM/DD HH24:MI:SS'),'DD') as dt_with_time_zerod
from dual
Fiddle - http://sqlfiddle.com/#!4/6a3a6/2/0
If the source is itself a date and you want to convert the date to a string in the Oracle default date format ('DD-MON-RR') you can achieve that by running:
select to_char(trunc(to_date('2015/01/22 12:07:00','YYYY/MM/DD HH24:MI:SS'),'DD'),'DD-MON-RR') as dt_with_time_zerod
from dual
Fiddle - http://sqlfiddle.com/#!4/6a3a6/3/0
If it's a date field, to_char without a mask will give you what you say you want.
actually i want oracle standard date format without time stamp for this date '2015/01/22 00:00:00'
I'm not sure what you mean by "Oracle standard date format." The format in which a date would appear would be based on your NLS settings (in particular, NLS_DATE_FORMAT). If you are just trying to format this string representing a date, then you might want something like the following:
SELECT TO_CHAR(TO_DATE('2015/01/22 00:00:00','YYYY/MM/DD HH:MI:SS'), 'YYYY/MM/DD')
FROM dual;
That is, you have the TO_CHAR() and TO_DATE() functions in the wrong order, and an incomplete date mask for the call to TO_DATE().
Try using date literals with the standard ISO 8601 format.
date '2015-01-22'
I suggest you not to give hour-minute-second if you do not want to show the time.
This is my simplest answer :
SELECT TO_DATE('2015/01/22','YYYY/MM/DD') FROM dual

How to convert string to date format in Oracle

I have a file contains a string like 2011-03-14 11:57:42+08:00 and I need to store it in a column with DATE datatype. I use Oracle 11g.
I have tried TO_DATE function but it didn't work.
Can anyone help me?
thanks in advance
You have to use the function TO_TIMESTAMP_TZ to get the timezone properly parsed:
SELECT CAST(TO_TIMESTAMP_TZ('2011-03-14 11:57:42+08:00', 'YYYY-MM-DD HH24:MI:SS+TZH:TZM') AS DATE) AS my_date
FROM DUAL
;
(And as a side note to your example in the comment: no need for TO_CHAR, quoting was missing and format model MON (abbreviated month) was incorrect)