Hive queries with dates - date

I have a little problem, I would like to filter a date with hive query but the output is empty. My column is string type
I tried this :
select * from my_table
where to_date(date) < to_date('01/08/19 00:00:00')
The format of my column date is 01/08/19 18:00:00

Dates in this format '01/08/19 00:00:00' are not in comparable format because in such format '02/08/19 00:00:00' is greater than '01/08/20 00:00:00'.
Use unix_timestamp and from_unixtime to convert to the comparable format ('yyyy-MM-dd HH:mm:ss'), then compare with date in the same format.
select * from my_table
where from_unixtime(unix_timestamp(date,'dd/MM/yy HH:mm:ss'),'yyyy-MM-dd HH:mm:ss') < '2019-08-01 00:00:00'

Related

cast column to_date with multiple date formats in original text column in postgresql

I have a table with a event_date text column with values following one of two date formats:
'YYYYMMDD'
'YYYY-MM-DD'
For example:
event_date: text
1991-04-01
2009-02-11
20010101
NULL
20020101
How might I parse that column into Date format?
Especially considering that TO_DATE() will take only one possible format
Both formats would be converted to dates with:
SELECT event_date::date event_date
FROM tablename;
Or use a CASE expression to choose one of the two formats:
SELECT TO_DATE(
event_date,
CASE
WHEN event_date LIKE '____-__-__' THEN 'YYYY-MM-DD'
WHEN event_date LIKE '________' THEN 'YYYYMMDD'
END
) event_date
FROM tablename;
See the demo.

Create date column from year and doy column

Is there a way to create a date column combining one column having the year as string and one column containing a date-of-year (doy) as integer?
I am aware of methods like SELECT EXTRACT(DOW FROM TIMESTAMP '2001-02-16 20:38:40'); or SELECT to_char(date_trunc('year', now()) + interval '169 days', 'MM/DD') but when trying to replace the "hard coded" stings with the columns I always get some kind of an error.
SELECT s.id, s.year, s.doy,
((s.year||'-01-01')::date + (s.doy||' days')::interval )::date AS date
FROM table_name AS s
the (s.year||'-01-01') or (s.doy||' days') concats the column value with a required string and the ::date or ::interval changes the resulting string type
You can use the make_date() function and add the number of days directly because date + integer is a valid operation:
select make_date(s.year, 1, 1) + s.doy as date
from ...

Postgres add values in timestamp

How could I go about a query that where all parts of the timestamp excluding the year add up to 50,
for example in my database testdb I have my column with a string timestamp, in that column I have my timestamp strings for example,
2016-03-01 07:13:21
2019-07-03 04:04:49
etc so they are separated by the -
so when I query my database, Select timestamp from testdb where.... = 50 I want to see where the individual timestamp strings from all the rows that add up to 50
remove the year, and then add up the day,month,hour,minute and the second and then if that amounts to 50 it will just display those rows that meet the criteria
So you first need to convert your string to a timestamp using e.g. to_timestamp(the_column, 'yyyy-mm-dd hh24:mi:ss')
From there you can extract each part of the timestamp using extract:
select *
from the_table
where extract(month from to_timestamp(the_column, 'yyyy-mm-dd hh24:mi:ss')
+ extract(day from to_timestamp(the_column, 'yyyy-mm-dd hh24:mi:ss')
+ extract(hour from to_timestamp(the_column, 'yyyy-mm-dd hh24:mi:ss')
+ extract(minute from to_timestamp(the_column, 'yyyy-mm-dd hh24:mi:ss')
+ extract(second from to_timestamp(the_column, 'yyyy-mm-dd hh24:mi:ss') = 50
It's a really bad idea to store timestamp values in text (or varchar) columns. They should be stored in columns defined as timestamp or better timestamptz

How to convert date format into milliseconds in postgresql?

I have date in postgresql in format "17/12/2011".
How can i convert it into milliseconds using select clause of postgreql ?
Currently i am just executing select clause as
select tableDate,tableSales
from table_name
I want to have something like when I select tableDate it should be converted into milliseconds using some postgresql functions.
tableDate DATE
tableSales Numeric
extract(epoch from ...) will return the number of seconds since 1970-01-01 00:00:00 so all you need to do is to multiply that by 1000:
select extract(epoch from tableDate) * 1000, tableSales
from table_name
More details in the manual:
http://www.postgresql.org/docs/current/static/functions-datetime.html#FUNCTIONS-DATETIME-EXTRACT

Postgres where clause compare timestamp

I have a table where column is of datatype timestamp
Which contains records multiple records for a day
I want to select all rows corresponding to day
How do I do it?
Assuming you actually mean timestamp because there is no datetime in Postgres
Cast the timestamp column to a date, that will remove the time part:
select *
from the_table
where the_timestamp_column::date = date '2015-07-15';
This will return all rows from July, 15th.
Note that the above will not use an index on the_timestamp_column. If performance is critical, you need to either create an index on that expression or use a range condition:
select *
from the_table
where the_timestamp_column >= timestamp '2015-07-15 00:00:00'
and the_timestamp_column < timestamp '2015-07-16 00:00:00';