What format does Oracle expect for a `DATE` value? - sql

I used the relational model in SQL Model developer from Oracle to create this table:
CREATE TABLE Orders
(
OrderNum INTEGER NOT NULL ,
OrderDate DATE ,
TIME DATE ,
Employee_EMP_CODE VARCHAR2 (10) NOT NULL ,
Tax_Id VARCHAR2 (10) ,
Tax_Rate VARCHAR2 (10) ,
Customers_Customer_No INTEGER NOT NULL ,
Consoles_Console_NO VARCHAR2 (10) NOT NULL ,
Customers_Buyer_First_Name VARCHAR2 (10) NOT NULL ,
Customers_Buyer_Last_Name VARCHAR2 (10) NOT NULL ,
Comments VARCHAR2 (250)
) ;
For some reason it will not take the time value when I try to insert a row. I have tried these formats
'12:31:00'
'12.31.00'
It keeps giving me the error that the month value isn't valid.

An Oracle date column always has a day and a time component. It makes no sense to have two columns OrderDate and Time both of type date. Your OrderDate would have a time component and your Time would have a day component. Realistically, you just want a single column of type date that has both the day and the time that the order was placed.
If you are trying to insert data into a date column, you would either want to pass the proper data type from your client application (i.e. bind a date in your client application when you create your insert statement) or you would want to do an explicit conversion from a string using the to_date function. For example
INSERT INTO Orders( OrderNum, OrderDate, <<more columns>> )
VALUES( 1, to_date( '2014-07-12 12:31:00', 'YYYY-MM-DD HH24:MI:SS' ), <<more values>> );

To answer your question "What format does Oracle expect for a DATE value?"
You can look at your NLS_DATE_FORMAT:
SQL> SHOW NLS_DATE_FORMAT
Assuming you're using Default, it should come back with something like: DD-MON-RR
You could actually insert or select without To_Date like this:
Select * from Orders Where OrderDate = '25-DEC-04';
To answer your real question: How should I store my orderDate :)
It seems you're storing Order Date, and Order Time in two separate fields.
There is really no need to do this, as this means you'll be using twice the disc space for your data field, which you only actually need 1 field.
as DATE datatype will store both Date and Time, you can actually just use 1 field, and this will make life a lot easier when trying to select your data as well.
You really should look at using sysdate at the moment your order is place.
ie:
SELECT TO_CHAR
(SYSDATE, 'MM-DD-YYYY HH24:MI:SS') "OrderDate"
FROM DUAL;
NOW
-------------------
04-13-2001 09:45:51
Also please notice that i've made use of TO_CHAR for printing purposes only.
when inserting, you will simply do:
Insert into Orders(OrderDate, other fields) Values (sysdate, other fields);
I hope this helps.

Oracle is very annoying when it comes to date stuff. However, I have had good luck with this format:
{ts 'yyyy-mm-dd HH:mi:ss'}
year, month, day, hours, minutes, seconds. Use leading zeros if appropriate. Use 24 hour clock for hours.

Related

ORA-01858: a non-numeric character was found where a numeric was expected

I am beginner in oracle and need help regarding following query:
select admin_id, to_date(date_created, 'DD-MM-YYYY'), name, mobile, email, (select status from status where status.status_id = admin.status_id) from admin;
Here are my tables:
SQL> desc admin
Name Null? Type
----------------------------------------- -------- ----------------------------
ADMIN_ID NOT NULL NUMBER(5)
STATUS_ID NOT NULL NUMBER(5)
DATE_CREATED TIMESTAMP(6)
STATUS_DATE DATE
NAME VARCHAR2(45)
MOBILE VARCHAR2(20)
EMAIL VARCHAR2(110)
SQL> desc status
Name Null? Type
----------------------------------------- -------- ----------------------------
STATUS_ID NOT NULL NUMBER(5)
STATUS VARCHAR2(36)
The main problem is I want to get status.status on basis of admin.status_id. The date could be fixed by using it as char to_char(date_created,'DD/MM/YYYY')
to_date(date_created, 'DD-MM-YYYY') doesn't make a whole lot of sense.
date_created is a timestamp with 6 decimal digits of sub-second precision.
to_date takes a string. So Oracle has to first convert the timestamp to a string using the session's nls_timestamp_format. That is the string that gets sent to to_date.
to_date then tries to convert that string back to a date (which will always have a time component) using the "DD-MM-YYYY" format mask. Unless that happens to be what your nls_timestamp_format is set to, that conversion will fail and will probably throw the error you reported.
If all this casting worked, you'd end up sending the Java application a date that has the time set to midnight. As a general rule, though, it would normally make more sense to just send the timestamp back to the Java application and let it handle how to convert that to a string and what components of the timestamp needed to be shown. So my default would be to just select date_created.
If you really want Oracle to do the data type conversion and to set the time component of the date to midnight, you can use trunc and cast instead. This avoids implicit data type conversion and eliminates dependencies on the session's nls_timestamp_format.
trunc( cast( date_created as date ) )
Check the values in the admin_id and status_id columns. Make sure they are correct data types.
Can you rearrange the query below and check output. Try to use join where possible instead of the nested query to improve the performance of the database:
select ad.admin_id, to_date(ad.date_created, 'DD-MM-YYYY'),
ad.name, ad.mobile, ad.email,st.status
from admin ad,status st
where ad.admin_id=st.status_id;
Here is link for a similar issue: Getting Error - ORA-01858: a non-numeric character was found where a numeric was expected (Stackoverflow)

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

is that possible sql keep tracking the time?

I'm a freshman in sql. So that recently my lecture had give us one practical.
The practical file was showing an error.
INSERT INTO sales VALUES ('6380', '1111', '2012-10-01 00:00:00', 2, '30', 'TC7777');
The error was 00:00:00. As my lecture say SQL did not allow this format
So here comes my question:
- Is that possible SQL keep tracking all the time , like the 00:00:00 ?
- And is that SQL are key sensitive , SELECT and select or I should keep the habit that put all in upper case ?
Speaking to Oracle only, the database ALWAYS stores times with date/timestamp values. You just have to properly tell it to do so. In your example, you could say:
INSERT INTO sales
VALUES ('6380', '1111', TO_DATE('2012-10-01 00:00:00','YYYY-MM-DD HH:MI:SS')...
The default format for date values in Oracle is governed by the NLS_DATE_FORMAT system parameter in the database. If you don't explicitly supply date values with the TO_DATE function, Oracle will expect date strings to conform to this format. That's why you're getting an error in your example. Also, if you don't supply a time value, it defaults to 00:00:00.
The same principle applies on output, too. You can accept the default output for a date value by simply selecting the value:
SQL> select sysdate from dual;
SYSDATE
---------
27-JUN-15
SQL>
Or you can format it with TO_CHAR:
SQL> select TO_CHAR(sysdate, 'YYYY-MM-DD HH24:MI:SS') from dual;
TO_CHAR(SYSDATE,'YY
-------------------
2015-06-27 11:10:48
SQL>
Is that possible SQL keep tracking all the time , like the 00:00:00 ?
If your Datatype in SQL table is Date, By default in Oracle, Default format for date is Date only so If you are specifying Time with date while inserting data you get error.
To Insert time with date, you have to convert date format while insert data
TO_DATE('2003/05/03 21:02:44', 'yyyy/mm/dd hh24:mi:ss')
SEE Example
You can change this default date format with
alter session set nls_date_format= your date format here
NLS_DATE_FORMAT Refer Here
is that SQL are key sensitive , SELECT and select or I should keep the
habit that put all in upper case ?
SQL is not case sensitive, you can use either one, UPPER CASE, lower case or even Camel Case. Example :- SELECT or select or even Select. Just for better readability, Prefer to use All keywords in UPPER Case.

Insert Timestamp into Varchar2 column in Oracle Database

I am working with an application that interfaces with an Oracle 11g database. I need to insert a timestamp in the YYYYMMDDhhmmss format, into a Varchar2 column.
My SQL update query is:
update CODELIST_XREF_ITEM set RECEIVER_ITEM=TIMESTAMP where LIST_NAME='BUSINESS_PROCESS';
The TIMESTAMP variable is where it need to be inserted. I have tried setting the TIMESTAMP variable to CURRENT_TIMESTAMP, however I am not able to format that.
Any suggestions?
Thanks in advance for all your help!
If you want to insert the character representation of a timestamp (it actually appears that you want the character representation of an Oracle date since you don't want fractional seconds or a time zone), you'd use the to_char function.
to_char( sysdate, 'YYYYMMDDHH24MISS' )
So your UPDATE statement would be
update CODELIST_XREF_ITEM
set RECEIVER_ITEM=to_char( sysdate, 'YYYYMMDDHH24MISS' )
where LIST_NAME='BUSINESS_PROCESS';
If you wanted to store fractional seconds or a time zone or do something else that required a timestamp, you'd use the same to_char function just with a different format mask.

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

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)