How to Insert table column values: month & day from column datetype - sql

I'm using Oracle 11g Schema
I want to select the month and day from a date concatenate it and put it into a column.
The syntax I have here makes sense but its throwing an error of..
INSERT INTO OB_SELECT_LST12_SPG WED
VALUES (((TO_CHAR(TO_DATE('RET_DATE', MM))||( TO_CHAR(TO_DATE('RET_DATE', DD)));
"SQL Error: ORA-00907: missing right parenthesis"
Any help is greatly appreciated.
Thanks.

Some points first...
Your table name has a space in in
Your not enough columns error is caused by you having more than one column in the table
You really shouldn't be doing this at all
If ret_date is a column don't encapsulate it in quotation marks (') as you won't be able to convert the string 'ret_date' into a date.
However, assuming ret_date is a string that looks like 13-06-2012
insert into ob_select_lst12_spg_wed (my_column)
values(to_char(to_date(:ret_date,'dd-mm-yyyy'),'mmdd'));
If ret_date is a date data-type then you can remove the inner conversion.
insert into ob_select_lst12_spg_wed (my_column)
values(to_char(:ret_date,'mmdd'));
to_char and to_date both make use of datetime format models, which have a number of options.
Please don't do this though. Always store dates as dates
From your comments ret_date is a date and the column wed is a character. You're getting the error bind variable not declared because you haven't specified the variable. I'm going to assume that ret_date is in another table as it's a date, in which case lose the values key-word and insert directly from that table:
insert into ob_select_lst12_spg (wed)
select to_char(ret_date,'mmdd')
from the_other_table
This shouldn't be required though, you can always convert a date into whatever you want on exit from the database. If you don't store it as a date in the database then it's easy for errors and incorrect values to creep in. I personally would change the column wed to a date, in which case your query becomes the very simple:
insert into ob_select_list12_spg (wed)
select ret_date
from the_other_table
You can then use the correct datetime format model for your needs when selecting from the database.

Sample query to get date, month from date column:
Select ((To_Char(to_date('01-FEB-73','dd-mon-yy'), 'MM'))
||( To_Char(to_date('01-JAN-73','dd-mon-yy'), 'dd')))
from dual;
if RET_DATE is date datatype then use
insert into table_name (columname)
Select ((To_Char(RET_DATE, 'MM'))
||( To_Char(RET_DATE, 'dd')))
from dual;

You are missing extra parentheses. Number of opened ( was 8 and closed ) was 5
INSERT INTO OB_SELECT_LST12_SPG(WED)
VALUES ((TO_CHAR(TO_DATE(RET_DATE, MM)))||( TO_CHAR(TO_DATE(RET_DATE, DD))));
Update
Make sure other columns are not required (i.e. Allow NULLs)

Related

Timestamp to date | Oracle DB

I want to select a column which is from datatype "datetime" in Orcale DB in date format.
column
1/31/2006 22:00:00 AM
I tried the following queries and got errors like below
select DATE_FORMAT(column, 'YYYY-MM-DD') as column from table
ORA-00904: "DATE_FORMAT": invalid identifier
select TO_DATE(column, 'YYYY-MM-DD') as column from table
ORA-00918: column ambiguously defined
select TO_DATE(column, 'YYYY-MM-DD') from table
org.apache.avro.SchemaParseException: Illegal character in: to_date(column,'yyyy-mm-dd')
What is the right syntax for this?
You should be using a to_char to convert a date to a specific string format.
select to_char(dat_column, 'YYYY-MM-DD') as column from table;
If you are expecting a date as return type, then that is more about the client you are using. You could set a session level parameter like this:
alter session set nls_date_format='YYYY-MM-DD';
In Oracle, there is no "datetime" data type. There is either DATE or TIMESTAMP and they both have year, month, day, hour, minute and second components (TIMESTAMP also has optional fractional seconds and time zone components). Both of those data types are binary data types and do NOT store a format.
If you want to set the time component of a DATE to midnight then use:
SELECT TRUNC(date_column) AS date_column_at_midnight
FROM table_name;
If you want to display a DATE column with only year, month and day components then you need to convert to a data type that does support a formatting; i.e. a string.
SELECT TO_CHAR(date_column, 'YYYY-MM-DD') AS formatted_date_string
FROM table_name;
To convert a column of datatype TIMESTAMP to datatype DATE use the CAST function (docs) :
select CAST(column AS DATE) as dt_column from table
The error "ORA-00918: column ambiguously defined" indicates that the SQL engine is unable to determine what table the column belongs to. To avoid that make sure to alias all joined tables and use the alias for those columns. Like this:
select CAST(t1.column AS DATE) as dt_column from table t1

DBMS date command [duplicate]

This question already has answers here:
Not a valid month while inserting data in oracle
(2 answers)
Closed 1 year ago.
whenever I'm trying to insert this query I'm getting an error.
CREATE TABLE dateOfBirth(dateOfBirth date);
INSERT INTO dateOfBirth(dateOfBirth)VALUES('1967-11-17');
I'm getting this error:
ORA-01843: not a valid month
Let me try your code:
SQL> CREATE TABLE dateOfBirth(dateOfBirth date);
Table created.
SQL> INSERT INTO dateOfBirth(dateOfBirth)VALUES('1967-11-17');
INSERT INTO dateOfBirth(dateOfBirth)VALUES('1967-11-17')
*
ERROR at line 1:
ORA-01861: literal does not match format string
SQL>
Doesn't work either, but - failed with a different error.
Basically, that's what happens (I mean - you get errors) when you're trying to enter a string into a date datatype column. Although it is clear at the first glance that - if you're inserting '1967-11-17' into dateOfBirth - that someone was born on 17th of November 1967. Oracle, unfortunately, isn't that enthusiastic about it. It will try to implicitly convert string to date, but - as you can see, in both your and my case it miserably failed.
Besides, what if you tried to enter e.g. 12-07-05. What is what in this string? Is 12 year (could be), month (could be) or day (could be as well)? The same goes for 07 and 05. Simply, don't do that, don't rely on implicit conversion from anything to something else.
So, what can you do about it? Obviously, insert a valid DATE datatype value! How? For example:
use date literal which always looks like date 'yyyy-mm-dd':
SQL> insert into dateofbirth (dateofbirth) values (date '1967-11-17');
1 row created.
or, use to_date function with appropriate format mask:
SQL> insert into dateofbirth (dateofbirth) values (to_date('17.11.1967', 'dd.mm.yyyy'));
1 row created.
or, alter session and set date format so that Oracle recognizes it (as you'll see, your "initial" insert will now succeed):
SQL> alter session set nls_date_format = 'yyyy-mm-dd';
Session altered.
SQL> INSERT INTO dateOfBirth(dateOfBirth)VALUES('1967-11-17');
1 row created.
Quite a few options. Therefore, if you take control over it, there'll be no problem at all.
Your insert statement tries to squeeze a string into a date column. Oracle must use implicit conversion in order to allow this to happen, this will use the sessions NLS parameters. In your example, the implicit conversion does not work with the string you have given it.
Implicit conversion is an easy way to end up with corrupt data or errors (as you are seeing now), you should always use explicit conversion so that you can be sure the string is being converted with the correct date format no matter what the session's settings are:
CREATE TABLE dateOfBirth(dateOfBirth date);
INSERT INTO dateOfBirth(dateOfBirth)VALUES(to_date('1967-11-17','yyyy-mm-dd'));
Once the data is stored in the table as a date, it can be displayed with whatever date format you prefer:
select to_char(dateOfBirth,'dd/mm/yyyy') dateOfBirth from dateOfBirth;
Or you can leave it up to the client's session settings to decide how to display it (making it the responsibility of the client to make it correct).
select dateOfBirth from dateOfBirth;

Insert Date and String on table

I have a table with the following properties:
U. TRADE_ID VARCHAR2
TRADE_DATE DATE
PRICE NUMBER(10.2)
Once I get the data from a file, the colums are as follow:
PSD231,03051982,50.1
My question is, are those the correct datatypes for the table? I'm getting the input from the file for the first sql column as a String. But I can't display it. I think I'm missing something in the sql part. How can I create a new sql row filled with data so I can test it? This is what I tried and works:
insert into myTable(TRADE_ID, TRADE_DATE, PRICE)
values('PSD231', date '2011-01-01', 10.4);
but I'm trying create a query with the date in the format:
01-01-2011, not like the above
"What if I wanted to insert the values like this: 10062019 in the format ddmmyyyy instead of how I have it in the query: 2011-01-01 "
Just put single quotes around the date, '10062019', and make sure it is in the correct date format for your region (ddmmyyyy in your case).
insert into myTable (TRADE_ID, TRADE_DATE, PRICE)
values ('PSD231', '20110101', 10.4);
To change date formats, use the CONVERT function e.g
SELECT CONVERT(varchar, getdate(), 1)
There is a nice list for the convert function here
Since you have defined the first field as a varchar type, it should be more like,
insert into myTable(TRADE_ID, TRADE_DATE, PRICE)
values('PSD231','2011-01-01', '10');
Values should be enclosed into single quotes when they are being inserted.

Working with dates in Teradata

I'm trying to insert some data into a Teradata database, this information has been originally exported from an Oracle instance, but I have a little problem with the dates, here's an example of the data:
CO_ID | CUSTOMER_NAME | JOIN_DATE
1022945 | John Carpenter | 07/03/2018 01:55:24 p.m.
And this is the create table statement:
CREATE TABLE transact (
co_id varchar(50),
user_name varchar(50),
join_date date);
Teradata is throwing error every time I execute the insert statement for example:
expected something between a string and a unicode character ...
How can I insert the information keeping the original format of the date, I have to modify the create table or there's any other trick?
thank you.
In Teradata a date is a date (without the time portion). For the insert you have to convert the input string into a valid date.
Somthing like
select cast('07/03/2018' as date format 'DD/MM/YYYY');
select cast(substr(input.join_date,1,10) as date format 'DD/MM/YYYY');
If you cast a string as date then the format clause is a description how the string parts are used to convert into a internal date format.
If you select from a date column the format clause is used to describe the wanted output format.
select cast( cast('07/03/2018' as date format 'DD/MM/YYYY') as date format 'YYYY-MM-DD')
If you add a format clause to your table definition join_date date 'DD/MM/YYYY', you define the default format for that column, which is used as the output format whenever no explicit format is given.
If you want to use the time portion too, your target column needs to be a timestamp
select cast( cast(regexp_replace('07/03/2018 01:55:24 p.m.','\.m\.', 'M')
as timestamp format 'MM/DD/YYBHH:MI:SS BT') as timestamp format 'YYYY-MM-DDBHH:MI:SS')
format phrase documentation

SQL oracle beginner questions

Question1:
Do i have to use to_date while inserting date?
INSERT INTO some_table (date1, date2)
VALUES (to_date('2012-10-24','YYYY-MM-DD'), to_date('2012-10-24','YYYY-MM-DD'));
Or can just insert as string? Will everything be OK this way too? I've tried and it worked.
INSERT INTO some_table (date1, date2)
VALUES ('2012-10-24',2012-10-24');
Question2:
What happens if i won't name columns that i'm inserting into? It works, but my question is if it inserts randomly now or it takes order of columns during creation of table?
INSERT INTO some_table
VALUES ('2012-10-24',2012-10-24');
1 seems to only work with the 'YYYY-MM-DD' format:
http://docs.oracle.com/cd/B28359_01/server.111/b28286/sql_elements003.htm#SQLRF51049 says
You can specify a DATE value as a string literal ... to specify a DATE value as a literal, you must use the Gregorian calendar. You can specify an ANSI literal... The ANSI date literal contains no time portion, and must be specified in the format 'YYYY-MM-DD'.
However, it might work with time if you use the
Alternatively you can specify an Oracle date value... The default date format for an Oracle DATE value is specified by the initialization parameter NLS_DATE_FORMAT.
For question 2, it uses the order at definition of the table. However you have to give values for all columns in that case.
Oracle supports Standard SQL date literals (since 9i).
It's DATE followed by a string with 'yyyy-mm-dd' format
DATE '2014-05-10'
It's much shorter than TO_DATE and it's independent of any NLS settings.
Similar for timestamps:
TIMESTAMP '2014-05-10 09:52:35'
Regarding your 2nd question: It's the order of columns as defined within the CREATE TABLE.
You could even do it like this one:
ALTER SESSION SET NLS_DATE_FORMAT = 'MM:YYYY:DD';
INSERT INTO some_table (date1) VALUES ('05:2014:10');
...but doing it like this is not recommended. Use TO_DATE or DATE Literal, e.g. DATE '2014-05-10' instead. It makes your life easier.