Athena - String to Date coversion - amazon-athena

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

Related

INVALID_CAST_ARGUMENT: Value cannot be cast to date

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;

Convert dd-mmm-yy to yyyy-mm-dd in big query

Is there a way to convert date of type 'dd-mmm-yy' to 'yyyy-mm-dd' format in big query
eg: 01-mar-15 to 2015-03-01
Thanks!.
SELECT PARSE_DATE("%m-%b-%y", "01-mar-15") as parsed;
Output:
Row parsed
1 2015-03-01

SAS Datetime25.6 to Character YYYYMMDD

Original Value is in DATETIME25.6
I need to change it to YYYYMMDD
I am using the below in a datastep;
'Date of Birth'n = put(borrower_dob,yymmddn8.);
However it returns ******** as the value. Help!
The date part of a SAS datetime value can be output with the format B8601DN8.
From docs
B8601DNw.Format
Writes dates from datetime values by using the ISO8601 basic notation yyyymmdd.
data want;
dtnow = datetime();
format dtnow datetime20.;
put dtnow= B8601dn8. 'is the yyyymmdd rendering of ' dtnow=;
yyyymmddstring = put (dtnow, B8601DN8.);
run;
----- LOG -----
dtnow=20191003 is the yyyymmdd rendering of dtnow=03OCT2019:10:02:14
You need just the date part(days) of the date time(seconds)variable. Check the documentation for explanation of SAS date and date-time variables.
'Date of Birth'n = put(DATEPART(borrower_dob),yymmddn8.);

convert a date string to datetime compatible to postgresql

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');

Regular expression on Dates in Oracle

I have date formats in all the possible permutations. MM/DD/YYYY, M/D/YYYY, MM/D/YYYY, M/DD/YYYY
Now I need to write a regular expression in Oracle DB to fetch different date formats from 1 column as is
Try this one:
with t(date_col) as (
select '01/01/2014' from dual
union all
select '1/2/2014' from dual
union all
select '01/3/2014' from dual
union all
select '1/04/2014' from dual
union all
select '11/1/14' from dual)
select date_col,
case
when regexp_instr(date_col, '^\d/\d/\d{4}$') = 1 then
'd/m/yyyy'
when regexp_instr(date_col, '^\d{2}/\d/\d{4}$') = 1 then
'dd/m/yyyy'
when regexp_instr(date_col, '^\d/\d{2}/\d{4}$') = 1 then
'd/mm/yyyy'
when regexp_instr(date_col, '^\d{2}/\d{2}/\d{4}$') = 1 then
'dd/mm/yyyy'
else
'Unknown format'
end date_format
from t;
DATE_COL DATE_FORMAT
---------- --------------
01/01/2014 dd/mm/yyyy
1/2/2014 d/m/yyyy
01/3/2014 dd/m/yyyy
1/04/2014 d/mm/yyyy
11/1/14 Unknown format
I am not sure what your goal is, but since months are always first, followed by day, you can use the following expression to get a date regardless of the input format:
select to_date( column, 'mm/dd/yyyy') from ...
You can select all records for which the following is true:
where [column_value] != to_char(to_date([column_value],'MM/DD/YYYY'),'MM/DD/YYYY')