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.
Related
My table has its dates in string format. I'm trying to transform them to date, but BQ is not accepting some values and I can't find the reason.
What I've tried:
PARSE_TIMESTAMP('%Y%m%d', CLIENTE.dtnasc)
PARSE_TIMESTAMP('%Y%m%d', cast( CLIENTE.dtnasc as string))
CAST(CLIENTE.dtnasc AS DATE)
Error example (it's not only this value):
Failed to parse input string "1991-02-11 00:00:00"
How the date is without transfomartion:
2000-01-01 00:00:00 (string)
Try this:
SELECT PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', '1991-02-11 00:00:00')
SELECT PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', CLIENTE.dtnasc)
FROM mytable
Consider below option (the simplest I can think of, having that your string column matches already format of timestamp - 2000-01-01 00:00:00 (string))
SELECT DATE(TIMESTAMP(CLIENTE.dtnasc))
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
I have to select the value 2019-03-25 from a date column of a table but in the following format:
2019-03-25T00:00:00.000+02:00
Hon can I get it?
Oracle 10g
Thanks!
The date datatype does not store milliseconds and timezone information, so I undertand your question as how to format a date to the target forma, with fixed values for milliseconds and timezone.
If so, you can use to_char() like so:
to_char(mycol, 'yyyy-mm-dd"T"hh24:mi:ss".000+2:00"')
You can CAST your DATE to a TIMESTAMP and then use FROM_TZ to set the time zone and then format it to your requirements using TO_CHAR:
SELECT TO_CHAR(
FROM_TZ( CAST( your_date AS TIMESTAMP ), '+02:00' ),
'YYYY-MM-DD"T"HH24:MI:SS.FF3TZH:TZM'
) AS formatted_date
FROM your_table;
Which, for your sample data:
CREATE TABLE your_table ( your_date ) AS
SELECT DATE '2019-03-25' FROM DUAL
Outputs:
| FORMATTED_DATE |
| :---------------------------- |
| 2019-03-25T00:00:00.000+02:00 |
db<>fiddle here
We can not store timezone information in db that's why we can simply use below format,
select to_char(sysdate,'YYYY-MM-DD')||'T'||to_char(sysdate,'HH24:MI:SS') from dual
I have a column in Athena with Timestamp Data Type and format is: 2019-08-28 00:00:00.000
How to format it to Date format using SQL to be:
DD-MON-YYYY
Thanks.
WITH test AS (
SELECT '2019-08-28 00:00:00.000' AS str
)
SELECT format_datetime(cast(str AS timestamp), 'dd-MM-YYYY')
FROM test
Result:
_col0
1 28-08-2019
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 ----------------------------^