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

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

Related

How to change the timestamp format in Postgresql to extract day part of the str?

I have create a datetime with type timestamp. datetime timestamp NOT NULL I am not sure why the output is like this:
I want to extract the day part. I have tried these different approach but in both cases I am getting an error. How can I fix it?
extract(DAY FROM TIMESTAMP min(to_char(u.datetime ,'YYYY-MM-DD HH24:MI'))::timestamp)
EXTRACT(DAY FROM TIMESTAMP min(to_char(u.datetime ,'YYYY-MM-DD HH24:MI')))
date_part('day', min(to_char(u.datetime ,'YYYY-MM-DD HH24:MI')))
As mentioned in response I modified query to be like below and it does work.
extract(day from MIN(datetime)) as Day
All you need is:
select *, extract(day from activated_at) as Day from yourTable;
What you are seeing is a timestamp formatted as text for the display. Underlying data is timestamp as you said, directly use it.

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.

How to get data between two date range in PostgreSQL?

I m querying data between two date range in PostgreSQL SQL but it does not give me expected result.
select
pi_serial,
amount_in_local_currency,
status,
proforma_invoice_date
from proforma_invoice
where created_by = 25
and proforma_invoice_date BETWEEN '03/01/2018' and '09/03/2018'
order by proforma_invoice_date
Now look at the query and column proforma_invoice_date. In this query i am searching data between 03/01/2018 and 09/03/2018. the date format is (DD/MM/YYYY) and it's character varying. The result i have got in this picture. it just give me the result according by the only day but not the whole date format. i have tried so many things date conversion, character varying to date. but i didn't get any expected result
Your Query is absolutely fine. just change it as follows
select pi_serial, amount_in_local_currency, status, proforma_invoice_date
from proforma_invoice
where created_by = 25
and to_date(proforma_invoice_date, 'DD/MM/YYYY') BETWEEN to_date('03/01/2018', 'DD/MM/YYYY') and to_date('09/03/2018', 'DD/MM/YYYY')
order by proforma_invoice_date

Oracle SQL : Expand year to a full date

This must be simple to answer, but how do you expand in Oracle a year to a full date, e.g.
1996 to 1996-01-01 00:00:00 ?
EDIT
The data type of the year is char, and I want to end up by comparing this year to a string-date, e.g.
1996 <= '1998-31-12 12:04:35'
It is important that the expanded data is expanded in the same data Format (since I get the dates preformatted)
At the end I need something like this
WHERE ( to_date(table.year_char ,'YYYY') <= '1996-12-31 00:00:00')
or sth like this
WHERE ( to_char(to_date(table.year_char ,'YYYY')) <= '1996-12-31 00:00:00')
or anything which works
If you're starting with the year as a string and you want to end up with a DATE object, you use the TO_DATE() function; but you need to supply a dummy month or it'll default to the first day of the current month in the specified year:
select to_date('1996', 'YYYY') from dual;
May, 01 1996 00:00:00+0000
SQL Fiddle
With the month, and to make it clearer the day too, appended and a suitable format model:
select to_date('1996' ||'-01-01', 'YYYY-MM-DD') from dual
January, 01 1996 00:00:00+0000
SQL Fiddle. I've left the year and the '-01-01' literal separate and concatenated on the assumption that you'll be using a variable really...
In a WHERE clause, using the sample date you initially showed:
select * from dual
where to_date('1996' ||'-01-01', 'YYYY-MM-DD')
<= to_date('1998-31-12 12:04:35', 'YYYY-DD-MM HH24:MI:SS')
Or if you're actually comparing to a string as your second example suggests, just leave it as a string, as you want both sides of the comparison to be the same data type without any implicit conversion that might cause you problems later. The string you have fortunately has the data in a format that is comparable:
WHERE (table.year_char || '-01-01 00:00:00' <= '1996-12-31 00:00:00')
You could convert it to and back from a DATE but there isn't any benefit in doing so.

Convert varchar2 to Date ('MM/DD/YYYY') in PL/SQL

I need to convert string from varchar to Date in 'MM/DD/YYYY' format. My input string is '4/9/2013' and my expected output is '04/09/2013'. i.e. 2 digit month, 2 digit date and 4 digit year seperated by'/'
I have below data:
DOJ varchar2(10 Byte)column has '4/9/2013' value.
I am using 'select TO_DATE(DOJ,'MM/DD/YYYY') from EmpTable. But I am getting result as 09-APR-13. I want it in 04/09/2013 format.
Please advise. Thanks in advance.
Easiest way is probably to convert from a VARCHAR to a DATE; then format it back to a VARCHAR again in the format you want;
SELECT TO_CHAR(TO_DATE(DOJ,'MM/DD/YYYY'), 'MM/DD/YYYY') FROM EmpTable;
An SQLfiddle to test with.
First you convert VARCHAR to DATE and then back to CHAR. I do this almost every day and never found any better way.
select TO_CHAR(TO_DATE(DOJ,'MM/DD/YYYY'), 'MM/DD/YYYY') from EmpTable
Example query:
SELECT TO_CHAR(TO_DATE('2017-08-23','YYYY-MM-DD'), 'MM/DD/YYYY') FROM dual;