convert a date string to datetime compatible to postgresql - django

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

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;

Athena - String to Date coversion

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

Trim milliseconds part in Informatica

I have value in datetime format 2021-03-08 00:00:00.0 How to remove the milliseconds part from it?
Like: 2021-03-08 00:00:00.0 to 2021-03-08 00:00:00
if your target is a table/file and column is in datetime format, pls use this -
TO_DATE(TO_CHAR(dttm_col,'YYYY-MM-DD HH24:MI:SS'),'YYYY-MM-DD HH24:MI:SS')
if your target is a table/file and column is in string format, pls use this -
TO_CHAR(dttm_col,'YYYY-MM-DD HH24:MI:SS')
if your target is a file, its better to use string format. if its a table, you need to handle in database side.

Power BI is converting date to datetime - DirectQuery (Advanced Option)

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.

datetime and date in same column

I have a question. I have a field in my SAS dataset that has a mixture of datetime and date variables.
The field is PST_DT and is Type: Numeric. Group: Date. Format: Date9. Length: 8.
Some values look like this:
PST_DT
8/22/2018 11:59:59 PM
8/22/2018
How can I turn just the datetime values in date format? I want all the values to be in date format.
Thanks.
The question does not make much sense as a date variable (number of days since 1960) cannot have a time component at all, much less have it selectively.
If you have a DATETIME value (number of seconds since 1960) and want to convert it to a date value use the datepart() function. And attach a date format so that the value displays in a human friendly way.
pst_date = datepart(pst_dt);
format pst_date yymmdd10. ;
If you have instead a character string then use the ANYDTDTE. informat to convert it to a date value.
pst_date = input(pst_dt,anydtdte40.);
format pst_date yymmdd10. ;