is there an alternative to the to_char() function in impala? I want to set a timestamp field where the date and minutes are fixed and only showing the hours, but can't seem to find an alternative.
This is my existing code in postgres which I need to convert to impala.
select to_char(starttime, '1900-01-01 HH24:00:00')::timestamp
Any help is appreciated!
In Impala, you can use
SELECT hours_add('1900-01-01', hour(now()));
to get the same result
function hour extracts the hour part from a timestamp
function hours_add adds hour to a timestamp
Related
I am trying to calculate the average of ride_length (hh:mm:ss format) on Big Query.
1
For example, I am aiming for a result that says 1 instead of 01:00:00.
I tried using the CAST function to convert it into an integer but it didn't work. I also tried following the steps of answers to questions similar to mine but they didn't work as well.
You can use Extract
function which will return the specified part of the time expressions. You can try the below queries.
To extract hour from the TIME expression you can consider the below query.
Query:
SELECT EXTRACT(HOUR FROM TIME "01:00:00") as hour;
Output:
To extract hour from TIMESTAMP expression below query can be considered.
Query:
select EXTRACT(hour from timestamp "2023-01-13 15:30:00" AT TIME ZONE "America/Los_Angeles") as hour
Output:
To extract hour from the TIME column you can consider the below sample query.
Query:
SELECT time,EXTRACT(HOUR FROM (time)) AS hour from `bigquery-public-data.austin_incidents.incidents_2008` limit 10
Output:
I have Oracle SQL query where it has been used TRUNC(04-Aug-2017 15:35:32)
What will be parameter in Hive to replace TRUNC?
Assuming you have a date/time, you can use the to_date() function:
select to_date(col)
If you have a timestamp, say ts, you can use trunc():
trunc(ts, 'day')
This returns a timestamp, with the time portion stripped off - which is similar to what trunc() does in Oracle when given one argument only.
On the other hand, you can also convert the timestamp to a date:
to_date(ts)
This returns a date rather than a timestamp: that's a different datatype, that has no time component (Oracle does not have such a datatype: both date and timestamp store the date and time).
As per Oracle docs, The TRUNC (date) function returns date with the time portion of the day truncated to the unit specified by the format model fmt. The value returned is always of datatype DATE, even if you specify a different datetime datatype for date. If you omit fmt, then date is truncated to the nearest day.
Similar is the function of to_date function in Hive.
It returns the date part of a timestamp string (pre-Hive 2.1.0): to_date("1970-01-01 00:00:00") = "1970-01-01".
If what you want is the timestamp(midnight timestamp : 00:00:00) along with the truncated date, you need to use some conversions as shown below:
cast(from_unixtime(unix_timestamp(to_date(<YOU_DATE_COL>), 'yyyy-MM-dd')) as timestamp)
I would like to extract the date & hour from UTC time from the below table in bigquery. I have used timestamp for getting the date or time using the below code. I would like to apply the code for the entire column. How to apply timestamp for the entire column? Can you please assist with it?
SELECT EXTRACT(HOUR FROM TIMESTAMP "2020-05-03 16:49:47.583494")
My data is like this
I want result like this:
You can do it this way:
SELECT my_column AS original_value,
DATE_FORMAT(STR_TO_DATE(my_column, "%Y-%m-%d %H:%i:%s.%f UTC"), "%e/%m/%Y") AS date,
DATE_FORMAT(STR_TO_DATE(my_column, "%Y-%m-%d %H:%i:%s.%f UTC"), "%l%p") AS hour
FROM my_table;
I am assuming that the column is VARCHAR, that's why I am converting it to DATE.
Output:
Demo:
You can check the demo here.
Edit:
My initial thought was that OP wanted the query for MySQL (probably BigQuery is based on that). But it turns out that BigQuery is not based on MySQL. So you can use FORMAT_TIMESTAMP in BigQuery, this is how the query would look:
SELECT Occurrence AS original_value,
FORMAT_TIMESTAMP("%e/%m/%Y", Occurrence) AS date,
FORMAT_TIMESTAMP("%l%p", Occurrence) AS hour
FROM mytable
I am trying to extract the date and time from a field in Teradata.
The field in question is:
VwNIMEventFct.EVENT_GMT_TIMESTAMP
Here is what the data look like:
01/02/2012 12:18:59.306000
I'd like the date and time only.
I have tried using EXTRACT(Date, EXTRACT(DAY_HOUR and a few others with no success.
DATE_FORMAT() does not appear to work since I'm on Teradata.
How would I select the date and time from VwNIMEventFct.EVENT_GMT_TIMESTAMP?
If the datatype of EVENT_GMT_TIMESTAMP is a TIMESTAMP, it's simple Standard SQL:
CAST(EVENT_GMT_TIMESTAMP AS DATE)
CAST(EVENT_GMT_TIMESTAMP AS TIME)
If it's a CHAR you need to apply a FORMAT, too:
CAST(CAST(EVENT_GMT_TIMESTAMP AS TIMESTAMP FORMAT 'dd/mm/yyyyBhh:mi:SS.s(6)') AS DATE)
CAST(CAST(EVENT_GMT_TIMESTAMP AS TIMESTAMP FORMAT 'dd/mm/yyyyBhh:mi:SS.s(6)') AS TIME)
Edit:
For simply changing the display format you need to add a FORMAT and a CAST to a string:
CAST(CAST(EVENT_GMT_TIMESTAMP AS FORMAT 'YYYYMMDDHHMI') AS CHAR(12))
or
CAST(CAST(EVENT_GMT_TIMESTAMP AS FORMAT 'YYYYMMDDHHMISS') AS CHAR(14))
If you don't care about display, just want to truncate the seconds:
EVENT_GMT_TIMESTAMP - (EXTRACT(SECOND FROM EVENT_GMT_TIMESTAMP) * INTERVAL '1.000000' SECOND)
Working with timestamps is a bit tricky :-)
I know this is an old topic, but I've struggled with this too. Try:
CAST(EVENT_GMT_TIMESTAMP AS TIMESTAMP(0))
The result will be
01/02/2012 12:18:59
The datatype will still be timestamp, but it will just be the date and time with no microseconds (looks just like a datetime object in Microsoft SQL).
How to get the date and time only up to minutes, not seconds, from timestamp in PostgreSQL. I need date as well as time.
For example:
2000-12-16 12:21:13-05
From this I need
2000-12-16 12:21 (no seconds and milliseconds only date and time in hours and minutes)
From a timestamp with time zone field, say update_time, how do I get date as well as time like above using PostgreSQL select query.
Please help me.
There are plenty of date-time functions available with postgresql:
See the list here
http://www.postgresql.org/docs/9.1/static/functions-datetime.html
e.g.
SELECT EXTRACT(DAY FROM TIMESTAMP '2001-02-16 20:38:40');
Result: 16
For formatting you can use these:
http://www.postgresql.org/docs/9.1/static/functions-formatting.html
e.g.
select to_char(current_timestamp, 'YYYY-MM-DD HH24:MI') ...
To get the date from a timestamp (or timestamptz) a simple cast is fastest:
SELECT now()::date
You get the date according to your local time zone either way.
If you want text in a certain format, go with to_char() like #davek provided.
If you want to truncate (round down) the value of a timestamp to a unit of time, use date_trunc():
SELECT date_trunc('minute', now());
This should be enough:
select now()::date, now()::time
, pg_typeof(now()), pg_typeof(now()::date), pg_typeof(now()::time)