how to round timestamp to day in HIVE? - sql

I have a string value like '2020-10-01T02:02:50.918+03:00'. How can I get value like this: 2020-10-01 00:00:00.000 in timestamp datatype in Hive?

Use substr to get yyyy-MM-dd, then use timestamp construct.
Demo:
select timestamp(substr('2020-10-01T02:02:50.918+03:00',1,10))
Result:
2020-10-01 00:00:00.0

Related

How to deal with Snowflake to_date cast issue with multiple date formats in same column?

The query in snowflake,
select date_column, try_to_date(date_column)
from tablename;
tends to mess up the intended dates as shown below:
01-NOV-18 ____________ 0018-11-01 (desired output 2018-11-01)
09-JAN-19 ____________ 0019-01-09
2018-11-03 20:44:54 __ 2018-11-03
2018-09-03 00:00:00 __ 2018-09-03
2018-08-22 19:38:41 __ 2018-08-22
This is similar to Snowsql two digit century start date cast issue, but with multiple date formats in the input column.
You could use a coalesce with the try_to_date. The key is to specifically put a date format in the try_to_date function so that it returns null if it can't convert a date that doesn't match the format. When the date doesn't match the format it'll fall back to the next method you specify and you can continue until you covered all your different date formats. Try something like:
select
date_column,
coalesce(try_to_date(date_column, 'YYYY-MM-DD HH:MI:SS'), try_to_date(date_column, 'DD-MON-YY'))
from tablename;
This returns:
+-------------------+----------------------------------------------------------------------------------------------+
|INPUT_DATE |COALESCE(TRY_TO_DATE(INPUT_DATE, 'YYYY-MM-DD HH:MI:SS'), TRY_TO_DATE(INPUT_DATE, 'DD-MON-YY'))|
+-------------------+----------------------------------------------------------------------------------------------+
|01-NOV-18 |2018-11-01 |
|09-JAN-19 |2019-01-09 |
|2018-11-03 20:44:54|2018-11-03 |
|2018-09-03 00:00:00|2018-09-03 |
|2018-08-22 19:38:41|2018-08-22 |
+-------------------+----------------------------------------------------------------------------------------------+

convert date unix to timestamp

I want to convert type date unix to timestamp in Informix.
My column date1 contains values as 1598915961, 1598911249, 1598911255...
expected output: 2020-02-13 15:00:00
How should I do it, please?
In Informix, you can use dbinfo() and 'utc_to_datetime':
select dbinfo('utc_to_datetime', myepoch)
The idea is that you can add the seconds to the date '1970-01-01'. I don't have Informix on-hand, but the syntax is something like this:
select datetime('1970-01-01') + interval date1 second

Change Date Format from an array in SQL SELECT Statement

I have a column updated_at that returns an array
["2019-01-05T17:28:32.506-05:00","2019-06-15T13:22:02.625-04:00"]
But I want the output date format like this 2019-01-03.
How can I accomplish this in sql databricks?
Thanks!
Try unnest and cast that as a date:
with ts_array as
(select array['2019-01-05T17:28:32.506-05:00','2019-06-15T13:22:02.625-04:00'] as tsa)
select unnest(tsa)::date from ts_array ;
You can use "date_trunc" SQL function to get the output in date format.
date_trunc(fmt, ts) - Returns timestamp ts truncated to the unit specified by the format model fmt. fmt should be one of [“YEAR”, “YYYY”, “YY”, “MON”, “MONTH”, “MM”, “DAY”, “DD”, “HOUR”, “MINUTE”, “SECOND”, “WEEK”, “QUARTER”]
Examples:
> SELECT date_trunc('YEAR', '2015-03-05T09:32:05.359');
2015-01-01 00:00:00
> SELECT date_trunc('MM', '2015-03-05T09:32:05.359');
2015-03-01 00:00:00
> SELECT date_trunc('DD', '2015-03-05T09:32:05.359');
2015-03-05 00:00:00
> SELECT date_trunc('HOUR', '2015-03-05T09:32:05.359');
2015-03-05 09:00:00
Reference: Databricks - SQL Functions.
Hope this helps.

Sanitize a string column with a mix of datetime and timestamps

I have a column on BigQuery of String datatype that has a mix of date and timestamps as strings.
https://www.evernote.com/l/AmOc0thoaMRLJ7y1IFnLMxwLAeREujUtGRc
Tried SAFE_CAST, DATE_PARSE but neither work.
I want to be able to query this column uniformly as timestamps.
I want to be able to query this column uniformly as timestamps.
Below example for BigQuery Standard SQL
#standardSQL
SELECT close_date,
COALESCE(
TIMESTAMP_MILLIS(SAFE_CAST(close_date AS INT64)),
SAFE.PARSE_TIMESTAMP('%m/%d/%y', close_date)
) AS close_date_as_timestamp
FROM `project.dataset.table`
If to apply to your sample data - result is
Row close_date close_date_as_timestamp
1 1556064000000 2019-04-24 00:00:00 UTC
2 01/24/19 2019-01-24 00:00:00 UTC
3 1548892800000 2019-01-31 00:00:00 UTC
4 11/27/18 2018-11-27 00:00:00 UTC
Note: You can add to COALESCE as many different patterns as you expect in your data
For example you can add below to support 2019-01-01
SAFE.PARSE_TIMESTAMP('%Y-%m-%d', close_date)
And so on ...

BigQuery Google timezone conversion

When I try running below query I get timestamp in UTC timezone.
select current_timestamp from table;
Can you please help me to convert timestamp to get in EST timezone.
Thanks
SELECT TIMESTAMP(DATETIME(CURRENT_TIMESTAMP), 'America/New_York')
The trick here is in converting TIMESTAMP to DATETIME which is timezone-less to represent timestamp as a just date/time, then convert back to TIMESTAMP but now specifying needed timezone.
Note, BigQuery still will show it as UTC but timestamp value itself will represent value in respective timezone
Try this instead:
SELECT STRING(CURRENT_TIMESTAMP, 'America/New_York') AS current_timestamp
FROM dataset.table
This converts the timestamps to strings using the New York time zone.
For more control you can use FORMAT_TIMESTAMP (https://cloud.google.com/bigquery/docs/reference/standard-sql/timestamp_functions#format_timestamp)
Example:
SELECT FORMAT_TIMESTAMP("%F %T EST", current_timestamp, "America/New_York") AS current_timestamp_EST FROM `dataset.table` LIMIT 1;
Results:
|-------------------------|
| current_timestamp_EST |
|-------------------------|
| 2020-10-23 18:25:17 EST |
|-------------------------|