Date mapping or translation in ORACLE - oracle

I have to use a schema on the table FOOTRADE.
When I look for a specific tradeid, it works fine, in this case it returns 1/7/2020.
select FOODATE from CASPER.FOOTRADE where tradeid = '0000000001';
FOODATE
========
1/7/2020
However when I want to look for all the rows that contains foodate 1/7/2020 - I get an error ORA-01843: not a valid month
select * from CASPER.FOOTRADE where TRADEDATE = '1/7/2020';
I suspect that the column is configured to somedate format and there has to be some mapping translation that needs to be done

Assuming TRADEDATE is actually an oracle DATE type, then you should not use direct literals to compare. Instead do:
where TRADEDATE = to_date('01/07/2020','MM/DD/YYYY');
The default date format mask is defined in NLS_DATE_FORMAT. So if that format does not match with your input you get an error. It is best to never assume what the default format may be on a system and do a to_date() call on the string.
Note that you can override the default and change the NLS_DATE_FORMAT for a session via:
ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY MM DD' .. etc whatever mask you need.

Related

Oracle SQL/PL - ORA-01843: not a valid month

I get the error message: ORA-01843: not a valid month after executing a sql plus script.
I try using the "standard" date format yyyy-mm-dd.
Is SQL/PL not understanding the alter session statement?
set linesize 200
set pagesize 1000
alter session set NLS_NUMERIC_CHARACTERS = ',.';
alter session set NLS_DATE_FORMAT = 'yyyy-mm-dd';
select
*
from my_table
where
date >= '2019-08-31';
exit
What you do need - from my point of view - is not to compare date values to strings.
Presuming that date here actually represent a DATE datatype column (why didn't you post table description?) (as already commented, you can't name a column that way, not unless you enclosed its name into double quotes), then
where date >= '2019-08-31'
---- ------------
DATE this is a string
datatype
Use date literal, which always has a DATE keyword and date in format 'yyyy-mm-dd':
where date >= date '2019-08-31'
Or, use to_date function with appropriate format mask:
where date >= to_date('2019-08-31', 'yyyy-mm-dd')
If date column (wrong name, as we already know) actually contains strings and you hope all of them are following the 'yyyy-mm-dd' format, well, some values don't. Storing dates into varchar2 datatype column is almost always a bad idea. Nobody prevents you from storing e.g. '2019-ac-31' into it, and that isn't a valid date value.

changing Date format in query

in some part of my program , I want to run a sql query and have the result which is a date like : %Y/%m/%d %H:%M:%S
SELECT MAX(created_at)
FROM HOT_FILES_LOGS
WHERE FILE_NAME = 'test'
date in created_at column is stored like 04/03/2021 15:45:30 ( it is fulled with SYSDATE)
but when I run this query, I get just 04.03.21
what should I do to fix it?
Apply TO_CHAR with appropriate format mask:
select to_char(max(created_at), 'yyyy.mm.dd hh24:mi:ss') as created_at
from hot_files_logs
where file_name = 'test'
Oracle does not store dates or timestamps in any display format, they are stored in an internal structure, every date in every Oracle database since at least 8i and probably earlier. This structure consists of 7 1-byte integers (timestamps in a similar but larger structure). How the date is displayed or a string converted to a date is controlled the specified date format string in the to_char or to_date function or if no format string given by the NLS_DISPLAY_FORMAT setting. To get a gimps at the internal settings run the following:
create table td( d date);
insert into td(d) values(sysdate);
select d "The Date" , dump(d) "Stored As" from td;
See example. The last used format is not practical but strictly demonstrable. Well I guess you could use it to seed a repeatable random sequence.

How to write date condition on where clause in oracle

I have data in the date column as below.
reportDate
21-Jan-17
02-FEB-17
I want to write a query to fetch data for 01/21/2017?
Below query not working in Oracle.
SELECT * FROM tablename where reportDate=to_date('01/21/2017','mm/dd/yyyy')
What is the data type of reportDate? It may be DATE or VARCHAR2 and there is no way to know by just looking at it.
Run describe table_name (where table_name is the name of the table that contains this column) and see what it says.
If it's a VARCHAR2 then you need to convert it to a date as well. Use the proper format model: 'dd-Mon-rr'.
If it's DATE, it is possible it has time-of-day component; you could apply trunc() to it, but it is better to avoid calling functions on your columns if you can avoid it, for speed. In this case (if it's really DATE data type) the where condition should be
where report_date >= to_date('01/21/2017','mm/dd/yyyy')
and report_date < to_date('01/21/2017','mm/dd/yyyy') + 1
Note that the date on the right-hand side can also be written, better, as
date '2017-01-21'
(this is the ANSI standard date literal, which requires the key word date and exactly the format shown, since it doesn't use a format model; use - as separator and the format yyyy-mm-dd.)
The query should be something like this
SELECT *
FROM table_name
WHERE TRUNC(column_name) = TO_DATE('21-JAN-17', 'DD-MON-RR');
The TRUNC function returns a date value specific to that column.
The o/p which I got when I executed in sqldeveloper
https://i.stack.imgur.com/blDCw.png

How to insert date in Oracle 10g xe?

I did the following:
CREATE TABLE BOOK(
BOOK_ID VARCHAR(4) PRIMARY KEY,
ISBN_10 VARCHAR(10),
TITLE VARCHAR(50),
CATEGORY VARCHAR(25),
PRICE DECIMAL(6,2),
BINDING VARCHAR(2),
PUB_DATE DATE,
AUTHOR_ID SMALLINT,
PUBLISHER_ID SMALLINT
);
2.
ALTER SESSION SET nls_date_format = 'DD-MM-YYYY HH24:MI:SS';
3.
INSERT INTO BOOK
VALUES('4','123459','INTRODUCTION TO SmallTalk','IT',157.00,'S',**'26-01-1991'**,13,103);
It gave an error:
ORA-01843: not a valid month
However if do the following there is no problem:
Query:
INSERT INTO
BOOK
VALUES('4','123459','INTRODUCTION TO Small Talk','IT',157.00,'S','**26-JAN-1991**',13,103);
Can anyone explain why?
You want to read the Datetime Literals section of the manual. Alternatives are:
Use a date literal: DATE '1991-01-26'
Convert from string: TO_DATE('26-01-1991', 'DD-MM-YYYY')
If you set NLS_DATE_FORMAT you can omit TO_DATE()'s second argument but not skip the function entirely.
See also: Datetime Format Models
It worked for me. That implies that your alter statement did not work for some reason. However, it is not good practice to assume a specific date format on a system when using literals. Instead, cast the literal with a format mask on the insert, such as:
INSERT INTO BOOK
VALUES('4','123459','INTRODUCTION TO Small Talk','IT',157.00,'S',
to_date('26-JAN-1991','DD-MON-YYYY'), 13,103);
(or whatever format you require as a literal input value)
This because in your database, nls_date_time is set to 'DD-MON-RRRR' format, you can check this using
select * from V$NLS_PARAMETERS
In this query
INSERT INTO BOOK VALUES('4','123459','INTRODUCTION TO SmallTalk','IT',157.00,'S','26-01-1991',13,103);
date format is 'dd-mm-rrrr', change the statement as
INSERT INTO BOOK VALUES('4','123459','INTRODUCTION TO SmallTalk','IT',157.00,'S',to_date('26-01-1991', 'dd-mm-rrrr'),13,103);
and it will also run since you have now provided the date format.
When you set
ALTER SESSION SET nls_date_format = 'DD-MM-YYYY HH24:MI:SS';
then your insert must match this format, i.e.
INSERT INTO BOOK VALUES('4','123459','INTRODUCTION TO SmallTalk','IT',157.00,'S',
'26-01-1991 00:00:00'
,13,103);
However, the more secure way is to use date literals or TO_DATE function.
You just need to use
INSERT INTO TABLE_NAME VALUES('07-JAN-96');
Oracle always takes "DD-MMM-YY" format. You should write your month as the first 3 characters of month name.

how to insert current date into a DATE field in dd/mm/yyyy format in oracle

I have a field in my table with datatype as DATE in Oracle.
I want to insert the current date into that field, in format DD/MM/YYYY format.
I tried the below query:
select to_date(to_char(sysdate,'dd/mm/yyyy'),'dd/mm/yyyy') from dual
But it gives
1/8/2011 12:00:00 AM.
I want it to insert and show as
08/01/2011 12:00:00 AM.
Can anyone help me in this please ?
DATE is a built-in type in Oracle, which is represented in a fixed way and you have no control over it.
So:
I want it to insert [...] as 08/01/2011 12:00:00 AM
The above is nonsensical. You don't insert a string, you insert a date.
Format is useful only when you want:
to convert a string to an internal representation of date with TO_DATE (format mask: how to parse the string);
to convert an internal representation of date to a string with TO_CHAR (format mask: how to render the date).
So basically, in your example you take a DATE, you convert it to a STRING with some format, and convert it back to DATE with the same format. This is a no-op.
Now, what your client displays: this is because your Oracle Client won't display DATE fields directly and the NLS layer will convert any DATE field that is selected. So it depends on your locale by default.
What you want is SELECT TO_CHAR(SYSDATE,'DD/MM/YYYY') FROM dual; which will explicitly perform the conversion and return a string.
And when you want to insert a date in a database, you can use TO_DATE or date literals.
Alternatively, if you want to retrieve the date part of the DATE field, you may use truncate, i.e.
select to_char(trunc(sysdate),'dd/mm/yyyy') from dual;
When the column is of type DATE, you can use something like:
Insert into your_table(your_date_column) Select TRUNC(SYSDATE) from DUAL;
This removes the time part from SYSDATE.
Maybe this can help
insert into pasok values ('&kode_pasok','&kode_barang','&kode_suplier',
to_date('&tanggal_pasok','dd-mm-yyyy'),&jumlah_pasok);
note: '&' help we to insert data again, insert / end than enter to
insert again example: Enter value for kode_pembelian: BEL-E005 Enter
value for kode_barang: ELK-02 Enter value for kode_customer: B-0001
old 2: '&kode_pembelian','&kode_barang','&kode_customer', new 2:
'BEL-E005','ELK-02','B-0001', Enter value for tanggal_pembelian:
24-06-2002 Enter value for jumlah_pembelian: 2 old 3:
to_date('&tanggal_pembelian','dd-mm-yyyy'),&jumlah_pembelian) new 3:
to_date('24-06-2002','dd-mm-yyyy'),2)
1 row created.
SQL> / (enter)

Resources