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.
Related
can we insert date into varchar2 in oracle without converting by to_char?
Oracle will convert the date into a string using what localization settings are in place during the insert. This is true not only of Oracle but of any database. For a date, that might commonly be in the format "DD-MON-YY".
Here is an example of inserting a date into a varchar2 column.
That said, you should not do this. You should be storing date/time values in the database using the correct data type -- and that would be either date or timestamp. If you want to insert a constant, then use the date or timestamp qualifier with the appropriate value following it.
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.
I'm in coldfusion working with data from an sql table, and using a query of queries to join the sql data to some data from an oracle database. Unfortunately, I need to order them by date, and the oracle table has two columns - DRWR_DATE which is of type DATE and TIME which is of type VARCHAR2. The two columns put into a string read 17-JUN-03 16:35:18 or something similar. I need to return these two columns as a TIMESTAMP so I can use query of queries to sort them.
Also, I think I read that a date column holds the time in Oracle anyway? I don't have much experience with Oracle so I am unsure how best to do this.
Try this:
SELECT to_timestamp(
to_char( drwr_date,'dd-mon-yy') ||' '|| time
, 'dd-mon-yy hh24:mi:ss'
)
FROM your_table
Try using TO_TIMESTAMP function:
SELECT TO_TIMESTAMP('17-JUN-03 16:35:18', 'DD-MON-RR HH24:MI:SS')
FROM DUAL;
you can try converting the column to date as the following:
TO_DATE(column,'DD-MON-YY HH24:MI:SS')
The first parameter takes your column and the second parameter specifies the date format that is used
If all you have to do is order by date, all you need is an order by clause.
order by drwr_date, time
You don't have to cast these to anything, unless you need to do so for another reason. Remember that a date datatype is essentially a floating point number. This, "17-JUN-03" is simply how your client is displaying it.
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.
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.