The table schema is like this:
Column | Type | Modifiers
--------+--------------------------+--------------------------------------------------
time | timestamp with time zone | default now()
The time format is like this:
time
------------------------
2016-07-11 18:58:28+00
2016-07-11 18:58:37+00
2016-07-12 00:59:31+00
How to group by date with time truncated?
I would like to see the result as:
date
------------------------
2016-07-11
2016-07-11
2016-07-12
If you want compare or group by dates instead of timestamps you can cast to DATE:
SELECT time::DATE, ... FROM ... GROUP BY time::DATE;
or simpler
SELECT time::DATE, ... FROM ... GROUP BY 1;
Take a look at the current Postgresql documentation for datetime functions: https://www.postgresql.org/docs/current/static/functions-datetime.html
You can use date_trunc, extract, to_char or the simplest way is to cast to date (as I would do):
SELECT time::date; // 2016-07-11 18:58:28+00 -> 2016-07-11
Cheers!
use date_trunc:
select date_trunc('day', time)
Related
I have a 'timestamp without time zone' value in a column example:
PunchIn = "2019-06-10 09:10:00"
How can I extract "2019-06-10 00:00:00" from this column?
demo:db<>fiddle
SELECT my_date::date::timestamp
This converts the date column my_date to date (cuts the time part) and if you cast it back into timestamp it gets the 0 time.
Alternatively you can use the date_trunc function:
SELECT date_trunc('day', my_date)
SELECT SUBSTRING("PunchIn = '2019-06-10 09:10:00'" FROM 11 FOR 21);
should return the string you're looking for (if I count the index number right:)
You can use the DATE function to extract date:
SELECT DATE(SUBSTRING("PunchIn = '2019-06-10 09:10:00'" FROM 11 FOR 21));
I have a dataset containing a date field in the format of MM/dd/yyyy, my goal is to create a table with the same date format but with timestamp format (at the end, there should be an option to execute date functions on that, if it is int or string date function will not work.)
Things I tried:
my column name: as_of_date
1) cast(unix_timestamp(as_of_date, "MM/dd/yyyy") as timestamp)
i/p -> 01/03/2006, o/p ->2006-01-03 00:00:00
problem - I do not want extra zeros in the output. substr is not working on the date function
2) If i keep the value as string, date functions does't work.
day('01/03/2006')
input: '01/03/2006' , output:null (but expected 3)
Can you please help me a date format that already existing or help me to create a new date format for my logic.
Try with this once
use unix_timestamp function to match your input date format then use from_unixtime function to change the output format then cast to date type.
hive> select date(from_unixtime(unix_timestamp("03/06/2018", "MM/dd/yyyy"),"yyyy-MM-dd"));
+-------------+--+
| _c0 |
+-------------+--+
| 2018-03-06 |
+-------------+--+
In Impala:
hive> select from_unixtime(unix_timestamp("03/06/2018", "MM/dd/yyyy"),"yyyy-MM-dd");
+-------------+--+
| _c0 |
+-------------+--+
| 2018-03-06 |
+-------------+--+
I'm completely new to Hive and I would really appreciate some help.
I have a date column in my table and I would like to keep month and year of this date. What I would do in excel is the following:
datenew= date(year(old_date),month(old_date),1)
my old_date is in YYYY-MM-DD format.
Thanks!!
You can use trunc with mm option (for Hive versions 1.2 and later), which will return the first of a month.
trunc(dateCol,'MM')
If trunc is not supported, use
date_add(dateCol,1-day(dateCol))
You can use TRUNC (date,fmt) which is a function returns date with the time portion of the day truncated to the unit specified by the format model fmt. it always return a value of datatype DATE,:
Example:
hive> select trunc(current_date, 'MM');
OK
2018-05-01
hive> select trunc(current_date, 'YEAR');
OK
2018-01-01
Use date_format or trunc function, or substr()+concat().
Demo:
hive> select current_date original_date,
> date_format(current_date,'yyyy-MM-01') `date_format`,
> trunc(current_date, 'MM') `trunc`,
> concat(substr(current_date,1,7),'-01') `substr`
> ;
OK
original_date date_format trunc substr
2018-05-30 2018-05-01 2018-05-01 2018-05-01
Time taken: 0.093 seconds, Fetched: 1 row(s)
My first query is:
SELECT distinct wfc_request_job_id,wfc_request_job_info,
replace(iso_cc,';',' ') as "iso_cc",to_char(wfc_request_start_ts,'yyyy-MM-dd HH:mm:ss') as ts,
sent_message_count,
(link_object_count + poi_object_count + point_address_object_count) as request_object_count
FROM wfc_request_job
where
wfc_request_job_id=173526;
This returns ts as 2015-08-16 03:08:59
Second Query:
SELECT wfc_request_job_id,wfc_request_start_ts,wfc_request_end_ts,replace(iso_cc,';',' ') as "iso_ccs",sent_message_count,wfc_queue_name
FROM wfc_request_job
where
to_char(wfc_request_start_ts,'YYYY-MM-DD') >= to_char(to_date('08/16/2015','MM/DD/YYYY'),'YYYY-MM-DD')
and to_char(wfc_request_start_ts,'YYYY-MM-DD') <= to_char(to_date('08/16/2015','MM/DD/YYYY'),'YYYY-MM-DD')
order by wfc_request_job_id desc
This returns ts of the job id mentioned above as - "2015-08-16 15:58:59.809+02"
How can I make both the queries return ts in UTC+02 - i.e. same timezone
The data type of wfc_request_start_ts is - timestamp with timezone
I changed to queries to have the format HH24:MI:SS however that did not help. Please note that the webapp using these queries will be opened in both Germany and USA.
According to postgresql manual to_char there is TZ (and OF as of v9.4) template patterns for Date/Time formatting.
Therefore in query you need to add it so
postgres=# select to_char(now(),'yyyy-MM-dd HH24:mm:ss TZ');
to_char
------------------------
2015-08-19 12:08:56 CEST
(1 row)
Also, make sure you specify timezone when converting
so instead
to_date('08/16/2015','MM/DD/YYYY')
use
TIMESTAMP WITH TIME ZONE '2015-08-16 00:00:00+02';
in second query.
So in my database i have a table with startimestamp that is "Timestamp with time zone" type
but what i want to display the timestamp without the time zone so i thought this would work
Select to_char("StartTimestamp",'YYYY/MM/DD HH24:MM:SS')from "Samples" where "ID" = 20
and a i get
"2013/08/02 14:08:04"
but the when i do it without the to_char and just call the timestamp for the same id like this
select "StartTimestamp" from "Samples" where "ID"=20
i get this which is the correct one
"2013-08-02 14:31:04-07"
I'm i missing something the to_char statement? Thanks
Change MM to MI in the minutes place
select
to_char(now(),'YYYY/MM/DD HH24:MM:SS'),
to_char(now(),'YYYY/MM/DD HH24:MI:SS');
to_char | to_char
---------------------+---------------------
2013/08/21 15:08:00 | 2013/08/21 15:51:00