Changing format of date field in Hive - date

I have a Hive table that has the timestamps in this format 08/29/2015 0:00:08. I have tried to use the unixtimestamp functions in order to isolate the date so I can use it for comparisons, but am always getting nulls for outputs. I think I need to change the format to 2015-08-09 0:00:08 but don't know how. I am new to Hive so any guidance would be welcome.

You should use a UDF to convert them to timestamps
unix_timestamp("08/29/2015 0:00:08", "dd/MM/yyyy H:mm:ss")
Check for help here :http://docs.treasuredata.com/articles/hive-functions#date-functions

Related

Date formatting in postgresql from existing data

I've date in dd-mm-yyyy format, I need the date in yyyy-mm(month) format. I'm using postgresql.
Thanks in advance.
date values don't have any format. Any formatting you see is applied by your SQL client. To turn a date into a string with a specific format, you can use to_char()
to_char(the_column, 'yyyy-mm')

Please help to convert String to timestamp in hive

Please help me to convert string to timestamp.
source data is in Excel
Need to convert it as below timestamp
2019-12-15T16:35:53.663-04:00
I tried with.
select from_unixtime(unix_timestamp('12/15/2019 21:18','mm/dd/yyyy'),'YYYY-MM-DDT00:00:00-00:00')
Got below error
Both source pattern and target pattern are wrong in your query. See SimpleDateFormat for reference. Also initial string does not contain the timezone and it is not clear how are you going to derive it as -04:00. In such case it will be UTC timezone used, you can convert to other timezone using from_utc_timestamp.
Timestamp string conversion demo:
select from_unixtime(unix_timestamp('12/15/2019 21:18','MM/dd/yyyy HH:mm'),"yyyy-MM-dd'T'HH:mm:ss.SSSZ")
Result:
2019-12-15T21:18:00.000+0000

Date format in where clause match date format from table

I'm using previously written scripts that have the date format in the WHERE clause as 'DD-MMM-YY', however, in the table it is formatted as 'DD-MMM-YY HH.MM.SS.000000000 AM/PM. Does it matter that these formats do not match up? After comparing results it doesn't seem like any data is missing using non-matching formats. Wasn't sure if efficiency would be different if they matched or didn't match? Just let me know your thoughts and opinions. Thanks all!
Your where clause is fine. The table formatting is just a representation of the date. You can change this if it helps you view the results more easily (see link below), but it will make no difference to the efficiency of the query.
How can I set a custom date time format in Oracle SQL Developer?

Is ISO8601 the best date-format for PostgreSQL jsonb when i want to filter by the date?

I'm new to PostgreSQL and I have the following question:
I have a table with just an id-column and a data-column, which uses the jsonb-type. Inside the jsonb-object I have a datetime field. I read in various posts, that I should use the ISO-8601 dateformat to store in the DB.
I want to filter my table by date like this:
SELECT * FROM table WHERE data->'date' > '2016-01-01T00:00'
Is this really the best date-format for this purpose?
Thanks in advance :)
IMHO Your query should produce
ERROR: operator does not exist: jsonb > timestamp with time zone
If I get it right. In case you change -> to ->> it should get a text value instead of jsonb field (which is also not comparable to timestamp).
It should be smth like
SELECT * FROM table WHERE (data->>'date')::timestamptz > '2016-01-01T00:00' to work
The big advantage of that format is that string order corresponds to date order, so a comparison like the one you quote in your question would actually work as intended.
A second advantage is that a timestamp in that format can easily be converted to a PostgreSQL timestamp with time zone value, because the type input function understands this format.
I hope you are not dealing with dates “before Christ”, because it wouldn't work so easily with those.

convert string date(yyyy/mm/dd) to date format in db2

I am saving the date as varchar in db2 with yyyy/mm/dd format i need to convert it to date datetype in the query how to achieve this?
I tried
select DATE(CRDTR2) from ASAODLIB.SSLR204 where CRDTR2 BETWEEN
'2015/03/01' AND '2015/03/31';
query and got the below error
The syntax of the string representation of a datetime value is
incorrect.. SQLCODE=-180, SQLSTATE=22007, DRIVER=3.68.61
can someone help me.
If your DB2 version is new enough, use to_date: select DATE(TO_DATE(CRDTR2, 'YYYY/MM/DD')) from ...
...because it doesn't recognize that format. I'd turn it into *ISO first, via REPLACE:
SELECT DATE(REPLACE(CRDTR2, '/', '-'))
FROM ASAODLIB.SSLR204
WHERE CRDTR2 BETWEEN '2015/03/01' AND '2015/03/31'
Incidentally, there's a couple other things here.
You should be storing dates as an actual date type, which would make this a non-issue.
You shouldn't use BETWEEN, in preference for an exclusive upper-bound (< - the blog talks about SQL Server, but the problem is really due to representation. That, and most versions of DB2 allow you to specify fractional seconds in timestamps...).