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.
Related
I have a oracle view in which we have column called dayofset which defined from subtraction of two date columns like(to_date(date_column1)-to_date(date_column2)) and it is stored as number(38) datatype.
2.so,when I run session in informatica to get data from oracle view to redshift.im getting error like "not a valid month".
3.Input values for that column is like (25-JAN-21,10-APR-13)
4.im getting the output values like 1,2,3,4... Like this all are integer values.(this column just do the datediff operation) and provide the difference between two dates.
Could you guys please help on this.
I have a oracle view in which we have column called dayofset which defined from subtraction of two date columns like to_date(date_column1)-to_date(date_column2) and it is stored as number(38) datatype.
Never use TO_DATE on a column that is already a DATE data type. Just use.
CREATE VIEW your_view (dayofset)
SELECT date_column1 - date_column2
FROM your_table;
If you use TO_DATE then it takes a string as the first argument so you are effectively performing an implicit conversion to a string to convert it back to a date and your code is the equivalent of:
CREATE VIEW your_view (dayofset)
SELECT TO_DATE(
TO_CHAR(
date_column1,
(SELECT value FROM NLS_SESSION_PARAMETERS WHERE parameter = 'NLS_DATE_FORMAT')
),
(SELECT value FROM NLS_SESSION_PARAMETERS WHERE parameter = 'NLS_DATE_FORMAT')
)
-
TO_DATE(
TO_CHAR(
date_column2,
(SELECT value FROM NLS_SESSION_PARAMETERS WHERE parameter = 'NLS_DATE_FORMAT')
),
(SELECT value FROM NLS_SESSION_PARAMETERS WHERE parameter = 'NLS_DATE_FORMAT')
)
FROM your_table;
Depending on your NLS_DATE_FORMAT session parameter, this could just be a waste of time or it could truncate the date and give you an unexpected result; however, any user can change their session parameters at any time so you may get different results for different users so you should NEVER rely on implicit conversions.
If your columns are not a DATE data-type but are strings then use an explicit format model (and, if required, language) in the conversion:
CREATE VIEW your_view (dayofset)
SELECT TO_DATE(string_column1, 'DD-MON-RR', 'NLS_DATE_LANGUAGE=English')
- TO_DATE(string_column2, 'DD-MON-RR', 'NLS_DATE_LANGUAGE=English')
FROM your_table;
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.
I am learning oracle 11g. I need to create columns to store Year and Month in the following sample format:
Year: 2015
Month: 6
I saw Date Time data type which takes whole date only .Also Number type may allow invalid year and month. But I want them in the given form while avoiding invalid month and year. Please tell me how to fix it.thanks
Updates: is this okay for such inputs?
CREATE TABLE FOOBAR (YYYY DATE, MM DATE);
The best solution is to store dates in DATE columns. Oracle has some pretty neat date functions, and you'll find it easy to work with storing the first of the month in a single DATE column. Otherwise you'll find yourself constantly extracting elements from other dates or cluttering your code with TO_CHAR() and TO_DATE() calls. Find out more.
However, if you have a rigid requirement, you can use strong typing and check constraints to avoid invalid months:
CREATE TABLE FOOBAR (
YYYY number(4,0) not null
, MM number(2,0) not null
, constraint foobar_yyyy_ck check (yyyy != 0)
, constraint foobar_mm_ck check (mm between 1 and 12)
);
This won't do what you want because it will default the missing elements:
CREATE TABLE FOOBAR (YYYY DATE, MM DATE);
We can't store just a year or just a month in DATE columns.
Use the DATE data type..
and when perform insert operation onto your db.. use
TO_DATE ('November 13, 1992', 'MONTH DD, YYYY')
For input and output of dates, the standard Oracle date format is DD-MON-YY, as follows:
'13-NOV-92'
perform insert operation/query like this:
INSERT INTO table_name (name, created_at) VALUES
('ANDY', TO_DATE ('November 13, 1992', 'MONTH DD, YYYY'));
Here is link to the guide as well:
https://docs.oracle.com/cd/B28359_01/server.111/b28318/datatype.htm#i1847
If you want to store month and year separately in the db you may use NUMBER & NUMBER(n)
https://docs.oracle.com/cd/B28359_01/server.111/b28318/datatype.htm#i22289
Hope this helps..
My table has two DATE format attributes, however, when i try to insert value it throws an error: date format picture ends before converting entire input string.
Here is my attempted code:
insert into visit
values(123456, '19-JUN-13', '13-AUG-13 12:56 A.M.');
I think the problem is with 12:56 but Oracle documentation says date implies both date and time.
Perhaps you should check NLS_DATE_FORMAT and use the date string conforming the format.
Or you can use to_date function within the INSERT statement, like the following:
insert into visit
values(123456,
to_date('19-JUN-13', 'dd-mon-yy'),
to_date('13-AUG-13 12:56 A.M.', 'dd-mon-yyyy hh:mi A.M.'));
Additionally, Oracle DATE stores date and time information together.
you need to alter session
you can try before insert
sql : alter session set nls_date_format = 'YYYY-MM-DD HH24:MI:SS'
What you're trying to insert is not a date, I think, but a string. You need to use to_date() function, like this:
insert into table t1 (id, date_field) values (1, to_date('20.06.2013', 'dd.mm.yyyy'));
I had this error today and discovered it was an incorrectly-formatted year...
select * from es_timeexpense where parsedate > to_date('12/3/2018', 'MM/dd/yyy')
Notice the year has only three 'y's. It should have 4.
Double-check your format.
Im having trouble inserting a row in my table. Here is the insert statement and table creation. This is part of a uni assignment hence the simplicity, what am i doing wrong? Im using oracle SQL developer Version 3.0.04.'
The problem i am having is that it is only inserting the dd/mon/yy but not the time. How do i get it to insert the time as well?
INSERT INTO WORKON (STAFFNO,CAMPAIGNTITLE,DATETIME,HOURS)
VALUES ('102','Machanic Summer Savings',TO_DATE('22/April/2011 8:30:00AM','DD/MON/YY HH:MI:SSAM'),'3')
;
CREATE TABLE WorkOn
(
StaffNo NCHAR(4),
CampaignTitle VARCHAR(50),
DateTime DATE,
Hours VARCHAR(2)
)
;
Thanks for the help.
EDIT: This is making no sense, i enter just a time in the field to test if time is working and it outputs a date WTF? This is really weird i may not use a date field and just enter the time in, i realise this will result in issues manipulating the data but this is making no sense...
You can use
insert into table_name
(date_field)
values
(TO_DATE('2003/05/03 21:02:44', 'yyyy/mm/dd hh24:mi:ss'));
Hope it helps.
You are doing everything right by using a to_date function and specifying the time. The time is there in the database. The trouble is just that when you select a column of DATE datatype from the database, the default format mask doesn't show the time. If you issue a
alter session set nls_date_format = 'dd/MON/yyyy hh24:mi:ss'
or something similar including a time component, you will see that the time successfully made it into the database.
Try this:
...(to_date('2011/04/22 08:30:00', 'yyyy/mm/dd hh24:mi:ss'));
Just use TO_DATE() function to convert string to DATE.
For Example:
create table Customer(
CustId int primary key,
CustName varchar(20),
DOB date);
insert into Customer values(1,'Vishnu', TO_DATE('1994/12/16 12:00:00', 'yyyy/mm/dd hh:mi:ss'));
create table Customer(
CustId int primary key,
CustName varchar(20),
DOB date);
insert into Customer values(1,'kingle', TO_DATE('1994-12-16 12:00:00', 'yyyy-MM-dd hh:mi:ss'));