I am using the following query to change all date to the Monday of the corresponding week:
select date_trunc('week', join_date) as join_wk from my_table
This query converts 2017-08-23 11:30:02 to 2017-08-21 00:00:00
I am wondering if it is possible to remove the hour/min/secondfrom the output 2017-08-21 00:00:00? i.e. make the output in the format of 2017-08-21
date_trunc returns a timestamp. You could cast it to a date to lose the time part of it:
SELECT DATE_TRUNC('week', join_date)::DATE AS join_wk FROM my_table
-- Here ----------------------------^
Related
I have the following field in my table:
VISIT_DATE TIMESTAMP(0)
And it is holding time like that:
2022-01-13 11:04:15
Could someone tell me is it possible to cut off that seconds? I really don't need them.
I want to hold time in the following format:
2022-01-13 11:04
Either truncate the timestamp by minutes using date_trunc, which will return a timestamp without seconds, or use to_char if it is only about formatting the output:
SELECT date_trunc('minute',VISIT_DATE) FROM t;
SELECT to_char(VISIT_DATE,'yyyy-mm-dd hh24:mi') FROM t;
Demo:
Using date_trunc (result as timestamp):
SELECT date_trunc('minute','2022-01-13 11:04:15'::timestamp);
date_trunc
---------------------
2022-01-13 11:04:00
(1 Zeile)
Using to_char (result as text):
SELECT to_char('2022-01-13 11:04:15'::timestamp,'yyyy-mm-dd hh24:mi');
to_char
------------------
2022-01-13 11:04
I have this timestamp metric which shows the following information:
2021-08-30 22:10:22.838 UTC
I would like to split and group this info by date and hour, so it should look something like this in BQ:
Date: 2021-08-30
Hour: 22:00:00 UTC
Anyone know how do do this?
Thanks!!
To extract the date and hour you can use this (replacing your_ts with the appropriate field name).
SELECT
EXTRACT(DATE FROM your_ts) dt,
EXTRACT(HOUR FROM your_ts) hr
FROM tbl
If you want to keep the formatting you provided (returning strings), you can try something like this.
SELECT
FORMAT_TIMESTAMP("%F", your_ts) dt,
FORMAT_TIMESTAMP("%X", TIMESTAMP_TRUNC(your_ts, HOUR))
FROM tbl
I run this query on redshift to add 1 momth to a specific timestamp that:
SELECT
DATEADD(month, 1, date '2021-08-12 18:37:19') AS date_interval;
The result is this:
2021-09-12 00:00:00.0
How do I need to modify the query so the result also comes with the correct time looking like this:
2021-09-12 18:37:19
If you want to do algebra on a timestamp, then you should start with a literal timestamp, not a date:
SELECT
DATEADD(month, 1, '2021-08-12 18:37:19') AS date_interval;
The problem with your current query is that the following is a date literal:
date '2021-08-12 18:37:19'
Hence, the time component of your timestamp will be "zeroed" out to midnight before you even add one month.
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.
I have a table where I store the times as varchars:
Times
starttime
00:00
16:00
22:00
From this table I can convert the column to a date like this:
Select to_date(starttime,'hh24:mi') from times
This gives me the following:
01/03/2013 00:00:00
01/03/2013 16:00:00
01/03/2013 22:00:00
How can I change this query so I can prefix the time values with today's date so I get the following instead: (16/03/2013 is today's date)
16/03/2013 00:00:00
16/03/2013 16:00:00
16/03/2013 22:00:00
Thanks
to_date(to_char(sysdate, 'dd.mm.yyyy')||' '||starttime, 'dd.mm.yyyy hh24:mi')
You can add the difference between the current date and the start of the month. I prefer this to string operations as you stick with dates, but it doesn't make much difference.
You can use TRUNC() to work it out:
select to_date('09:00','hh24:mi') + ( trunc(sysdate) - trunc(sysdate, 'mm'))
from dual
SQL Fiddle
trunc(sysdate) is the earliest today and trunc(sysdate, 'mm') is the beginning of the month. Oracle's date arithmetic means that it returns a day difference between today and the beginning of the month; giving you the difference you need to add to your original TO_DATE().