Can not convert varchar to Date - sql

I have a table with dates as a varchar type and the resulted date output (Begin_date) is in the '20130630' format.
I need to convert the begin date output as a date. It doesn't matter what date format as long as it is a date.
I have tried these but none seems to work. I get the ORA-01843 not a valid month error.
to_date(aa.Begin_date, 'yyyymmdd')
to_char(to_date(aa.Begin_date, 'yyyymmdd'), 'dd-mm-yy')
to_char(to_date(aa.Begin_date, 'yyyymmdd'), 'dd-MON-yy')
2) Additionally , some begin dates have this format 20030000. Essentially meaning we only know the year. I need to have a output that input a January 1 and year (doesn't matter format).
Case when aa.begin_date between '1900' and '2030'
then to_date('0101'+aa.begin_date,'YYYYMMDD')
I tried a like statement
case when aa.begin_date like '%0000'
then to_date('0101'+aa.begin_date,'YYYYMMDD')
end as tes

You can try something like this:
with test_data as
( select '20130630' begin_date from dual
union
select '20030000' begin_date from dual
)
select
case when test_data.begin_date like '%0000' then
to_date(substr(begin_date,1,4)||'0101','YYYYMMDD')
else to_date(begin_date,'YYYYMMDD') end my_date
from test_data
But best thing is to correct your data model to use real DATE type. Storing dates as a string is a bad design.

this will work:
SELECT to_date('20130630','YYYYMMDD') FROM dual

Related

(Oracle)Getting 30 days before

My goal is to get 30 days before.
The data are stored as varchar2 type, '20200630' and '20200613'.
The expected result is '20200531' and '20200514' for each.
I have simply subtracted 30 from my varchar2 '20200630' and '20200613' and
it shows result by casting, but the result are not expected such as '20200600','20200583' which are not form of date.
Could I know how to modify my code as below
WITH A AS
(SELECT '20200630' YEARMONTHDAY FROM DUAL
UNION ALL
SELECT '20200613' FROM DUAL)
SELECT YEARMONTHDAY - 30 FROM A;
Store your data using the appropriate type! That is, use date rather than a string.
If you are stuck with data in this format, convert to date:
select to_date(yearmonthday, 'YYYYMMDD') - interval '30' day
I would not recommend converting the value back to a string. Dates should be dates.

To_char for Oracle SQL not working

i have the following query which does not retrieve data even though there is:
select *
from INTERFACE_RUN
where TO_CHAR(INTERFACE_RUN.START_TIME, 'dd-mon-yy') = '04-MAY-10';
The start_time field is a timestamp field. Hence I converted it using to_char and compared with the user passed value of 04-MAY-10. But it did not fetch any data.
Any help please. Thanks in advance
to_char() pays attention to the case of the pattern. So, you are producing `04-may-10', which is not the same.
So, try this:
where TO_CHAR(INTERFACE_RUN.START_TIME, 'DD-MON-YY') = '04-MAY-10';
That said, I much prefer:
where trunc(INTERFACE_RUN.START_TIME) = date '2010-05-04'
or:
where INTERFACE_RUN.START_TIME >= date '2010-05-04' AND
INTERFACE_RUN.START_TIME < (date '2010-05-04') + 1
Why? For two reasons. First, the column is a date, so I prefer date comparisons. Second, I prefer the ANSI/ISO standard date formats. The second version can also readily take advantage of an index on the START_TIME column.
Oracle will convert the date to lowercase, thus generating '04-may-10'
Try with 'DD-MON-YY' (uppercase)
select TO_CHAR(sysdate, 'dd-mon-yy') from dual
union all
select TO_CHAR(sysdate, 'dd-MON-yy') from dual;
TO_CHAR(S
---------
13-jun-16
13-JUN-16

YYYYMMDD to YYYYMM in oracle

I have a column with DATE datatype in a table.
I am trying to retrieve the column values in YYYYMM format. My select query looks like below
select *
from tablename
where date column = to_char(to_date('12/31/4000','MM/DD/YYYY'),'YYYYMM');
I am getting below exception.
ORA-01847: day of month must be between 1 and last day of month
Appreciate any input on this.
I think the simplest method is:
where to_char(datecolumn, 'YYYYMM') = '400012'
Or, if you prefer:
where to_char(datecolumn, 'YYYYMM') = to_char(to_date('12/31/4000', 'MM/DD/YYYY'), 'YYYYMM');
Syntax-wise, the right hand date (to the right of the equals) is OK. But you are doing a character comparison, not a date comparison.
This works for me in multiple databases:
select to_char (to_date('12/31/4000','MM/DD/YYYY'),'YYYYMM')
from dual;
Even though your column is named DATE_COLUMN, you are comparing based on characters in the query.
So, try this instead - this compares based on dates (NOT a character comparison) and truncates off the hour, minute, ETC. so you are only comparing the DAY:
select * from DATE_TAB
where TRUNC(DATE1, 'DDD') = TRUNC(to_date('12/31/4000','MM/DD/YYYY'),'DDD');
NOTE: The DATE1 field above is a DATE field. If you're DATE_COLUMN is not a DATE field, you must
convert it to a DATE datatype first (using TO_DATE, ETC.)
Assuming that "date_column" is actually a date, and that you have an index on date_column, you can do something like this to return the data quickly (without truncating dates in all rows to do a comparison):
with dat as (
select level as id, sysdate - (level*10) as date_column
from dual
connect by level <= 100
)
select id, date_column
from dat
where date_column between to_date('11/1/2013', 'MM/DD/YYYY') and last_day(to_date('11/2013 23:59:59', 'MM/YYYY HH24:MI:SS'))
Here I just dummy up some data with dates going back a few years. This example picks all rows that have a date in the month of November 2013.
If your date_column's data-type is DATE, then use
select *
from tablename
where TO_CHAR(date_column,'YYYYMM') = to_char (to_date('12/31/4000','MM/DD/YYYY'),'YYYYMM');
If your date_column's data-type is VARCHAR, then use:
select *
from tablename
where date_column = to_char (to_date('12/31/4000','MM/DD/YYYY'),'YYYYMM');
I somehow feel your error is because you have a space between date and column as
"date column". If the field name in the table is "COLUMN", then just removing the word "DATE" from your original query would suffice, as:
select *
from tablename
where column = to_char(to_date('12/31/4000','MM/DD/YYYY'),'YYYYMM');
If your column (YYYYMMDD) is in number format, the simplest way to get YYYYMM would be
select floor(DATE/100)
from tablename;

Retrieve previous month from date column that contains MONYYYY

I need to change a date value that is coming from a table in the form of May2013 (MonYYYY). The column itself is a VARCHAR
In my select statement, I am looking to retrieve the previous month (Apr2013). I've done some research and found the following, if I were using SYSDATE:
select to_date(add_months(sysdate, 'MONYYY')-1) from dual
How do I make it work for the date structure I have above? I've tried:
select to_date(add_months(date.datetable, MONYYY)-1) from datetable
ADD_MONTHS function needs a date variable as input. So, first you need to convert your varchar column to date type and then apply the add_months function.
SELECT ADD_MONTHS (TO_DATE ('May2013', 'monyyyy'), -1) FROM DUAL;
The return type will be date. In this case, it returns 0th hour of first day of April.

store dates in oracle

I have a table as
create table Dummy (date_created date)
in oracle.I want to store date in 'dd-mon-yyyy' (12-dec-2010) format.
How should i do this.
Please help.
In Oracle a column created with the DATE datatype just stores the date. It doesn't have a particular format, it just stores the day, month, year, hour, minute, and second. You need to convert from whatever format you have using the TO_DATE function. If you have a text string with the date in 'dd-mon-yyyy' format and you want to put this date into your table you'd use something like
INSERT INTO DUMMY (DATE_CREATED)
VALUES (TO_DATE('01-FEB-2011', 'DD-MON-YYYY');
Going the other way (from DATE column value to character string) you'd use the TO_CHAR function. If you were retrieving a value from your table and wanted to convert it to 'DD-MON-YYYY' format you'd use something like
SELECT TO_CHAR(DATE_CREATED, 'DD-MON-YYYY')
FROM DUMMY;
Share and enjoy.
Use to_date() function. In your case, the syntax would be
insert into Dummy values (to_date('08-09-2010', 'dd-mm-yyyy'));
Here is a link to the detailed help.
The DATE datatype will store date and time information (century, year, month, day, hours, minutes, and seconds) in an internal format in the database. When you get it out of the database, you can choose to display it in whatever format you like.
This information is either created using implicit conversion from a string or explicitly using either the TO_DATE function or the ANSI date literal. If you look in the v$nls_parameters view, this will tell you what the NLS_DATE_FORMAT is which is generally used for the implicit conversion. This may often be defined as DD-MON-RR, which might be why the date will come out as 23-DEC-10 when the query select sysdate from dual is run. (Not entirely sure I'm right about the nls stuff. Correct me if I'm wrong.)
However, all the date information is available if you know how to get it. The query select to_char(sysdate, 'dd-mon-yyyy hh24:mi:ss') from dual will return all the date fields.
Likewise, the insert statement shown below will create a row with a date value in it.
insert into dummy (date_created)
values (to_date('12-dec-2010 12:34:56', 'dd-mon-yyyy hh24:mi:ss'))`
This data can then be retrieved.
select date_created from dummy
This will implicitly convert the date to a character string using the NLS_DATE_FORMAT, providing the output below.
DATE_CREA
---------
23-DEC-10
The full date information is available by explicitly converting the date to a character string.
select to_char(date_created, 'DD-MON-YYYY') as date_created from dummy;
select to_char(date_created, 'DD-MON-YYYY HH24:MI:SS') as date_created
from dummy;
This will provide output in the format you require:
DATE_CREATE
-----------
23-DEC-2010
If you always use the TO_DATE and TO_CHAR functions to convert to/from a date datatype, then you will have fewer problems. Implicit conversion is useful but can cause some confusion or problems.
You can keep and eye here
http://www.techonthenet.com/oracle/functions/to_date.php
use to_date function to save a data with the format you need. I suggest to use SYSDATE updating table and when you need to read data from table use something like that:
dbms_output.put_line(TO_CHAR(SYSDATE, 'DD-MON-YYYY HH24:MI:SS'));
to solve your problem use:
to_date('08/JAN/2010', 'DD/MON/YYYY')
Just use TRUNC(YourDate) if date have time part, it will be truncate time part. Oracle have not just 'DATE' type, 'DATE' always have time part.
However if you do not specify time - it will 00:00:00.
SELECT TRUNC(SYSDATE) from dual
Result:
23-12-2010
Oracle does not support DATE without time part.
You can make it always be an integer date by adding a CHECK constraint:
CREATE TABLE dummy (date_created date CHECK (date_created = TRUNC(date_created)))
, insert it in any format you want:
INSERT
INTO dummy (date_created)
VALUES (TO_DATE('23-DEC-2010', 'dd-mon-yyyy'))
and select it in any format you want:
SELECT TO_CHAR(date_created, 'dd-mon-yyyy')
FROM dummy