I have column in Athena table as time -> string
I am trying to convert to datetime as below :
SELECT
"source"
, "account"
, CAST(time as date) Datetime
FROM
"testdata"
It gives me below error:
INVALID_CAST_ARGUMENT: Value cannot be cast to date: 2021-11-28T08:04:21Z
Your timestamp looks like it's formatted using ISO 8601, and Athena has a function for that: from_iso8601_timestamp
SELECT
source,
account,
from_iso8601_timestamp("time") AS datetime
FROM testdata
If you only want the date part you can cast the result of the function to date:
SELECT
source,
account,
CAST(from_iso8601_timestamp("time") AS DATE) AS "date"
FROM testdata
Use DATE_PARSE:
SELECT source, account,
DATE_PARSE(time, '%Y-%m-%dT%H:%i:%sZ') AS Datetime
FROM testdata;
Related
I want to convert below string to only date column. the column has both time and date stored as string
String
2021-01-01 12:43:58 ==> 2021-01-01
You achieve what you want with this query:
SELECT
cast(date_parse(column, '%Y-%m-%d') as date)
FROM
table
You can use date_format with date_parse or cast as date to achieve this:
SELECT date_format(date_parse('2021-01-01 12:43:58', '%Y-%m-%d %h:%i:%s'),'%Y-%m-%d')
will give 2021-01-01
Just replace the timestamp with column name
SELECT date_format(date_parse(<timestamp_column>, '%Y-%m-%d %h:%i:%s'),'%Y-%m-%d')
Now using cast as shown below
SELECT cast(date_parse('2021-01-01 12:43:58', '%Y-%m-%d %h:%i:%s') as date)
similarly replace timestamp with column name
For what is worth, here's a solution without casting back and forth from string to date:
select substr(column,1,10) from mytable
I have a PowerBI report connected to Azure SQL Direct Query Mode (Advanced Option - where we can write SQL Statements).
I got date column in SQL DB (date type), but PowerBI is converting this to datetime, even it’s in date type in SQL DB. Now I cannot transform this column in PowerBI as its Direct Query.
However, if I connect directly to table (Direct Query mode only, but not advanced option) it takes date column as date.
Sample Date values:
created_date
2002-02-19
2002-02-19
2002-02-19
2002-02-19
2002-02-19
Converted Date values by PowerBI
created_date
19-02-2002 00:00:00
19-02-2002 00:00:00
19-02-2002 00:00:00
19-02-2002 00:00:00
19-02-2002 00:00:00
Any inputs on this will be highly appreciated.
Thank you,
That's a known problem, even if you use CAST or CONVERT to force the datatype in the query PowerBI will format it as a DateTime.
You can change it thought, from the report view:
select the field (in the field bar)
open the "Modeling" section in the top bar, here you will be able to change the data type and formatting of the field to Short Date
you could just select the column and go to 'column tools' ribbon and change the format. to dd-mm-yyyy
Use the DATEVALUE() function to convert a date to a datetime. This works with a direct query as well.
Question is old but I ran into this today. I solved this by creating a SQL view, cast datetime to date in the view then used DirectQuery without advanced option to grab the view.
I am looking to convert the following string: mmm-dd-yyyy to a date: yyyy-mm-dd
e.g
Nov-06-2015 to 2015-11-06
within Amazon Athena
I would do date_parse. Adjust your regex accordingly.
select date_parse('Nov-06-2015','%b-%d-%Y')
2015-11-06 00:00:00.000
refd:https://prestodb.io/docs/current/functions/datetime.html
You can also use cast function to get desire output as date type.
select cast(date_parse('Nov-06-2015','%M-%d-%Y') as date);
output--2015-11-06
in amazon athena
https://prestodb.io/docs/current/functions/datetime.html
used date parse to parse string and cast to convert 2015-11-06 00:00:00.000
into 2015-11-06
I have a date string in the format,
"dd/mm/yyyy"
e.g="23/2/2017"
How can I convert this to a valid format, so that I can save this value in Datetime field of postgresql.
I tried using datetime package, but not getting.
in Postgres you can "adjust" your datestyle, eg:
t=# set datestyle TO DMY;
SET
Time: 0.215 ms
t=# select '23/2/2017'::timestamptz;
timestamptz
------------------------
2017-02-23 00:00:00+00
(1 row)
or just "parse" with right mask (ignoring not suitable datestyle), eg:
t=# select to_timestamp('23/2/2017','DD/MM/YYYY');
to_timestamp
------------------------
2017-02-23 00:00:00+00
(1 row)
ALTER TABLE <tablename> ALTER COLUMN <columnname> TYPE DATE using to_date(<columnname>, 'DD-MM-YYYY');
Just wondering how I can alter the following query to show date in the format I want. I am using SAS to pull this data.
Existing Date format: 15MAR2011:09:05:16.000000
Format I want: 15MAR2011:09:05:16
Query I am using:
proc sql;
create table data.test as
select * from connection to odbc
(
select ID,
DATE AS CREATION_DATE,
from maintable
);
quit;
A format affects how SAS displays a variable value. It does not affect the actual value itself.
So, assuming the variable CREATION_DATE is a datetime value, just assign it a format of DATETIME20. to display is as you want:
proc sql;
create table data.test as
select ID, CREATION_DATE format=datetime20.
from connection to odbc
( select ID, DATE AS CREATION_DATE
from maintable );
quit;
However, some ODBC interfaces will return your date column as a character string, so you need to be sure it is showing up on the SAS side as a proper datetime value.