Converting varchar to date in postgresql (date_parse doesn't work) - postgresql

Running this existing query in presto:
date(date_parse(activation_date, '%%m-%%d-%%Y')) from table1
gives the error
"Invalid format: "02/06/2022""
Activation_date is varchar, showing MM/DD/YYYY
How do I convert it to a date so that I can join it to a column that is already in postgresql date type? Thank you so much!

The expected format is %m/%d/%Y in Trino (formerly PrestoSQL).
trino> SELECT date(date_parse('02/06/2022', '%m/%d/%Y'));
_col0
------------
2022-02-06
https://trino.io/docs/current/functions/datetime.html?highlight=date_parse#mysql-date-functions

date_parse is not a Postgres function.
You don't need to escape % in Postgres strings.
Your format has - while your string has /.
If your DateStyle is set to MDY (Month Day Year), simply cast the string to a date.
# SELECT current_setting('datestyle');
current_setting
-----------------
ISO, MDY
# select '02/06/2022'::date;
date
------------
2022-02-06
See Date/Time Input for more.

Related

How to extract Year and Month in SQL Postgres by using Data_Part function

I am facing an issue extracting the month and year from the data (in Character varying type) >> InvoiceDate in SQL Postgres. I have seen the solution is relatively easy with MySQL function: DATEFROMPARTS as per the below Code which is not available in SQLpostgres. How can I get the same result DATA_PART function in Postgres SQL, but simultaneously I need to change the data type of the column "InvoiceDate" to the date
Select
CustomerID,
min(InvoiceDate) first_purchase_date,
DATEFROMPARTS(year(min(InvoiceDate)), month(min(InvoiceDate)), 1) Cohort_Date
into #cohort
from #online_retail_main
group by CustomerID
The output:
Customer ID| first_purchase_date |Cohort_Date|
-----------+-------------------------+-----------+
12345 | 2010-12-20 15:47:00:00 | 2010-12-01|
I am trying to make a date consits of Year and Month , while the day to be set as 1 for all
Assuming a valid Postgres timestamp:
select date_trunc('month', '2010-12-20 15:47:00.00'::timestamp)::date;
date_trunc
------------
12/01/2010
--or ISO format
set datestyle = 'ISO,MDY';
select date_trunc('month', '2010-12-20 15:47:00.00'::timestamp)::date;
date_trunc
------------
2010-12-01
Uses date_trunc to truncate the timestamp to a month which means the first of the month. Then cast(::date) to a date type. The DateStyle just deals with how the value is presented to the user. The value is not stored formatted.
To do something similar to what you did in MySQL:
select make_date(extract(year from '2010-12-20 15:47:00.00'::timestamp)::integer, extract(month from '2010-12-20 15:47:00.00'::timestamp)::integer, 1);
This uses make_date from here Date/time functions and extract to build a date.

Timestamp with timezone in PostgreSQL 9.6

I am currently searching my transaction table using the following query. The second TO_TIMESTAMP is specified as 2019-08-21..
SELECT t.* FROM Transaction t WHERE t.datetime >= TO_TIMESTAMP('2019-01-01T07:54:34','YYYY-MM-ddTHH:MI:SS')
AND t.datetime < TO_TIMESTAMP('2019-08-21T14:38:34','YYYY-MM-ddTHH:MI:SS') AND (t.location_1 = 2001 OR t.location_2 = 2001);
It returns me the following result:
When i change the second TO_TIMESTAMP to 2019-08-22.. It returns me the current day's result. I am not sure why I need to add one more day in order to retrieve the current day's result..
The current timezone in the PostgreSQL 9.6 is:
This is your problem:
SELECT TO_TIMESTAMP('2019-08-21T14:38:34','YYYY-MM-ddTHH:MI:SS');
to_timestamp
------------------------
2019-08-21 00:38:34+00
(1 row)
The unescaped T confuses the parser (the actual result may even be buggy).
Use the correct format string:
SELECT TO_TIMESTAMP('2019-08-21T14:38:34','YYYY-MM-DD"T"HH24:MI:SS');
to_timestamp
------------------------
2019-08-21 14:38:34+00
(1 row)

How to change a date format in postgres?

I am trying to convert a character varying date field to the format YYYYMMDD and below is the select query I tried. The output format is 1999-04-27 (YYYY-MM-DD). Admindate is the field name in the table.
select to_date( admindate,'MMDDYYYY') from test;
Can someone please advice what I am doing wrong?
Use to_char to convert the converted date into the desired format.
select to_char(to_date(admindate, 'MMDDYYYY'), 'YYYYMMDD')
from test;
you can try this
SELECT TO_CHAR(NOW() :: DATE, 'dd/mm/yyyy');

Postgresql date inconsistent format

I am building an app using node with a Postgres backend. I have a column for date where the data type is date. The dates in the database are stored in the format YYYY-MM-DD. However, when I do a query in my app, the date returned is in the format YYYY-MM-DD + (time zone information). For example:
2016-01-06T05:00:00.000Z
Does anyone know how I can prevent this?
Thanks in advance!
Use to_char
SELECT to_char(dateColumn, 'YYYY-MM-DD') AS formattedDate FROM ...
Example:
ubuntu=> SELECT to_char(to_timestamp('2016-01-07', 'YYYY-MM-DD'), 'YYYY-MM-DD');
to_char
------------
2016-01-07
(1 row)

ORA-01843: not a valid month - but what month format? Oracle 11g

I want to know what other MONTH formats exist except MM , MONTH or MON.
When the query below runs it gives me an error ORA-01843: not a valid month and I can understand why, because the server removes the "0" from the month "07" and leaves only the number "7", so the MM format is not the right one.
But which one is it?
select to_char(to_date(START_DATE,'MM/DD/YYYY '), 'DD-MM-YYYY')
from PER_ALL_PEOPLE_F
WHERE person_id=12345
The START_DATE column is DATE TYPE and it provides results like: 7/17/2012 .
Your assumption that the single-digit 7 for the month is a problem is not correct; Oracle is generally quite flexible and will happily parse a single digit month with the MM model:
select to_date('7/17/2012', 'MM/DD/YYYY') from dual;
TO_DATE('7/17/2012'
-------------------
2012-07-17 00:00:00
If start_date is already a DATE type then you should not be calling to_date() for it. You're doing an implicit conversion to a string using your NLS_DATE_FORMAT moodel, and then back to a date with your specified format. So really you're doing:
select to_char(to_date(to_char(START_DATE, <NLS_DATE_FORMAT>),
'MM/DD/YYYY '), 'DD-MM-YYYY')
If your NLS_DATE_FORMAT is something other than MM/DD/YYYY, e.g. DD-MON-YYYY, then you'll get an ORA-1843 error for at least some values.
You can see this with:
select to_date(date '2014-01-16', 'MM/DD/YYYY') from dual;
or the expanded:
select to_date(to_char(date '2014-01-16', 'DD-MON-YYYY'),
'MM/DD/YYYY') from dual;
Dates do not have any specific format, they're stored in an internal representation and then converted to a formatted string for display. You said your dates display like 7/12/2012, but given the error you're seeing your client seems to be doign that formatting, and it isn't related to the session NLS_DATE_FORMAT.
You only need to do:
select to_char(START_DATE, 'DD-MM-YYYY')